-
Notifications
You must be signed in to change notification settings - Fork 23
/
metadata.go
65 lines (55 loc) · 1.54 KB
/
metadata.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
package rets
import (
"bytes"
"io"
"net/http"
"net/url"
"context"
)
// MetadataParams for the request
type MetadataParams struct {
Format, MType, ID string
}
// MetadataRequest ...
type MetadataRequest struct {
// RETS request options
URL, HTTPMethod string
HTTPFormEncodedValues bool // POST style http params
MetadataParams
}
// PrepMetadataRequest creates an http.Request from a MetadataRequest
func PrepMetadataRequest(r MetadataRequest) (*http.Request, error) {
url, err := url.Parse(r.URL)
if err != nil {
return nil, err
}
values := url.Query()
// required
values.Add("Format", r.Format)
values.Add("Type", r.MType)
values.Add("ID", r.ID)
method := "GET"
if r.HTTPMethod != "" {
method = r.HTTPMethod
}
// http POST style params
if r.HTTPFormEncodedValues {
req, err := http.NewRequest(method, url.String(), bytes.NewBufferString(values.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return req, err
}
url.RawQuery = values.Encode()
return http.NewRequest(method, url.String(), nil)
}
// MetadataResponse processes the request... TODO may no longer be necessary
func MetadataResponse(ctx context.Context, requester Requester, r MetadataRequest) (*http.Response, error) {
req, err := PrepMetadataRequest(r)
if err != nil {
return nil, err
}
return requester(ctx, req)
}
// MetadataStream encodes the http response stream for us
func MetadataStream(resp *http.Response, err error) (io.ReadCloser, error) {
return DefaultReEncodeReader(resp.Body, resp.Header.Get(ContentType)), nil
}