Skip to content

Commit

Permalink
fix: missing nil checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mistahj67 committed Aug 28, 2024
1 parent e27373e commit 5e157cf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
17 changes: 5 additions & 12 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ func getAzureObjectList[T any](client rest.RestClient, ctx context.Context, path
_ = pipeline.Send(ctx.Done(), out, errResult)
return
} else {
if req, err := rest.NewRequest(ctx, "GET", nextUrl, nil, params.AsMap(), nil); err != nil {
paramsMap := make(map[string]string)
if params != nil {
paramsMap = params.AsMap()
}
if req, err := rest.NewRequest(ctx, "GET", nextUrl, nil, paramsMap, nil); err != nil {
errResult.Error = err
_ = pipeline.Send(ctx.Done(), out, errResult)
return
Expand Down Expand Up @@ -164,17 +168,6 @@ func getAzureObjectList[T any](client rest.RestClient, ctx context.Context, path
}
}

func getAzureObject[T any](client rest.RestClient, ctx context.Context, path string, params query.Params) (T, error) {
var response T
if res, err := client.Get(ctx, path, params, nil); err != nil {
return response, err
} else if err := rest.Decode(res.Body, &response); err != nil {
return response, err
} else {
return response, nil
}
}

type azureClient struct {
msgraph rest.RestClient
resourceManager rest.RestClient
Expand Down
38 changes: 29 additions & 9 deletions client/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ func (s *restClient) Authenticate() error {

func (s *restClient) Delete(ctx context.Context, path string, body interface{}, params query.Params, headers map[string]string) (*http.Response, error) {
endpoint := s.api.ResolveReference(&url.URL{Path: path})
if req, err := NewRequest(ctx, http.MethodDelete, endpoint, body, params.AsMap(), headers); err != nil {
paramsMap := make(map[string]string)
if params != nil {
paramsMap = params.AsMap()
}
if req, err := NewRequest(ctx, http.MethodDelete, endpoint, body, paramsMap, headers); err != nil {
return nil, err
} else {
return s.Send(req)
Expand All @@ -166,15 +170,19 @@ func (s *restClient) Delete(ctx context.Context, path string, body interface{},

func (s *restClient) Get(ctx context.Context, path string, params query.Params, headers map[string]string) (*http.Response, error) {
endpoint := s.api.ResolveReference(&url.URL{Path: path})
paramsMap := make(map[string]string)

if params.NeedsEventualConsistencyHeaderFlag() {
if headers == nil {
headers = make(map[string]string)
if params != nil {
paramsMap = params.AsMap()
if params.NeedsEventualConsistencyHeaderFlag() {
if headers == nil {
headers = make(map[string]string)
}
headers["ConsistencyLevel"] = "eventual"
}
headers["ConsistencyLevel"] = "eventual"
}

if req, err := NewRequest(ctx, http.MethodGet, endpoint, nil, params.AsMap(), headers); err != nil {
if req, err := NewRequest(ctx, http.MethodGet, endpoint, nil, paramsMap, headers); err != nil {
return nil, err
} else {
return s.Send(req)
Expand All @@ -183,7 +191,11 @@ func (s *restClient) Get(ctx context.Context, path string, params query.Params,

func (s *restClient) Patch(ctx context.Context, path string, body interface{}, params query.Params, headers map[string]string) (*http.Response, error) {
endpoint := s.api.ResolveReference(&url.URL{Path: path})
if req, err := NewRequest(ctx, http.MethodPatch, endpoint, body, params.AsMap(), headers); err != nil {
paramsMap := make(map[string]string)
if params != nil {
paramsMap = params.AsMap()
}
if req, err := NewRequest(ctx, http.MethodPatch, endpoint, body, paramsMap, headers); err != nil {
return nil, err
} else {
return s.Send(req)
Expand All @@ -192,7 +204,11 @@ func (s *restClient) Patch(ctx context.Context, path string, body interface{}, p

func (s *restClient) Post(ctx context.Context, path string, body interface{}, params query.Params, headers map[string]string) (*http.Response, error) {
endpoint := s.api.ResolveReference(&url.URL{Path: path})
if req, err := NewRequest(ctx, http.MethodPost, endpoint, body, params.AsMap(), headers); err != nil {
paramsMap := make(map[string]string)
if params != nil {
paramsMap = params.AsMap()
}
if req, err := NewRequest(ctx, http.MethodPost, endpoint, body, paramsMap, headers); err != nil {
return nil, err
} else {
return s.Send(req)
Expand All @@ -201,7 +217,11 @@ func (s *restClient) Post(ctx context.Context, path string, body interface{}, pa

func (s *restClient) Put(ctx context.Context, path string, body interface{}, params query.Params, headers map[string]string) (*http.Response, error) {
endpoint := s.api.ResolveReference(&url.URL{Path: path})
if req, err := NewRequest(ctx, http.MethodPost, endpoint, body, params.AsMap(), headers); err != nil {
paramsMap := make(map[string]string)
if params != nil {
paramsMap = params.AsMap()
}
if req, err := NewRequest(ctx, http.MethodPost, endpoint, body, paramsMap, headers); err != nil {
return nil, err
} else {
return s.Send(req)
Expand Down

0 comments on commit 5e157cf

Please sign in to comment.