summaryrefslogtreecommitdiff
path: root/src/http.go
blob: 57e59392512731aa8180b8337701d2b312c1fc58 (plain)
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
package eduvpn

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
)

type HTTPResourceError struct {
	URL string
	Err error
}

func (e *HTTPResourceError) Error() string {
	return fmt.Sprintf("failed obtaining HTTP resource %s with error %v", e.URL, e.Err)
}

type HTTPStatusError struct {
	URL    string
	Status int
}

func (e *HTTPStatusError) Error() string {
	return fmt.Sprintf("failed obtaining HTTP resource %s as it gave an unsuccesful status code %d", e.URL, e.Status)
}

type HTTPReadError struct {
	URL string
	Err error
}

func (e *HTTPReadError) Error() string {
	return fmt.Sprintf("failed reading HTTP resource %s with error %v", e.URL, e.Err)
}

type HTTPParseJsonError struct {
	URL  string
	Body string
	Err  error
}

func (e *HTTPParseJsonError) Error() string {
	return fmt.Sprintf("failed parsing json %s for HTTP resource %s with error %v", e.Body, e.URL, e.Err)
}

type HTTPRequestCreateError struct {
	URL string
	Err error
}

func (e *HTTPRequestCreateError) Error() string {
	return fmt.Sprintf("failed to create HTTP request with url %s and error %v", e.URL, e.Err)
}

type HTTPOptionalParams struct {
	Headers *http.Header
}

func HTTPGet(url string) ([]byte, error) {
	return HTTPGetWithOptionalParams(url, nil)
}

func HTTPGetWithOptionalParams(url string, opts *HTTPOptionalParams) ([]byte, error) {
	client := &http.Client{}
	req, reqErr := http.NewRequest(http.MethodGet, url, nil)
	if reqErr != nil {
		return nil, &HTTPRequestCreateError{URL: url, Err: reqErr}
	}
	if opts != nil && opts.Headers != nil {
		for k, v := range *opts.Headers {
			req.Header.Add(k, v[0])
		}
	}
	resp, respErr := client.Do(req)

	if respErr != nil {
		return nil, &HTTPResourceError{URL: url, Err: respErr}
	}
	defer resp.Body.Close()

	body, readErr := ioutil.ReadAll(resp.Body)
	if readErr != nil {
		return nil, &HTTPReadError{URL: url, Err: readErr}
	}

	return body, nil
}

func HTTPPost(url string, body url.Values) ([]byte, error) {
	return HTTPPostWithOptionalParams(url, body, nil)
}

func HTTPPostWithOptionalParams(url string, data url.Values, opts *HTTPOptionalParams) ([]byte, error) {
	client := &http.Client{}
	req, reqErr := http.NewRequest(http.MethodPost, url, strings.NewReader(data.Encode()))
	if reqErr != nil {
		return nil, &HTTPRequestCreateError{URL: url, Err: reqErr}
	}
	if opts != nil && opts.Headers != nil {
		for k, v := range *opts.Headers {
			req.Header.Add(k, v[0])
		}
	}
	resp, respErr := client.Do(req)

	if respErr != nil {
		return nil, &HTTPResourceError{URL: url, Err: respErr}
	}
	defer resp.Body.Close()

	body, readErr := ioutil.ReadAll(resp.Body)
	if readErr != nil {
		return nil, &HTTPReadError{URL: url, Err: readErr}
	}

	return body, nil
}