forked from johnreutersward/postnord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostnord.go
155 lines (124 loc) · 3.62 KB
/
postnord.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
// Package postnord provides a client library to access postnords API.
package postnord
import (
"encoding/xml"
"fmt"
"net/http"
"net/url"
)
const (
libraryVersion = "0.1"
userAgent = "go-postnord/" + libraryVersion
baseURL = "http://logistics.postennorden.com/wsp/rest-services/ntt-service-rest/api/"
)
type Client struct {
// Locale may be set to en, sv, no, fi or da. English (en) is the default locale.
Locale string
ConsumerID string
UserAgent string
baseURL *url.URL
httpClient *http.Client
}
// NewClient returns a postnord api access client. If no http client is provided http.DefaultClient will be used.
func NewClient(ConsumerID string, httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}
baseURL, _ := url.Parse(baseURL)
c := &Client{
Locale: "en",
UserAgent: userAgent,
baseURL: baseURL,
ConsumerID: ConsumerID,
httpClient: httpClient,
}
return c
}
type shipmentResponse struct {
Shipments []Shipment `xml:"shipments>Shipment"`
}
type Shipment struct {
ShipmentId string `xml:"shipmentId"`
URI string `xml:"uri"`
AssertedNumitems int `xml:"assessedNumberOfItems"`
Service struct {
Code string `xml:"code"`
Name string `xml:"name"`
} `xml:"service"`
Consignee struct {
Address string `xml:"address"`
} `xml:"consignee"`
StatusText struct {
Header string `xml:"header"`
} `xml:"statusText"`
Items []struct {
ItemId string `xml:"itemId"`
DeliveryDate string `xml:"deliveryDate"`
NoItems int `xml:"noItems"`
Status string `xml:"status"`
StatusText struct {
Header string `xml:"header"`
Body string `xml:"body"`
} `xml:"statusText"`
TrackingEvents []struct {
EventTime string `xml:"eventTime"`
EventCode string `xml:"eventCode"`
EventDescription string `xml:"eventDescription"`
Location struct {
LocationId string `xml:"locationId"`
DisplayName string `xml:"displayName"`
Name string `xml:"name"`
LocationType string `xml:"locationType"`
} `xml:"location"`
} `xml:"events>TrackingEvent"`
References string `xml:"references"`
ItemRefIds string `xml:"itemRefIds"`
FreeTexts string `xml:"freeTexts"`
} `xml:"items>Item"`
Status string `xml:"status"`
AdditionalServices string `xml:"additionalServices"`
SplitStatuses string `xml:"splitStatuses"`
ShipmentReferences string `xml:"shipmentReferences"`
}
func (c *Client) Shipment(ID string) (*Shipment, error) {
endp := "shipment.xml?"
vals := url.Values{}
vals.Set("id", ID)
v := &shipmentResponse{}
_, err := c.get(endp, vals, v)
if err != nil {
return nil, err
}
if len(v.Shipments) == 0 {
return nil, fmt.Errorf("Shipment not found")
}
return &v.Shipments[0], nil
}
func (c *Client) get(endp string, vals url.Values, v interface{}) (*http.Response, error) {
vals.Set("consumerId", c.ConsumerID)
vals.Set("locale", c.Locale)
// can't use url.Values.Encode() because order is not guaranteed
qs := fmt.Sprintf("id=%s&locale=%s&consumerId=%s", vals.Get("id"), vals.Get("locale"), vals.Get("consumerId"))
ref, err := url.Parse(endp + qs)
if err != nil {
return nil, err
}
url := c.baseURL.ResolveReference(ref)
req, err := http.NewRequest("GET", url.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", c.UserAgent)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode > 299 {
return nil, fmt.Errorf("api error HTTP %d", resp.StatusCode)
}
defer resp.Body.Close()
if v != nil {
err = xml.NewDecoder(resp.Body).Decode(v)
}
return resp, err
}