-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merche.go
164 lines (137 loc) · 4.37 KB
/
merche.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package merche
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
const (
defaultBaseURL = "https://api.mercedes-benz.com/"
userAgent = "go-merche"
)
// A Client manages communication with the Mercedes API.
type Client struct {
client *http.Client
// Base URL for API requests. Defaults to the Mercedes API.
//BaseURL should always be specified with a trailing slash.
BaseURL *url.URL
// User agent used when communicating with the Mercedes API.
UserAgent string
common service // Reuse a single struct instead of allocating one for each service on the heap.
Resources *ResourcesService
VehicleStatus *VehicleStatusService
ElectricVehicleStatus *ElectricVehicleStatusService
VehicleLockStatus *VehicleLockStatusService
FuelStatus *FuelStatusService
PayAsYouDrive *PayAsYouDriveService
}
type service struct {
client *Client
}
// NewClient returns a new Mercedes API client. If a nil httpClient is
// provided, a new http.Client will be used. To use API methods which require
// authentication, provide an http.Client that will perform the authentication
// like golang.org/x/oauth2 library.
func NewClient(httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = &http.Client{}
}
baseURL, _ := url.Parse(defaultBaseURL)
c := &Client{
client: httpClient,
BaseURL: baseURL,
UserAgent: userAgent,
}
c.common.client = c
c.Resources = (*ResourcesService)(&c.common)
c.VehicleStatus = (*VehicleStatusService)(&c.common)
c.ElectricVehicleStatus = (*ElectricVehicleStatusService)(&c.common)
c.VehicleLockStatus = (*VehicleLockStatusService)(&c.common)
c.FuelStatus = (*FuelStatusService)(&c.common)
c.PayAsYouDrive = (*PayAsYouDriveService)(&c.common)
return c
}
// NewRequest creates a Mercedes API request. A path can be provided in path,
// in which case it is resolved relative to the BaseURL of the Client.
func (c *Client) NewRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error) {
if !strings.HasSuffix(c.BaseURL.Path, "/") {
return nil, fmt.Errorf("BaseURL must have a trailing slash, but %q does not", c.BaseURL)
}
req, err := http.NewRequestWithContext(ctx, method, c.BaseURL.String()+path, body)
if err != nil {
return nil, err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("Accept", "application/json")
if c.UserAgent != "" {
req.Header.Set("User-Agent", c.UserAgent)
}
return req, nil
}
// Do sends an API request and lets you handle the api response. If an error
// or API Error occurs, the error will contain more information. Otherwise you
// are supposed to read and close the response's Body.
func (c *Client) Do(req *http.Request, v interface{}) (*Response, error) {
resp, err := c.client.Do(req)
if err != nil {
return &Response{resp}, err
}
defer resp.Body.Close()
err = checkResponse(resp)
if err != nil {
return &Response{resp}, err
}
switch v := v.(type) {
case nil:
case io.Writer:
_, err = io.Copy(v, resp.Body)
default:
decErr := json.NewDecoder(resp.Body).Decode(v)
if decErr == io.EOF {
decErr = nil // ignore EOF errors caused by empty response body
}
if decErr != nil {
err = decErr
}
}
return &Response{resp}, err
}
func checkResponse(r *http.Response) error {
if code := r.StatusCode; http.StatusOK <= code && code <= 299 {
return nil
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return errors.New("check response: error reading response body")
}
if isExVeError(r.StatusCode) {
var exVeError ExVeError
json.Unmarshal(body, &exVeError)
return &exVeError
}
if r.StatusCode == http.StatusUnauthorized {
var authErr UnauthorizedError
json.Unmarshal(body, &authErr)
return &authErr
}
return &MercedesAPIError{r.StatusCode}
}
// Bool is a helper routine that allocates a new bool value
// to store v and returns a pointer to it.
func Bool(v bool) *bool { return &v }
// Int is a helper routine that allocates a new int value
// to store v and returns a pointer to it.
func Int(v int) *int { return &v }
// Int64 is a helper routine that allocates a new int64 value
// to store v and returns a pointer to it.
func Int64(v int64) *int64 { return &v }
// String is a helper routine that allocates a new string value
// to store v and returns a pointer to it.
func String(v string) *string { return &v }