|
| 1 | +// Copyright © 2019 cloud.ca Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
1 | 15 | package api |
2 | 16 |
|
3 | 17 | import ( |
4 | 18 | "bytes" |
5 | 19 | "crypto/tls" |
| 20 | + "fmt" |
6 | 21 | "io" |
7 | 22 | "net/http" |
8 | 23 | "net/url" |
9 | 24 | "strings" |
10 | 25 | ) |
11 | 26 |
|
12 | | -type ApiClient interface { |
13 | | - Do(request CcaRequest) (*CcaResponse, error) |
14 | | - GetApiURL() string |
15 | | - GetApiKey() string |
| 27 | +// Client Represent the Client interface for interacting with cloud.ca API |
| 28 | +type Client interface { |
| 29 | + Do(request Request) (*Response, error) |
| 30 | + GetAPIURL() string |
| 31 | + GetAPIKey() string |
16 | 32 | } |
17 | 33 |
|
18 | | -type CcaApiClient struct { |
| 34 | +// CcaClient for interacting with cloud.ca API |
| 35 | +type CcaClient struct { |
19 | 36 | apiURL string |
20 | 37 | apiKey string |
21 | 38 | httpClient *http.Client |
22 | 39 | } |
23 | 40 |
|
24 | | -const API_KEY_HEADER = "MC-Api-Key" |
| 41 | +// Do Execute the API call to server and returns a Response. cloud.ca errors will |
| 42 | +// be returned in the Response body, not in the error return value. The error |
| 43 | +// return value is reserved for unexpected errors. |
| 44 | +func (c CcaClient) Do(request Request) (*Response, error) { |
| 45 | + var bodyBuffer io.Reader |
| 46 | + if request.Body != nil { |
| 47 | + bodyBuffer = bytes.NewBuffer(request.Body) |
| 48 | + } |
| 49 | + method := request.Method |
| 50 | + if method == "" { |
| 51 | + method = "GET" |
| 52 | + } |
| 53 | + req, err := http.NewRequest(request.Method, c.buildURL(request.Endpoint, request.Options), bodyBuffer) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + req.Header.Add("MC-Api-Key", c.apiKey) |
| 58 | + req.Header.Add("Content-Type", "application/json") |
| 59 | + resp, err := c.httpClient.Do(req) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + defer func() { |
| 64 | + err := resp.Body.Close() |
| 65 | + fmt.Printf("%s", err) |
| 66 | + }() |
| 67 | + return NewResponse(resp) |
| 68 | +} |
| 69 | + |
| 70 | +// GetAPIKey Return the API key being used by API client |
| 71 | +func (c CcaClient) GetAPIKey() string { |
| 72 | + return c.apiKey |
| 73 | +} |
| 74 | + |
| 75 | +// GetAPIURL Return the API URL being used by API client |
| 76 | +func (c CcaClient) GetAPIURL() string { |
| 77 | + return c.apiURL |
| 78 | +} |
25 | 79 |
|
26 | | -func NewApiClient(apiURL, apiKey string) ApiClient { |
27 | | - return CcaApiClient{ |
| 80 | +// NewClient Create a new Client with provided API URL and key |
| 81 | +func NewClient(apiURL, apiKey string) Client { |
| 82 | + return CcaClient{ |
28 | 83 | apiURL: apiURL, |
29 | 84 | apiKey: apiKey, |
30 | 85 | httpClient: &http.Client{}, |
31 | 86 | } |
32 | 87 | } |
33 | 88 |
|
34 | | -func NewInsecureApiClient(apiURL, apiKey string) ApiClient { |
| 89 | +// NewInsecureClient Create a new Client with provided API URL and key that accepts insecure connections |
| 90 | +func NewInsecureClient(apiURL, apiKey string) Client { |
35 | 91 | tr := &http.Transport{ |
36 | 92 | TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
37 | 93 | } |
38 | | - return CcaApiClient{ |
| 94 | + return CcaClient{ |
39 | 95 | apiURL: apiURL, |
40 | 96 | apiKey: apiKey, |
41 | 97 | httpClient: &http.Client{Transport: tr}, |
42 | 98 | } |
43 | 99 | } |
44 | 100 |
|
45 | | -//Build a URL by using endpoint and options. Options will be set as query parameters. |
46 | | -func (ccaClient CcaApiClient) buildUrl(endpoint string, options map[string]string) string { |
| 101 | +// buildURL Builds a URL by using endpoint and options. Options will be set as query parameters. |
| 102 | +func (c CcaClient) buildURL(endpoint string, options map[string]string) string { |
47 | 103 | query := url.Values{} |
48 | 104 | if options != nil { |
49 | 105 | for k, v := range options { |
50 | 106 | query.Add(k, v) |
51 | 107 | } |
52 | 108 | } |
53 | | - u, _ := url.Parse(ccaClient.apiURL + "/" + strings.Trim(endpoint, "/") + "?" + query.Encode()) |
| 109 | + u, _ := url.Parse(c.apiURL + "/" + strings.Trim(endpoint, "/") + "?" + query.Encode()) |
54 | 110 | return u.String() |
55 | 111 | } |
56 | | - |
57 | | -//Does the API call to server and returns a CCAResponse. Cloud.ca errors will be returned in the |
58 | | -//CCAResponse body, not in the error return value. The error return value is reserved for unexpected errors. |
59 | | -func (ccaClient CcaApiClient) Do(request CcaRequest) (*CcaResponse, error) { |
60 | | - var bodyBuffer io.Reader |
61 | | - if request.Body != nil { |
62 | | - bodyBuffer = bytes.NewBuffer(request.Body) |
63 | | - } |
64 | | - method := request.Method |
65 | | - if method == "" { |
66 | | - method = "GET" |
67 | | - } |
68 | | - req, err := http.NewRequest(request.Method, ccaClient.buildUrl(request.Endpoint, request.Options), bodyBuffer) |
69 | | - if err != nil { |
70 | | - return nil, err |
71 | | - } |
72 | | - req.Header.Add(API_KEY_HEADER, ccaClient.apiKey) |
73 | | - req.Header.Add("Content-Type", "application/json") |
74 | | - resp, err := ccaClient.httpClient.Do(req) |
75 | | - if err != nil { |
76 | | - return nil, err |
77 | | - } |
78 | | - defer resp.Body.Close() |
79 | | - return NewCcaResponse(resp) |
80 | | -} |
81 | | - |
82 | | -func (ccaClient CcaApiClient) GetApiKey() string { |
83 | | - return ccaClient.apiKey |
84 | | -} |
85 | | - |
86 | | -func (ccaClient CcaApiClient) GetApiURL() string { |
87 | | - return ccaClient.apiURL |
88 | | -} |
|
0 commit comments