-
Notifications
You must be signed in to change notification settings - Fork 14
/
service.go
64 lines (54 loc) · 2.05 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Code generated by sysl DO NOT EDIT.
package downstream
import (
"context"
"fmt"
"net/http"
"net/url"
"github.com/anz-bank/sysl-go/common"
"github.com/anz-bank/sysl-go/restlib"
"github.com/anz-bank/sysl-go/validator"
)
// Service interface for Downstream
type Service interface {
GetServiceDocsList(ctx context.Context, req *GetServiceDocsListRequest) (*[]ServiceDoc, error)
}
// Client for Downstream API
type Client struct {
client *http.Client
url string
}
// NewClient for Downstream
func NewClient(client *http.Client, serviceURL string) *Client {
return &Client{client, serviceURL}
}
// GetServiceDocsList ...
func (s *Client) GetServiceDocsList(ctx context.Context, req *GetServiceDocsListRequest) (*[]ServiceDoc, error) {
required := []string{}
var okResponse []ServiceDoc
var errorResponse Status
u, err := url.Parse(fmt.Sprintf("%s/service-docs", s.url))
if err != nil {
return nil, common.CreateError(ctx, common.InternalError, "failed to parse url", err)
}
result, err := restlib.DoHTTPRequest(ctx, s.client, "GET", u.String(), nil, required, &okResponse, &errorResponse)
if err != nil {
response, ok := err.(*restlib.HTTPResult)
if !ok {
return nil, common.CreateError(ctx, common.DownstreamUnavailableError, "call failed: Downstream <- GET "+u.String(), err)
}
return nil, common.CreateDownstreamError(ctx, common.DownstreamResponseError, response.HTTPResponse, response.Body, &errorResponse)
}
if result.HTTPResponse.StatusCode == http.StatusUnauthorized {
return nil, common.CreateDownstreamError(ctx, common.DownstreamUnauthorizedError, result.HTTPResponse, result.Body, nil)
}
OkServiceDocResponse, ok := result.Response.(*[]ServiceDoc)
if ok {
valErr := validator.Validate(OkServiceDocResponse)
if valErr != nil {
return nil, common.CreateDownstreamError(ctx, common.DownstreamUnexpectedResponseError, result.HTTPResponse, result.Body, valErr)
}
return OkServiceDocResponse, nil
}
return nil, common.CreateDownstreamError(ctx, common.DownstreamUnexpectedResponseError, result.HTTPResponse, result.Body, nil)
}