-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
50 lines (43 loc) · 1.33 KB
/
request.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 trafikinfo
import "encoding/xml"
// Request tells the API what we're interested in
//
// It must include the Login information and at least
// one Query.
type Request struct {
XMLName string `xml:"REQUEST"`
Login *login `xml:"LOGIN"`
Queries []*Query `xml:"QUERY"`
}
// Query adds one or more queries to the request
func (r *Request) Query(queries ...*Query) *Request {
r.Queries = append(r.Queries, queries...)
return r
}
// APIKey sets the API key to use for this request
func (r *Request) APIKey(key string) *Request {
r.Login = &login{AuthenticationKey: key}
return r
}
// Build returns the XML encoded request as an array of bytes. It
// can be passed as http.NewRequest's body by wrapping it in a call
// to bytes.NewBuffer().
//
// The Build() method is final when used in a fluent API style,
// you can't chain additional methods on it that continue to modify
// the request.
func (r *Request) Build() ([]byte, error) {
return xml.Marshal(r)
}
// login holds the API authentication key
type login struct {
AuthenticationKey string `xml:"authenticationkey,attr"`
}
// NewRequest returns a Request using the specified API authentication key
// and the data to be retrieved and filtered by the specified queries. At
// least 1 query needs to be provided.
func NewRequest() *Request {
return &Request{
Queries: []*Query{},
}
}