Skip to content

Commit 5e157cf

Browse files
committed
fix: missing nil checks
1 parent e27373e commit 5e157cf

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

client/client.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ func getAzureObjectList[T any](client rest.RestClient, ctx context.Context, path
124124
_ = pipeline.Send(ctx.Done(), out, errResult)
125125
return
126126
} else {
127-
if req, err := rest.NewRequest(ctx, "GET", nextUrl, nil, params.AsMap(), nil); err != nil {
127+
paramsMap := make(map[string]string)
128+
if params != nil {
129+
paramsMap = params.AsMap()
130+
}
131+
if req, err := rest.NewRequest(ctx, "GET", nextUrl, nil, paramsMap, nil); err != nil {
128132
errResult.Error = err
129133
_ = pipeline.Send(ctx.Done(), out, errResult)
130134
return
@@ -164,17 +168,6 @@ func getAzureObjectList[T any](client rest.RestClient, ctx context.Context, path
164168
}
165169
}
166170

167-
func getAzureObject[T any](client rest.RestClient, ctx context.Context, path string, params query.Params) (T, error) {
168-
var response T
169-
if res, err := client.Get(ctx, path, params, nil); err != nil {
170-
return response, err
171-
} else if err := rest.Decode(res.Body, &response); err != nil {
172-
return response, err
173-
} else {
174-
return response, nil
175-
}
176-
}
177-
178171
type azureClient struct {
179172
msgraph rest.RestClient
180173
resourceManager rest.RestClient

client/rest/client.go

+29-9
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,11 @@ func (s *restClient) Authenticate() error {
157157

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

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

170-
if params.NeedsEventualConsistencyHeaderFlag() {
171-
if headers == nil {
172-
headers = make(map[string]string)
175+
if params != nil {
176+
paramsMap = params.AsMap()
177+
if params.NeedsEventualConsistencyHeaderFlag() {
178+
if headers == nil {
179+
headers = make(map[string]string)
180+
}
181+
headers["ConsistencyLevel"] = "eventual"
173182
}
174-
headers["ConsistencyLevel"] = "eventual"
175183
}
176184

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

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

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

202218
func (s *restClient) Put(ctx context.Context, path string, body interface{}, params query.Params, headers map[string]string) (*http.Response, error) {
203219
endpoint := s.api.ResolveReference(&url.URL{Path: path})
204-
if req, err := NewRequest(ctx, http.MethodPost, endpoint, body, params.AsMap(), headers); err != nil {
220+
paramsMap := make(map[string]string)
221+
if params != nil {
222+
paramsMap = params.AsMap()
223+
}
224+
if req, err := NewRequest(ctx, http.MethodPost, endpoint, body, paramsMap, headers); err != nil {
205225
return nil, err
206226
} else {
207227
return s.Send(req)

0 commit comments

Comments
 (0)