forked from vseinstrumentiru/CDEK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpvzlist-service.go
executable file
·50 lines (39 loc) · 935 Bytes
/
pvzlist-service.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
package cdek
import (
"encoding/xml"
"io/ioutil"
"net/http"
"net/url"
"path"
)
const (
pvzListURL = "pvzlist/v1/xml"
)
//GetPvzList The method is used to load the list of active pickup points, from which the client can pick up its order.
func (c Client) GetPvzList(filter map[PvzListFilter]string) ([]*Pvz, error) {
serverURL, err := url.Parse(c.apiURL)
if err != nil {
return nil, err
}
serverURL.Path = path.Join(serverURL.Path, pvzListURL)
queryString := serverURL.Query()
for filterKey, value := range filter {
queryString.Set(string(filterKey), value)
}
serverURL.RawQuery = queryString.Encode()
reqURL := serverURL.String()
resp, err := http.Get(reqURL)
if err != nil {
return nil, err
}
defer func() {
_ = resp.Body.Close()
}()
body, _ := ioutil.ReadAll(resp.Body)
var pvzList pvzList
err = xml.Unmarshal(body, &pvzList)
if err != nil {
return nil, err
}
return pvzList.Pvz, nil
}