Skip to content

Commit 4266fa2

Browse files
committed
service client: add no auth request and rework PricePlans method
1 parent ab7f751 commit 4266fa2

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

pkg/v2/client.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,49 @@ type RequestHeader struct {
9494
Value string
9595
}
9696

97+
// DoRequestWithoutAuth performs the HTTP request with the current ServiceClient's HTTPClient.
98+
// Authentication and optional headers will be added automatically.
99+
//
100+
//nolint:bodyclose
101+
func (client *ServiceClient) DoRequestWithoutAuth(
102+
ctx context.Context, method, path string, body io.Reader, headers ...*RequestHeader,
103+
) (*ResponseResult, error) {
104+
// Prepare an HTTP request with the provided context.
105+
request, err := http.NewRequestWithContext(ctx, method, path, body)
106+
if err != nil {
107+
return nil, err
108+
}
109+
110+
request.Header.Set("User-Agent", client.userAgent)
111+
if body != nil {
112+
request.Header.Set("Content-Type", "application/json")
113+
}
114+
115+
for _, header := range headers {
116+
request.Header.Set(header.Key, header.Value)
117+
}
118+
119+
// Send the HTTP request and populate the ResponseResult.
120+
response, err := client.HTTPClient.Do(request)
121+
if err != nil {
122+
return nil, err
123+
}
124+
125+
responseResult := &ResponseResult{
126+
Response: response,
127+
}
128+
129+
// Check status code and populate custom error body with extended error message if it's possible.
130+
if response.StatusCode >= http.StatusBadRequest {
131+
err = responseResult.extractErr()
132+
}
133+
if err != nil {
134+
return nil, err
135+
}
136+
137+
return responseResult, nil
138+
}
139+
97140
// DoRequest performs the HTTP request with the current ServiceClient's HTTPClient.
98141
// Authentication and optional headers will be added automatically.
99142
//

pkg/v2/price_plan.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,18 @@ func (p PricePlans) FindOneByName(name string) *PricePlan {
2525
return nil
2626
}
2727

28+
func (p PricePlans) FindOneID(id string) *PricePlan {
29+
for _, pp := range p {
30+
if pp.UUID == id {
31+
return pp
32+
}
33+
}
34+
35+
return nil
36+
}
37+
2838
func (client *ServiceClient) PricePlans(ctx context.Context) (PricePlans, *ResponseResult, error) {
29-
url := fmt.Sprintf("%s/plan", client.Endpoint)
39+
url := fmt.Sprintf("%s/pub/plan", client.Endpoint)
3040

3141
headers := []*RequestHeader{
3242
{
@@ -35,7 +45,7 @@ func (client *ServiceClient) PricePlans(ctx context.Context) (PricePlans, *Respo
3545
},
3646
}
3747

38-
responseResult, err := client.DoRequest(ctx, http.MethodGet, url, nil, headers...)
48+
responseResult, err := client.DoRequestWithoutAuth(ctx, http.MethodGet, url, nil, headers...)
3949
if err != nil {
4050
return nil, nil, err
4151
}

0 commit comments

Comments
 (0)