From 273ebd61d7379a9e486493f15bd84a7c1a5d46f9 Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Mon, 28 Aug 2023 16:43:15 +0300 Subject: [PATCH] private/apigen/example: make it nicer Change-Id: I1bd779090a902ed2b99b3993dc7cf61fc250f10f --- private/apigen/example/api.gen.go | 26 ++++++++++++------------ private/apigen/example/apidocs.gen.md | 8 ++++---- private/apigen/example/client-api.gen.ts | 4 ++-- private/apigen/example/gen.go | 5 +++-- private/apigen/gogen_test.go | 6 +++--- 5 files changed, 25 insertions(+), 24 deletions(-) diff --git a/private/apigen/example/api.gen.go b/private/apigen/example/api.gen.go index 98f047071..e2ac2008f 100644 --- a/private/apigen/example/api.gen.go +++ b/private/apigen/example/api.gen.go @@ -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))) } } diff --git a/private/apigen/example/apidocs.gen.md b/private/apigen/example/apidocs.gen.md index 52ff036bb..5ca137a00 100644 --- a/private/apigen/example/apidocs.gen.md +++ b/private/apigen/example/apidocs.gen.md @@ -6,14 +6,14 @@

List of Endpoints

-* TestAPI - * [](#testapi-) +* Documents + * [Update Content](#documents-update-content) -

(go to full list)

+

Update Content (go to full list)

-`POST /api/v0/testapi/{path}` +`POST /api/v0/docs/{path}` **Query Params:** diff --git a/private/apigen/example/client-api.gen.ts b/private/apigen/example/client-api.gen.ts index f871182f4..6d6bc5ab5 100644 --- a/private/apigen/example/client-api.gen.ts +++ b/private/apigen/example/client-api.gen.ts @@ -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}`; diff --git a/private/apigen/example/gen.go b/private/apigen/example/gen.go index 9267fb456..ecb2095a3 100644 --- a/private/apigen/example/gen.go +++ b/private/apigen/example/gen.go @@ -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"` diff --git a/private/apigen/gogen_test.go b/private/apigen/gogen_test.go index 857a48c94..41a4136bc 100644 --- a/private/apigen/gogen_test.go +++ b/private/apigen/gogen_test.go @@ -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()),