transport.go 3.38 KB
Newer Older
1
package irmago
2
3
4
5

import (
	"bytes"
	"encoding/json"
6
	"fmt"
7
8
9
10
11
12
13
	"io"
	"io/ioutil"
	"net/http"
	"strings"
	"time"
)

Sietse Ringers's avatar
Sietse Ringers committed
14
// HTTPTransport sends and receives JSON messages to a HTTP server.
15
type HTTPTransport struct {
Sietse Ringers's avatar
Sietse Ringers committed
16
17
18
	Server  string
	client  *http.Client
	headers map[string]string
19
20
}

Sietse Ringers's avatar
Sietse Ringers committed
21
22
const verbose = false

Sietse Ringers's avatar
Sietse Ringers committed
23
// NewHTTPTransport returns a new HTTPTransport.
24
25
26
27
28
29
func NewHTTPTransport(serverURL string) *HTTPTransport {
	url := serverURL
	if !strings.HasSuffix(url, "/") {
		url += "/"
	}
	return &HTTPTransport{
Sietse Ringers's avatar
Sietse Ringers committed
30
31
		Server:  url,
		headers: map[string]string{},
32
33
34
35
36
37
		client: &http.Client{
			Timeout: time.Second * 5,
		},
	}
}

38
// SetHeader sets a header to be sent in requests.
Sietse Ringers's avatar
Sietse Ringers committed
39
40
41
42
func (transport *HTTPTransport) SetHeader(name, val string) {
	transport.headers[name] = val
}

Sietse Ringers's avatar
Sietse Ringers committed
43
func (transport *HTTPTransport) request(url string, method string, result interface{}, object interface{}) error {
44
45
46
47
48
49
50
	if method != http.MethodPost && method != http.MethodGet {
		panic("Unsupported HTTP method " + method)
	}
	if method == http.MethodGet && object != nil {
		panic("Cannot GET and also post an object")
	}

51
	var isstr bool
52
53
	var reader io.Reader
	if object != nil {
54
55
		var objstr string
		if objstr, isstr = object.(string); isstr {
Sietse Ringers's avatar
Sietse Ringers committed
56
57
58
			if verbose {
				fmt.Printf("GET %s\n", url)
			}
59
60
61
62
			reader = bytes.NewBuffer([]byte(objstr))
		} else {
			marshaled, err := json.Marshal(object)
			if err != nil {
63
				return &Error{Err: err, ErrorCode: ErrorSerialization}
Sietse Ringers's avatar
Sietse Ringers committed
64
				//return &TransportError{Err: err.Error()}
65
			}
Sietse Ringers's avatar
Sietse Ringers committed
66
67
68
			if verbose {
				fmt.Printf("POST %s: %s\n", url, string(marshaled))
			}
69
			reader = bytes.NewBuffer(marshaled)
70
71
72
73
74
		}
	}

	req, err := http.NewRequest(method, transport.Server+url, reader)
	if err != nil {
75
		return &Error{Err: err, ErrorCode: ErrorTransport}
76
77
78
79
	}

	req.Header.Set("User-Agent", "irmago")
	if object != nil {
80
81
82
83
84
		if isstr {
			req.Header.Set("Content-Type", "text/plain; charset=UTF-8")
		} else {
			req.Header.Set("Content-Type", "application/json; charset=UTF-8")
		}
85
	}
Sietse Ringers's avatar
Sietse Ringers committed
86
87
88
	for name, val := range transport.headers {
		req.Header.Set(name, val)
	}
89
90
91

	res, err := transport.client.Do(req)
	if err != nil {
92
		return &Error{Err: err, ErrorCode: ErrorTransport}
93
94
95
96
	}

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
97
		return &Error{Err: err, Status: res.StatusCode}
98
99
100
101
102
	}
	if res.StatusCode != 200 {
		apierr := &ApiError{}
		json.Unmarshal(body, apierr)
		if apierr.ErrorName == "" { // Not an ApiErrorMessage
Sietse Ringers's avatar
Sietse Ringers committed
103
			return &Error{ErrorCode: ErrorTransport, Status: res.StatusCode}
104
		}
Sietse Ringers's avatar
Sietse Ringers committed
105
106
107
		if verbose {
			fmt.Printf("ERROR: %+v\n", apierr)
		}
Sietse Ringers's avatar
Sietse Ringers committed
108
		return &Error{ErrorCode: ErrorTransport, Status: res.StatusCode, ApiError: apierr}
109
110
	}

Sietse Ringers's avatar
Sietse Ringers committed
111
112
113
	if verbose {
		fmt.Printf("RESPONSE: %s\n", string(body))
	}
114
115
116
117
118
119
120
	if _, resultstr := result.(*string); resultstr {
		*result.(*string) = string(body)
	} else {
		err = json.Unmarshal(body, result)
		if err != nil {
			return &Error{Err: err, Status: res.StatusCode}
		}
121
122
123
124
125
	}

	return nil
}

Sietse Ringers's avatar
Sietse Ringers committed
126
// Post sends the object to the server and parses its response into result.
Sietse Ringers's avatar
Sietse Ringers committed
127
func (transport *HTTPTransport) Post(url string, result interface{}, object interface{}) error {
128
129
130
	return transport.request(url, http.MethodPost, result, object)
}

Sietse Ringers's avatar
Sietse Ringers committed
131
// Get performs a GET request and parses the server's response into result.
Sietse Ringers's avatar
Sietse Ringers committed
132
func (transport *HTTPTransport) Get(url string, result interface{}) error {
133
134
135
	return transport.request(url, http.MethodGet, result, nil)
}

Sietse Ringers's avatar
Sietse Ringers committed
136
// Delete performs a DELETE.
137
138
139
func (transport *HTTPTransport) Delete(url string) {
	// TODO
}