private/apigen/example: make it nicer

Change-Id: I1bd779090a902ed2b99b3993dc7cf61fc250f10f
This commit is contained in:
Egon Elbre 2023-08-28 16:43:15 +03:00 committed by Storj Robot
parent 2d8f396eeb
commit 273ebd61d7
5 changed files with 25 additions and 24 deletions

View File

@ -20,10 +20,10 @@ import (
const dateLayout = "2006-01-02T15:04:05.999Z"
var ErrTestapiAPI = errs.Class("example testapi api")
var ErrDocsAPI = errs.Class("example docs api")
type TestAPIService interface {
GenTestAPI(ctx context.Context, path string, id uuid.UUID, date time.Time, request struct {
type DocumentsService interface {
UpdateContent(ctx context.Context, path string, id uuid.UUID, date time.Time, request struct {
Content string "json:\"content\""
}) (*struct {
ID uuid.UUID "json:\"id\""
@ -33,29 +33,29 @@ type TestAPIService interface {
}, api.HTTPError)
}
// TestAPIHandler is an api handler that exposes all testapi related functionality.
type TestAPIHandler struct {
// DocumentsHandler is an api handler that exposes all docs related functionality.
type DocumentsHandler struct {
log *zap.Logger
mon *monkit.Scope
service TestAPIService
service DocumentsService
auth api.Auth
}
func NewTestAPI(log *zap.Logger, mon *monkit.Scope, service TestAPIService, router *mux.Router, auth api.Auth) *TestAPIHandler {
handler := &TestAPIHandler{
func NewDocuments(log *zap.Logger, mon *monkit.Scope, service DocumentsService, router *mux.Router, auth api.Auth) *DocumentsHandler {
handler := &DocumentsHandler{
log: log,
mon: mon,
service: service,
auth: auth,
}
testapiRouter := router.PathPrefix("/api/v0/testapi").Subrouter()
testapiRouter.HandleFunc("/{path}", handler.handleGenTestAPI).Methods("POST")
docsRouter := router.PathPrefix("/api/v0/docs").Subrouter()
docsRouter.HandleFunc("/{path}", handler.handleUpdateContent).Methods("POST")
return handler
}
func (h *TestAPIHandler) handleGenTestAPI(w http.ResponseWriter, r *http.Request) {
func (h *DocumentsHandler) handleUpdateContent(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var err error
defer h.mon.Task()(&ctx)(&err)
@ -107,7 +107,7 @@ func (h *TestAPIHandler) handleGenTestAPI(w http.ResponseWriter, r *http.Request
return
}
retVal, httpErr := h.service.GenTestAPI(ctx, path, id, date, payload)
retVal, httpErr := h.service.UpdateContent(ctx, path, id, date, payload)
if httpErr.Err != nil {
api.ServeError(h.log, w, httpErr.Status, httpErr.Err)
return
@ -115,6 +115,6 @@ func (h *TestAPIHandler) handleGenTestAPI(w http.ResponseWriter, r *http.Request
err = json.NewEncoder(w).Encode(retVal)
if err != nil {
h.log.Debug("failed to write json GenTestAPI response", zap.Error(ErrTestapiAPI.Wrap(err)))
h.log.Debug("failed to write json UpdateContent response", zap.Error(ErrDocsAPI.Wrap(err)))
}
}

View File

@ -6,14 +6,14 @@
<h2 id='list-of-endpoints'>List of Endpoints</h2>
* TestAPI
* [](#testapi-)
* Documents
* [Update Content](#documents-update-content)
<h3 id='testapi-'> (<a href='#list-of-endpoints'>go to full list</a>)</h3>
<h3 id='documents-update-content'>Update Content (<a href='#list-of-endpoints'>go to full list</a>)</h3>
`POST /api/v0/testapi/{path}`
`POST /api/v0/docs/{path}`
**Query Params:**

View File

@ -15,9 +15,9 @@ export class {
body: string;
}
export class testapiHttpApiV0 {
export class docsHttpApiV0 {
private readonly http: HttpClient = new HttpClient();
private readonly ROOT_PATH: string = '/api/v0/testapi';
private readonly ROOT_PATH: string = '/api/v0/docs';
public async (request: , path: string, id: UUID, date: Time): Promise<> {
const path = `${this.ROOT_PATH}/${path}?id=${id}&date=${date}`;

View File

@ -16,10 +16,11 @@ import (
func main() {
a := &apigen.API{PackageName: "example", Version: "v0", BasePath: "/api"}
g := a.Group("TestAPI", "testapi")
g := a.Group("Documents", "docs")
g.Post("/{path}", &apigen.Endpoint{
MethodName: "GenTestAPI",
Name: "Update Content",
MethodName: "UpdateContent",
Response: struct {
ID uuid.UUID `json:"id"`
Date time.Time `json:"date"`

View File

@ -44,7 +44,7 @@ func (a auth) IsAuthenticated(ctx context.Context, r *http.Request, isCookieAuth
func (a auth) RemoveAuthCookie(w http.ResponseWriter) {}
func (s service) GenTestAPI(
func (s service) UpdateContent(
ctx context.Context,
pathParam string,
id uuid.UUID,
@ -98,7 +98,7 @@ func TestAPIServer(t *testing.T) {
defer ctx.Cleanup()
router := mux.NewRouter()
example.NewTestAPI(zaptest.NewLogger(t), monkit.Package(), service{}, router, auth{})
example.NewDocuments(zaptest.NewLogger(t), monkit.Package(), service{}, router, auth{})
server := httptest.NewServer(router)
defer server.Close()
@ -114,7 +114,7 @@ func TestAPIServer(t *testing.T) {
}
resp, err := send(ctx, http.MethodPost,
fmt.Sprintf("%s/api/v0/testapi/%s?id=%s&date=%s",
fmt.Sprintf("%s/api/v0/docs/%s?id=%s&date=%s",
server.URL,
expected.PathParam,
url.QueryEscape(expected.ID.String()),