-
Notifications
You must be signed in to change notification settings - Fork 7
/
location.go
82 lines (70 loc) · 2.36 KB
/
location.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
package gsclient
import (
"context"
"errors"
"net/http"
"path"
)
//LocationOperator an interface defining API of a location operator
type LocationOperator interface {
GetLocationList(ctx context.Context) ([]Location, error)
GetLocation(ctx context.Context, id string) (Location, error)
}
//LocationList JSON struct of a list of locations
type LocationList struct {
//Array of locations
List map[string]LocationProperties `json:"locations"`
}
//Location JSON struct of a single location
type Location struct {
//Properties of a location
Properties LocationProperties `json:"location"`
}
//LocationProperties JSON struct of properties of a location
type LocationProperties struct {
//Uses IATA airport code, which works as a location identifier.
Iata string `json:"iata"`
//Status indicates the status of the object.
Status string `json:"status"`
//List of labels.
Labels []string `json:"labels"`
//The human-readable name of the location. It supports the full UTF-8 charset, with a maximum of 64 characters.
Name string `json:"name"`
//The UUID of an object is always unique, and refers to a specific object.
ObjectUUID string `json:"object_uuid"`
//The human-readable name of the location. It supports the full UTF-8 charset, with a maximum of 64 characters.
Country string `json:"country"`
}
//GetLocationList gets a list of available locations]
//
//See: https://gridscale.io/en//api-documentation/index.html#operation/getLocations
func (c *Client) GetLocationList(ctx context.Context) ([]Location, error) {
r := gsRequest{
uri: apiLocationBase,
method: http.MethodGet,
skipCheckingRequest: true,
}
var response LocationList
var locations []Location
err := r.execute(ctx, *c, &response)
for _, properties := range response.List {
locations = append(locations, Location{Properties: properties})
}
return locations, err
}
//GetLocation gets a specific location
//
//See: https://gridscale.io/en//api-documentation/index.html#operation/getLocation
func (c *Client) GetLocation(ctx context.Context, id string) (Location, error) {
if !isValidUUID(id) {
return Location{}, errors.New("'id' is invalid")
}
r := gsRequest{
uri: path.Join(apiLocationBase, id),
method: http.MethodGet,
skipCheckingRequest: true,
}
var location Location
err := r.execute(ctx, *c, &location)
return location, err
}