This repository was archived by the owner on Aug 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
120 lines (111 loc) · 3.55 KB
/
http.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
package dealmap
import (
"encoding/xml"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
"io/ioutil"
)
// Dealmap API information
const (
ApiUrl = "http://api.thedealmap.com"
)
// DealMap class to used to communicate with the API
type DealMap struct {
baseUrl string // The base URL to access the API
httpClient *http.Client // The HTTP client to use
key string // The developer key provided by TheDealMap
}
// Creates a new DealMap object using the given HTTP client and API key
func New(apiUrl string, client *http.Client, apiKey string) *DealMap {
return &DealMap{apiUrl, client, apiKey}
}
// buildQuery returns a map to use as the query string in requests to the API.
// The given parameters are included if they are non-empty.
func (dm *DealMap) buildQuery(location string, query string, distanceMi int, startIndex int, pageSize int, activity int, capability int, expiration time.Time) url.Values {
args := make(map[string][]string)
if location != "" {
args["l"] = []string{location}
}
if query != "" && query != "*" {
args["q"] = []string{query}
}
if distanceMi > 0 {
args["d"] = []string{strconv.Itoa(distanceMi)}
}
if startIndex >= 0 {
args["si"] = []string{strconv.Itoa(startIndex)}
}
if pageSize > 0 {
args["ps"] = []string{strconv.Itoa(pageSize)}
}
if activity > 0 {
args["a"] = []string{strconv.Itoa(activity)}
}
if capability > 0 {
args["c"] = []string{strconv.Itoa(capability)}
}
if expiration.Unix() > 0 {
args["ed"] = []string{expiration.Format("2006-01-02")}
}
args["key"] = []string{dm.key}
return args
}
// SearchDeals invokes the "search deals" API from TheDealMap and returns the response as a Deals object.
func (dm *DealMap) SearchDeals(location string, query string, distanceMi int, startIndex int, pageSize int, activity int, capability int, expirationDate time.Time) (*Deals, error) {
args := dm.buildQuery(location, query, distanceMi, startIndex, pageSize, activity, capability, expirationDate)
resp, err := dm.httpClient.Get(fmt.Sprintf("%s/search/deals/?%s", dm.baseUrl, args.Encode()))
defer resp.Body.Close()
if err != nil {
return nil, err
}
var result Deals
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = xml.Unmarshal(data, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// SearchBusinesses invokes the "search businesses" API from TheDealMap and returns the response as a Businesses object.
func (dm *DealMap) SearchBusinesses(location string, query string, distanceMi int, startIndex int, pageSize int, activity int) (*Businesses, error) {
args := dm.buildQuery(location, query, distanceMi, startIndex, pageSize, activity, 0, time.Unix(0, 0))
resp, err := dm.httpClient.Get(fmt.Sprintf("%s/search/businesses/?%s", dm.baseUrl, args.Encode()))
if err != nil {
return nil, err
}
var result Businesses
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = xml.Unmarshal(data, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// DealDetails invokes the "deal details" API from TheDeapMap and returns the response as a Deal object.
func (dm *DealMap) DealDetails(id string) (*Deal, error) {
args := dm.buildQuery("", "", 0, 0, 0, 0, 0, time.Unix(0, 0))
resp, err := dm.httpClient.Get(fmt.Sprintf("%s/deals/%s/?%s", dm.baseUrl, id, args.Encode()))
defer resp.Body.Close()
if err != nil {
return nil, err
}
var result Deal
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = xml.Unmarshal(data, &result)
if err != nil {
return nil, err
}
return &result, nil
}