-
Notifications
You must be signed in to change notification settings - Fork 1
/
order.go
119 lines (99 loc) · 3.17 KB
/
order.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
package gbdx
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// Order holds the ID and all the associated Acquisitions of a submitted order.
type Order struct {
ID string `json:"order_id"`
Acquisitions []Acquisition `json:"acquisitions"`
}
// Acquisition holds the ID, ordering state, and location (an s3 path) of an order.
type Acquisition struct {
ID string `json:"acquisition_id"`
State string `json:"state"`
Location string `json:"location"`
}
// OrderStatus returns the status of the order given the string identifying it.
func (a *Api) OrderStatus(orderID string) (*Order, error) {
url := fmt.Sprintf("%s%s", endpoints.orders, orderID)
resp, err := a.client.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Order status check returned a bad status code: %s", resp.Status)
}
var order *Order
err = json.NewDecoder(resp.Body).Decode(&order)
if err != nil {
return nil, fmt.Errorf("Order status failed to decode properly: %v", err)
}
return order, err
}
// NewOrder submits a new order.
func (a *Api) NewOrder(IDs ...string) (*Order, error) {
jsonBody, err := json.Marshal(IDs)
if err != nil {
return nil, fmt.Errorf("NewOrder failed to marshall input IDs: %v", err)
}
resp, err := a.client.Post(endpoints.orders, "application/json", bytes.NewReader(jsonBody))
if err != nil {
return nil, fmt.Errorf("NewOrder failed to post: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("NewOrder post returned a bad status code: %s %v", resp.Status, err)
}
var order *Order
err = json.NewDecoder(resp.Body).Decode(&order)
if err != nil {
return nil, fmt.Errorf("NewOrder response failed to decode properly: %v", err)
}
return order, err
}
// OrderLocation returns the location of orders in AWS.
func (a *Api) OrderLocation(IDs ...string) (*Order, error) {
u, err := url.Parse(endpoints.ordersLocation)
if err != nil {
return nil, fmt.Errorf("OrderLocation failed parsing the url: %v", err)
}
idBytes, err := json.Marshal(IDs)
if err != nil {
return nil, fmt.Errorf("NewOrder failed to marshall input IDs: %v", err)
}
q := u.Query()
q.Set("acquisitionIds", string(idBytes))
u.RawQuery = q.Encode()
resp, err := a.client.Get(u.String())
if err != nil {
return nil, fmt.Errorf("OrderLocation failed to get: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("OrderLocation get returned a bad status code: %s", resp.Status)
}
var order *Order
err = json.NewDecoder(resp.Body).Decode(&order)
if err != nil {
return nil, fmt.Errorf("OrderLocation response failed to decode properly: %v", err)
}
return order, err
}
// OrderingHeartbeat checks if the ordering endpoint is alive and well.
func OrderingHeartbeat() error {
url := endpoints.ordersHeartbeat
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("ordering heartbeat failed %s: %v", resp.Status, err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("ordering heartbeat returned a bad status code: %s", resp.Status)
}
defer resp.Body.Close()
return err
}