-
Notifications
You must be signed in to change notification settings - Fork 5
/
tracking.go
43 lines (36 loc) · 922 Bytes
/
tracking.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
package usps
import (
"bytes"
"encoding/xml"
"fmt"
"strings"
)
type TrackResponse struct {
TrackInfo struct {
TrackSummary string `xml:"TrackSummary"`
} `xml:"TrackInfo"`
}
func (U *USPS) TrackPackage(trackingID string) TrackResponse {
result := TrackResponse{}
if U.Username == "" {
fmt.Println("Username is missing")
return result
}
var requestURL bytes.Buffer
requestURL.WriteString("TrackV2&XML=")
urlToEncode := "<TrackRequest USERID=\"" + U.Username + "\">"
urlToEncode += "<TrackID ID=\"" + trackingID + "\"></TrackID>"
urlToEncode += "</TrackRequest>"
requestURL.WriteString(URLEncode(urlToEncode))
body := U.GetRequest(requestURL.String())
if body == nil {
return result
}
bodyHeaderless := strings.Replace(string(body), xml.Header, "", 1)
err := xml.Unmarshal([]byte(bodyHeaderless), &result)
if err != nil {
fmt.Printf("error: %v", err)
return result
}
return result
}