From adb4d6ebc5e9210b85ced294db26c42a5b68cfed Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Thu, 7 Sep 2023 18:30:43 +0300 Subject: [PATCH] stubs unused --- api/openapi/api.yaml | 14 +++++ internal/app/rest_server.go | 5 ++ internal/domain/rest/api.gen.go | 20 +++++++ pkg/sdk/api.gen.go | 102 ++++++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+) diff --git a/api/openapi/api.yaml b/api/openapi/api.yaml index 8b1a3f22..0b116916 100644 --- a/api/openapi/api.yaml +++ b/api/openapi/api.yaml @@ -41,6 +41,20 @@ paths: $ref: '#/components/schemas/MessageOK' # stubs + /stubs/unused: + get: + tags: + - stubs + summary: Getting a list of unused stubs + description: The list is needed to quickly find unused stubs + operationId: listUnusedStubs + responses: + '200': + description: Successful operation + content: + application/json: + schema: + $ref: '#/components/schemas/StubList' /stubs: get: tags: diff --git a/internal/app/rest_server.go b/internal/app/rest_server.go index 1b37d304..45465656 100644 --- a/internal/app/rest_server.go +++ b/internal/app/rest_server.go @@ -102,6 +102,11 @@ func (h *StubsServer) DeleteStubByID(w http.ResponseWriter, _ *http.Request, _ r panic("DeleteStubByID") } +func (h *StubsServer) ListUnusedStubs(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + panic("ListUnusedStubs") +} + func (h *StubsServer) ListStubs(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "application/json") err := json.NewEncoder(w).Encode(h.stubs.Stubs()) diff --git a/internal/domain/rest/api.gen.go b/internal/domain/rest/api.gen.go index ed594461..2645d67f 100644 --- a/internal/domain/rest/api.gen.go +++ b/internal/domain/rest/api.gen.go @@ -98,6 +98,9 @@ type ServerInterface interface { // Stub storage search // (POST /stubs/search) SearchStubs(w http.ResponseWriter, r *http.Request) + // Getting a list of unused stubs + // (GET /stubs/unused) + ListUnusedStubs(w http.ResponseWriter, r *http.Request) // Deletes stub by ID // (DELETE /stubs/{uuid}) DeleteStubByID(w http.ResponseWriter, r *http.Request, uuid ID) @@ -202,6 +205,21 @@ func (siw *ServerInterfaceWrapper) SearchStubs(w http.ResponseWriter, r *http.Re handler(w, r.WithContext(ctx)) } +// ListUnusedStubs operation middleware +func (siw *ServerInterfaceWrapper) ListUnusedStubs(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + handler := func(w http.ResponseWriter, r *http.Request) { + siw.Handler.ListUnusedStubs(w, r) + } + + for _, middleware := range siw.HandlerMiddlewares { + handler = middleware(handler) + } + + handler(w, r.WithContext(ctx)) +} + // DeleteStubByID operation middleware func (siw *ServerInterfaceWrapper) DeleteStubByID(w http.ResponseWriter, r *http.Request) { ctx := r.Context() @@ -353,6 +371,8 @@ func HandlerWithOptions(si ServerInterface, options GorillaServerOptions) http.H r.HandleFunc(options.BaseURL+"/stubs/search", wrapper.SearchStubs).Methods("POST") + r.HandleFunc(options.BaseURL+"/stubs/unused", wrapper.ListUnusedStubs).Methods("GET") + r.HandleFunc(options.BaseURL+"/stubs/{uuid}", wrapper.DeleteStubByID).Methods("DELETE") return r diff --git a/pkg/sdk/api.gen.go b/pkg/sdk/api.gen.go index d19d6d23..2600ae42 100644 --- a/pkg/sdk/api.gen.go +++ b/pkg/sdk/api.gen.go @@ -177,6 +177,9 @@ type ClientInterface interface { SearchStubs(ctx context.Context, body SearchStubsJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) + // ListUnusedStubs request + ListUnusedStubs(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) + // DeleteStubByID request DeleteStubByID(ctx context.Context, uuid ID, reqEditors ...RequestEditorFn) (*http.Response, error) } @@ -277,6 +280,18 @@ func (c *Client) SearchStubs(ctx context.Context, body SearchStubsJSONRequestBod return c.Client.Do(req) } +func (c *Client) ListUnusedStubs(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewListUnusedStubsRequest(c.Server) + if err != nil { + return nil, err + } + req = req.WithContext(ctx) + if err := c.applyEditors(ctx, req, reqEditors); err != nil { + return nil, err + } + return c.Client.Do(req) +} + func (c *Client) DeleteStubByID(ctx context.Context, uuid ID, reqEditors ...RequestEditorFn) (*http.Response, error) { req, err := NewDeleteStubByIDRequest(c.Server, uuid) if err != nil { @@ -477,6 +492,33 @@ func NewSearchStubsRequestWithBody(server string, contentType string, body io.Re return req, nil } +// NewListUnusedStubsRequest generates requests for ListUnusedStubs +func NewListUnusedStubsRequest(server string) (*http.Request, error) { + var err error + + serverURL, err := url.Parse(server) + if err != nil { + return nil, err + } + + operationPath := fmt.Sprintf("/stubs/unused") + if operationPath[0] == '/' { + operationPath = "." + operationPath + } + + queryURL, err := serverURL.Parse(operationPath) + if err != nil { + return nil, err + } + + req, err := http.NewRequest("GET", queryURL.String(), nil) + if err != nil { + return nil, err + } + + return req, nil +} + // NewDeleteStubByIDRequest generates requests for DeleteStubByID func NewDeleteStubByIDRequest(server string, uuid ID) (*http.Request, error) { var err error @@ -576,6 +618,9 @@ type ClientWithResponsesInterface interface { SearchStubsWithResponse(ctx context.Context, body SearchStubsJSONRequestBody, reqEditors ...RequestEditorFn) (*SearchStubsResponse, error) + // ListUnusedStubsWithResponse request + ListUnusedStubsWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*ListUnusedStubsResponse, error) + // DeleteStubByIDWithResponse request DeleteStubByIDWithResponse(ctx context.Context, uuid ID, reqEditors ...RequestEditorFn) (*DeleteStubByIDResponse, error) } @@ -713,6 +758,28 @@ func (r SearchStubsResponse) StatusCode() int { return 0 } +type ListUnusedStubsResponse struct { + Body []byte + HTTPResponse *http.Response + JSON200 *StubList +} + +// Status returns HTTPResponse.Status +func (r ListUnusedStubsResponse) Status() string { + if r.HTTPResponse != nil { + return r.HTTPResponse.Status + } + return http.StatusText(0) +} + +// StatusCode returns HTTPResponse.StatusCode +func (r ListUnusedStubsResponse) StatusCode() int { + if r.HTTPResponse != nil { + return r.HTTPResponse.StatusCode + } + return 0 +} + type DeleteStubByIDResponse struct { Body []byte HTTPResponse *http.Response @@ -804,6 +871,15 @@ func (c *ClientWithResponses) SearchStubsWithResponse(ctx context.Context, body return ParseSearchStubsResponse(rsp) } +// ListUnusedStubsWithResponse request returning *ListUnusedStubsResponse +func (c *ClientWithResponses) ListUnusedStubsWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*ListUnusedStubsResponse, error) { + rsp, err := c.ListUnusedStubs(ctx, reqEditors...) + if err != nil { + return nil, err + } + return ParseListUnusedStubsResponse(rsp) +} + // DeleteStubByIDWithResponse request returning *DeleteStubByIDResponse func (c *ClientWithResponses) DeleteStubByIDWithResponse(ctx context.Context, uuid ID, reqEditors ...RequestEditorFn) (*DeleteStubByIDResponse, error) { rsp, err := c.DeleteStubByID(ctx, uuid, reqEditors...) @@ -961,6 +1037,32 @@ func ParseSearchStubsResponse(rsp *http.Response) (*SearchStubsResponse, error) return response, nil } +// ParseListUnusedStubsResponse parses an HTTP response from a ListUnusedStubsWithResponse call +func ParseListUnusedStubsResponse(rsp *http.Response) (*ListUnusedStubsResponse, error) { + bodyBytes, err := io.ReadAll(rsp.Body) + defer func() { _ = rsp.Body.Close() }() + if err != nil { + return nil, err + } + + response := &ListUnusedStubsResponse{ + Body: bodyBytes, + HTTPResponse: rsp, + } + + switch { + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: + var dest StubList + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON200 = &dest + + } + + return response, nil +} + // ParseDeleteStubByIDResponse parses an HTTP response from a DeleteStubByIDWithResponse call func ParseDeleteStubByIDResponse(rsp *http.Response) (*DeleteStubByIDResponse, error) { bodyBytes, err := io.ReadAll(rsp.Body)