Skip to content

Commit 94d1ef9

Browse files
committed
Convert PriceZones to 1.3 API
1 parent edfdac4 commit 94d1ef9

3 files changed

Lines changed: 1553 additions & 214 deletions

File tree

upcloud/client/client.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,19 @@ import (
1414

1515
// Constants
1616
const (
17-
DEFAULT_API_VERSION = "1.2.3"
18-
DEFAULT_API_BASEURL = "https://api.upcloud.com"
17+
DefaultAPIVersion = "1.2.3"
18+
FutureAPIVersion = "1.3.4"
19+
DefaultAPIBaseURL = "https://api.upcloud.com"
1920

2021
// The default timeout (in seconds)
21-
DEFAULT_TIMEOUT = 10
22+
DefaultTimeout = 10
2223
)
2324

2425
// Client represents an API client
2526
type Client struct {
2627
userName string
2728
password string
2829
httpClient *http.Client
29-
30-
apiVersion string
31-
apiBaseURL string
3230
}
3331

3432
// New creates ands returns a new client configured with the specified user and password
@@ -45,10 +43,7 @@ func NewWithHTTPClient(userName string, password string, httpClient *http.Client
4543
client.userName = userName
4644
client.password = password
4745
client.httpClient = httpClient
48-
client.SetTimeout(time.Second * DEFAULT_TIMEOUT)
49-
50-
client.apiVersion = DEFAULT_API_VERSION
51-
client.apiBaseURL = DEFAULT_API_BASEURL
46+
client.SetTimeout(time.Second * DefaultTimeout)
5247

5348
return &client
5449
}
@@ -65,7 +60,13 @@ func (c *Client) GetTimeout() time.Duration {
6560

6661
// CreateRequestURL creates and returns a complete request URL for the specified API location
6762
func (c *Client) CreateRequestURL(location string) string {
68-
return fmt.Sprintf("%s%s", c.getBaseURL(), location)
63+
return fmt.Sprintf("%s%s", c.getBaseURL(false), location)
64+
}
65+
66+
// CreateFutureRequestURL creates and returns a complete request URL for the specified API location
67+
// using a newer API version
68+
func (c *Client) CreateFutureRequestURL(location string) string {
69+
return fmt.Sprintf("%s%s", c.getBaseURL(true), location)
6970
}
7071

7172
// PerformJSONGetRequest performs a GET request to the specified URL and returns the response body and eventual errors
@@ -147,10 +148,15 @@ func (c *Client) performJSONRequest(request *http.Request) ([]byte, error) {
147148
}
148149

149150
// Returns the base URL to use for API requests
150-
func (c *Client) getBaseURL() string {
151-
urlVersion, _ := semver.Make(c.apiVersion)
151+
func (c *Client) getBaseURL(future bool) string {
152+
var urlVersion semver.Version
153+
if !future {
154+
urlVersion, _ = semver.Make(DefaultAPIVersion)
155+
} else {
156+
urlVersion, _ = semver.Make(FutureAPIVersion)
157+
}
152158

153-
return fmt.Sprintf("%s/%d.%d", c.apiBaseURL, urlVersion.Major, urlVersion.Minor)
159+
return fmt.Sprintf("%s/%d.%d", DefaultAPIBaseURL, urlVersion.Major, urlVersion.Minor)
154160
}
155161

156162
// Parses the response and returns either the response body or an error

0 commit comments

Comments
 (0)