Skip to content

Commit

Permalink
Merge pull request #23 from Avanis-GmbH/linting
Browse files Browse the repository at this point in the history
Fixed variable and function naming and other issues
  • Loading branch information
Valentin-Kaiser authored Feb 15, 2024
2 parents 2adac56 + 62ca15d commit d747bc5
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 44 deletions.
10 changes: 5 additions & 5 deletions com/bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}
Expand All @@ -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): {
Expand Down
2 changes: 1 addition & 1 deletion com/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 14 additions & 16 deletions com/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,16 @@ 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")
}

// 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)
Expand All @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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,
}
}
14 changes: 7 additions & 7 deletions com/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}

Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
Expand Down
8 changes: 4 additions & 4 deletions com/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion com/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
20 changes: 11 additions & 9 deletions com/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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"`
}
1 change: 0 additions & 1 deletion com/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit d747bc5

Please sign in to comment.