Skip to content

Commit 614c50a

Browse files
committed
Overhaul pkg/cloudca/api
1 parent 39a852c commit 614c50a

5 files changed

Lines changed: 200 additions & 101 deletions

File tree

pkg/cloudca/api/api.go

Lines changed: 69 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,111 @@
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+
115
package api
216

317
import (
418
"bytes"
519
"crypto/tls"
20+
"fmt"
621
"io"
722
"net/http"
823
"net/url"
924
"strings"
1025
)
1126

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
1632
}
1733

18-
type CcaApiClient struct {
34+
// CcaClient for interacting with cloud.ca API
35+
type CcaClient struct {
1936
apiURL string
2037
apiKey string
2138
httpClient *http.Client
2239
}
2340

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+
}
2579

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{
2883
apiURL: apiURL,
2984
apiKey: apiKey,
3085
httpClient: &http.Client{},
3186
}
3287
}
3388

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 {
3591
tr := &http.Transport{
3692
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
3793
}
38-
return CcaApiClient{
94+
return CcaClient{
3995
apiURL: apiURL,
4096
apiKey: apiKey,
4197
httpClient: &http.Client{Transport: tr},
4298
}
4399
}
44100

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 {
47103
query := url.Values{}
48104
if options != nil {
49105
for k, v := range options {
50106
query.Add(k, v)
51107
}
52108
}
53-
u, _ := url.Parse(ccaClient.apiURL + "/" + strings.Trim(endpoint, "/") + "?" + query.Encode())
109+
u, _ := url.Parse(c.apiURL + "/" + strings.Trim(endpoint, "/") + "?" + query.Encode())
54110
return u.String()
55111
}
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-
}

pkg/cloudca/api/api_test.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
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+
115
package api
216

317
import (
@@ -29,18 +43,18 @@ func TestGetTaskReturnTaskIfSuccess(t *testing.T) {
2943
}
3044

3145
httpClient := &http.Client{Transport: transport}
32-
ccaClient := CcaApiClient{server.URL, "api-key", httpClient}
46+
ccaClient := CcaClient{server.URL, "api-key", httpClient}
3347

34-
expectedResp := CcaResponse{
35-
TaskId: "test_task_id",
48+
expectedResp := Response{
49+
TaskID: "test_task_id",
3650
TaskStatus: "test_task_status",
3751
Data: []byte(`{"key":"value"}`),
3852
MetaData: map[string]interface{}{"meta_key": "meta_value"},
3953
StatusCode: 200,
4054
}
4155

4256
//when
43-
resp, _ := ccaClient.Do(CcaRequest{Method: "GET", Endpoint: "/fooo"})
57+
resp, _ := ccaClient.Do(Request{Method: "GET", Endpoint: "/fooo"})
4458

4559
//then
4660
assert.Equal(t, expectedResp, *resp)
@@ -62,15 +76,15 @@ func TestGetTaskReturnErrorsIfErrorOccured(t *testing.T) {
6276
}
6377

6478
httpClient := &http.Client{Transport: transport}
65-
ccaClient := CcaApiClient{server.URL, "api-key", httpClient}
79+
ccaClient := CcaClient{server.URL, "api-key", httpClient}
6680

67-
expectedResp := CcaResponse{
68-
Errors: []CcaError{{ErrorCode: "FOO_ERROR", Message: "message1"}, {ErrorCode: "BAR_ERROR", Message: "message2"}},
81+
expectedResp := Response{
82+
Errors: []Error{{ErrorCode: "FOO_ERROR", Message: "message1"}, {ErrorCode: "BAR_ERROR", Message: "message2"}},
6983
StatusCode: 400,
7084
}
7185

7286
//when
73-
resp, _ := ccaClient.Do(CcaRequest{Method: "GET", Endpoint: "/fooo"})
87+
resp, _ := ccaClient.Do(Request{Method: "GET", Endpoint: "/fooo"})
7488

7589
//then
7690
assert.Equal(t, expectedResp, *resp)

pkg/cloudca/api/doc.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
15+
// Package api contains the implementation of API client
16+
package api

pkg/cloudca/api/request.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
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+
115
package api
216

3-
//HTTP Methods
17+
//nolint
418
const (
519
GET = "GET"
620
POST = "POST"
721
DELETE = "DELETE"
822
PUT = "PUT"
923
)
1024

11-
//A request object
12-
type CcaRequest struct {
25+
// Request a request object
26+
type Request struct {
1327
Method string
1428
Endpoint string
1529
Body []byte

0 commit comments

Comments
 (0)