From 62ca15d55f1c3fdf6c4b200c4497cc49fb0d8718 Mon Sep 17 00:00:00 2001 From: Valentin Kaiser Date: Thu, 15 Feb 2024 16:47:30 +0100 Subject: [PATCH] [FIX] variable and function naming and other issues --- com/bulk.go | 10 +++++----- com/cache.go | 2 +- com/client.go | 30 ++++++++++++++---------------- com/collections.go | 14 +++++++------- com/credentials.go | 8 ++++---- com/info.go | 2 +- com/search.go | 20 +++++++++++--------- com/type.go | 1 - 8 files changed, 43 insertions(+), 44 deletions(-) diff --git a/com/bulk.go b/com/bulk.go index 9c28467..495018a 100644 --- a/com/bulk.go +++ b/com/bulk.go @@ -16,12 +16,12 @@ type SyncOperation struct { } type deleteEntity struct { - Id string `json:"id"` + ID string `json:"id"` } // Starts a sync process for the list of provided actions. // This can be inserts, upserts, updates and deletes on different entities. -func (c *Client) Sync(ctx ApiContext, payload map[string]SyncOperation) (*http.Response, error) { +func (c *Client) Sync(ctx APIContext, payload map[string]SyncOperation) (*http.Response, error) { req, err := c.NewRequest(ctx, http.MethodPost, "/api/_action/sync", nil, payload) if err != nil { @@ -34,7 +34,7 @@ func (c *Client) Sync(ctx ApiContext, payload map[string]SyncOperation) (*http.R // Inserts or updates the provided entities in bulk mode // Provided entities must be a slice // Uses underlying Sync method to perform the request -func (c *Client) Upsert(ctx ApiContext, entities interface{}) (*http.Response, error) { +func (c *Client) Upsert(ctx APIContext, entities interface{}) (*http.Response, error) { if c.GetSegmentSnakeCase(entities) == "unknown" { return nil, errors.New("unknown entity") } @@ -52,11 +52,11 @@ func (c *Client) Upsert(ctx ApiContext, entities interface{}) (*http.Response, e // Deletes the provided entities in bulk mode // Uses underlying Sync method to perform the request -func (c *Client) Delete(ctx ApiContext, entity interface{}, ids []string) (*http.Response, error) { +func (c *Client) Delete(ctx APIContext, entity interface{}, ids []string) (*http.Response, error) { payload := make([]deleteEntity, 0) for _, id := range ids { - payload = append(payload, deleteEntity{Id: id}) + payload = append(payload, deleteEntity{ID: id}) } return c.Sync(ctx, map[string]SyncOperation{c.GetSegmentSnakeCase(entity): { diff --git a/com/cache.go b/com/cache.go index 42ef75b..4aba1f6 100644 --- a/com/cache.go +++ b/com/cache.go @@ -7,7 +7,7 @@ import ( ) // Clear the shopware cache -func (c Client) Clear(ctx ApiContext) (*http.Response, error) { +func (c Client) Clear(ctx APIContext) (*http.Response, error) { r, err := c.NewRequest(ctx, http.MethodDelete, "/api/_action/cache", nil, nil) if err != nil { diff --git a/com/client.go b/com/client.go index 01bdb45..e3f8adf 100644 --- a/com/client.go +++ b/com/client.go @@ -86,10 +86,8 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*htt return resp, nil case io.Writer: _, err = io.Copy(v, resp.Body) - defer resp.Body.Close() default: err = json.NewDecoder(resp.Body).Decode(v) - defer resp.Body.Close() } return resp, errors.Wrap(err, "failed to decode response body") @@ -97,7 +95,7 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*htt // Creates a new request with the given context, method, url and body // The body will be encoded as json and the content type will be set to application/json -func (c *Client) NewRequest(context ApiContext, method, path string, options map[string]string, body interface{}) (*http.Request, error) { +func (c *Client) NewRequest(context APIContext, method, path string, options map[string]string, body interface{}) (*http.Request, error) { buf := &bytes.Buffer{} if body != nil { encoder := json.NewEncoder(buf) @@ -122,7 +120,7 @@ func (c *Client) NewRequest(context ApiContext, method, path string, options map // Creates a new request using a io.Reader as body and without encoding the body as json // This has to be done manually by the caller if needed -func (c *Client) NewRawRequest(context ApiContext, method, path string, options map[string]string, body io.Reader) (*http.Request, error) { +func (c *Client) NewRawRequest(context APIContext, method, path string, options map[string]string, body io.Reader) (*http.Request, error) { path, err := url.JoinPath(c.remote, path) if err != nil { return nil, err @@ -147,8 +145,8 @@ func (c *Client) NewRawRequest(context ApiContext, method, path string, options } req.Header.Set("Accept", "application/json") - req.Header.Set("sw-language-id", context.LanguageId) - req.Header.Set("sw-version-id", context.VersionId) + req.Header.Set("sw-language-id", context.LanguageID) + req.Header.Set("sw-version-id", context.VersionID) if context.SkipFlows { req.Header.Set("sw-skip-trigger-flow", "1") @@ -183,20 +181,20 @@ func (c *Client) checkResponse(r *http.Response) error { return errors.Wrapf(&aerr, "request failed") } -// ApiContext is the context for the api requests -type ApiContext struct { - Context context.Context - LanguageId string - VersionId string +// APIContext is the context for the api requests +type APIContext struct { + context.Context + LanguageID string + VersionID string SkipFlows bool } -// NewApiContext creates a new ApiContext with the given context and default values -func NewApiContext(ctx context.Context) ApiContext { - return ApiContext{ +// NewAPIContext creates a new ApiContext with the given context and default values +func NewAPIContext(ctx context.Context) APIContext { + return APIContext{ Context: ctx, - LanguageId: "2fbb5fe2e29a4d70aa5854ce7ce3e20b", - VersionId: "0fa91ce3e96a4bc2be4bd9ce752c3425", + LanguageID: "2fbb5fe2e29a4d70aa5854ce7ce3e20b", + VersionID: "0fa91ce3e96a4bc2be4bd9ce752c3425", SkipFlows: false, } } diff --git a/com/collections.go b/com/collections.go index c8385fe..6f1369c 100644 --- a/com/collections.go +++ b/com/collections.go @@ -5,11 +5,11 @@ import "github.com/Avanis-GmbH/shopware-kit/model" // Collection is the interface for all collections returned by the shopware api // Implemented by EntityCollection which in turn is embedded by all other collections. type Collection interface { - setTotal(int64) + setTotal(total int64) getTotal() int64 - setAggregations(interface{}) + setAggregations(aggregations interface{}) getAggregations() interface{} - setData([]interface{}) + setData(data []interface{}) getData() []interface{} } @@ -45,13 +45,13 @@ func (c EntityCollection) getData() []interface{} { return c.Data } -type AclRoleCollection struct { +type ACLRoleCollection struct { EntityCollection Data []model.AclRole `json:"data"` } -type AclUserRoleCollection struct { +type ACLUserRoleCollection struct { EntityCollection Data []model.AclUserRole `json:"data"` @@ -1071,13 +1071,13 @@ type ScriptCollection struct { Data []model.Script `json:"data"` } -type SeoUrlTemplateCollection struct { +type SEOUrlTemplateCollection struct { EntityCollection Data []model.SeoUrlTemplate `json:"data"` } -type SeoUrlCollection struct { +type SEOUrlCollection struct { EntityCollection Data []model.SeoUrl `json:"data"` diff --git a/com/credentials.go b/com/credentials.go index 7aa844f..490ebc3 100644 --- a/com/credentials.go +++ b/com/credentials.go @@ -48,15 +48,15 @@ func (c PasswordCredentials) GetTokenSource(ctx context.Context, tokenURL string // IntegrationCredentials are the credentials for the authorization using a API key type IntegrationCredentials struct { - ClientId string + ClientID string ClientSecret string Scopes []string } // NewIntegrationCredentials creates a new IntegrationCredentials struct with the given parameters -func NewIntegrationCredentials(clientId, clientSecret string, scopes []string) IntegrationCredentials { +func NewIntegrationCredentials(clientID, clientSecret string, scopes []string) IntegrationCredentials { return IntegrationCredentials{ - ClientId: clientId, + ClientID: clientID, ClientSecret: clientSecret, Scopes: scopes, } @@ -65,7 +65,7 @@ func NewIntegrationCredentials(clientId, clientSecret string, scopes []string) I // GetTokenSource returns the token source for the client credentials grant type func (c IntegrationCredentials) GetTokenSource(ctx context.Context, tokenURL string) (oauth2.TokenSource, error) { oauthConf := &clientcredentials.Config{ - ClientID: c.ClientId, + ClientID: c.ClientID, ClientSecret: c.ClientSecret, Scopes: c.Scopes, TokenURL: tokenURL, diff --git a/com/info.go b/com/info.go index e307eb5..ca04112 100644 --- a/com/info.go +++ b/com/info.go @@ -34,7 +34,7 @@ func (r InfoResponse) IsCloudShop() bool { } // Info returns the info of the shopware api -func (c *Client) Info(ctx ApiContext) (*InfoResponse, *http.Response, error) { +func (c *Client) Info(ctx APIContext) (*InfoResponse, *http.Response, error) { r, err := c.NewRequest(ctx, http.MethodGet, "/api/_info/config", nil, nil) if err != nil { return nil, nil, errors.Wrap(err, "failed to create request for info") diff --git a/com/search.go b/com/search.go index 7757cf9..311befa 100644 --- a/com/search.go +++ b/com/search.go @@ -8,7 +8,7 @@ import ( // Search performs a search request for the given criteria and collection // The collection must be a pointer to a struct that implements the Collection interface -func (c *Client) Search(ctx ApiContext, criteria Criteria, v Collection) error { +func (c *Client) Search(ctx APIContext, criteria Criteria, v Collection) error { url, err := url.JoinPath("/api/search", c.GetSegment(v)) if err != nil { return errors.Wrapf(err, "failed to join path for %T", v) @@ -19,16 +19,17 @@ func (c *Client) Search(ctx ApiContext, criteria Criteria, v Collection) error { return errors.Wrapf(err, "failed to create request for %T", v) } - _, err = c.Do(ctx.Context, req, v) + resp, err := c.Do(ctx.Context, req, v) if err != nil { return errors.Wrapf(err, "failed to do Search request for %T", v) } + defer resp.Body.Close() return nil } // SearchAll performs multiple search requests with a limited size until all results are fetched -func (c *Client) SearchAll(ctx ApiContext, criteria Criteria, v Collection) error { +func (c *Client) SearchAll(ctx APIContext, criteria Criteria, v Collection) error { if criteria.Limit == 0 { criteria.Limit = 50 } @@ -58,8 +59,8 @@ func (c *Client) SearchAll(ctx ApiContext, criteria Criteria, v Collection) erro return nil } -// SearchIds performs a search request for the given criteria and returns only the ids -func (c *Client) SearchIds(ctx ApiContext, criteria Criteria, v interface{}) (*SearchIdsResponse, error) { +// SearchIDs performs a search request for the given criteria and returns only the ids +func (c *Client) SearchIDs(ctx APIContext, criteria Criteria, v interface{}) (*SearchIDsResponse, error) { url, err := url.JoinPath("/api/search-ids", c.GetSegment(v)) if err != nil { return nil, errors.Wrapf(err, "failed to join path for %T", v) @@ -70,17 +71,18 @@ func (c *Client) SearchIds(ctx ApiContext, criteria Criteria, v interface{}) (*S return nil, errors.Wrapf(err, "failed to create request for %T", v) } - data := &SearchIdsResponse{} - _, err = c.Do(ctx.Context, req, data) + data := &SearchIDsResponse{} + resp, err := c.Do(ctx.Context, req, data) if err != nil { return data, errors.Wrapf(err, "failed to do searchIds request for %T", v) } + defer resp.Body.Close() return data, nil } -// SearchIdsResponse is the response for a searchIds request -type SearchIdsResponse struct { +// SearchIDsResponse is the response for a searchIds request +type SearchIDsResponse struct { Total int `json:"total"` Data []string `json:"data"` } diff --git a/com/type.go b/com/type.go index 25134e1..da5e292 100644 --- a/com/type.go +++ b/com/type.go @@ -28,7 +28,6 @@ func (c *Client) GetSegment(v interface{}) string { } func (c *Client) GetSegmentSnakeCase(v interface{}) string { - // If the type name contains "Collection", remove it. return strcase.ToSnake(c.GetSegment(v)) }