forked from gpmd/bigcommerce-api-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocations.go
134 lines (115 loc) · 3.68 KB
/
locations.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
package bigcommerce
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
)
type Location struct {
Code string `json:"code"`
Label string `json:"label"`
Description string `json:"description"`
ManagedByExternalSource bool `json:"managed_by_external_source"`
TypeId string `json:"type_id"`
Enabled bool `json:"enabled"`
OperatingHours LocationOpeningHours `json:"operating_hours"`
TimeZone string `json:"time_zone"`
Address LocationAddress `json:"address"`
StorefrontVisibility bool `json:"storefront_visibility"`
SpecialHours []LocationSpecialHours `json:"special_hours,omitempty"`
}
type LocationAddress struct {
Address1 string `json:"address1"`
Address2 string `json:"address2"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
Email string `json:"email"`
Phone string `json:"phone"`
GeoCoordinates Coordenates `json:"geo_coordinates"`
CountryCode string `json:"country_code"`
}
type Coordenates struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}
type LocationOpeningHours struct {
Sunday Weekday `json:"sunday"`
Monday Weekday `json:"monday"`
Tuesday Weekday `json:"tuesday"`
Wednesday Weekday `json:"wednesday"`
Thursday Weekday `json:"thursday"`
Friday Weekday `json:"friday"`
Saturday Weekday `json:"saturday"`
}
type LocationSpecialHours struct {
Label string `json:"label"`
Date string `json:"date"`
Open bool `json:"open"`
Opening string `json:"opening"`
Closing string `json:"closing"`
AllDay bool `json:"all_day"`
Annual bool `json:"annual"`
}
type Weekday struct {
Open bool `json:"open"`
Opening string `json:"opening"`
Closing string `json:"closing"`
}
// GetLocations returns all locations using filters.
// filters: request query parameters for BigCommerce locations endpoint, for example {"is_active": true}
func (bc *Client) GetLocations(filters map[string]string) ([]Location, error) {
var params []string
for k, v := range filters {
params = append(params, fmt.Sprintf("%s=%s", k, v))
}
url := "/v3/inventory/locations?" + strings.Join(params, "&")
req := bc.getAPIRequest(http.MethodGet, url, nil)
res, err := bc.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := processBody(res)
if err != nil {
if res.StatusCode == http.StatusNoContent {
return []Location{}, nil
}
return nil, err
}
var locations []Location
err = json.Unmarshal(body, &locations)
print(string(body))
if err != nil {
return nil, err
}
return locations, nil
}
// CreateLocations creates a new location based on the Location struct
func (bc *Client) CreateLocations(location *[]Location) error {
url := "/v3/inventory/locations"
reqJSON, _ := json.Marshal(location)
fmt.Println(string(reqJSON))
req := bc.getAPIRequest(http.MethodPost, url, bytes.NewReader(reqJSON))
res, err := bc.HTTPClient.Do(req)
if err != nil {
return err
}
if res.StatusCode != 200 {
return errors.New(res.Status)
}
return nil
}
// UpdateLocation alters the locations values
func (bc *Client) UpdateLocation(location *Location) error {
url := "/v3/inventory/locations"
reqJSON, _ := json.Marshal(location)
req := bc.getAPIRequest(http.MethodPut, url, bytes.NewReader(reqJSON))
_, err := bc.HTTPClient.Do(req)
if err != nil {
return err
}
return nil
}