This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
84 lines (70 loc) · 2.64 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
// Copyright (C) 2022 Alexander Sowitzki
//
// This program is free software: you can redistribute it and/or modify it under the terms of the
// GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License along with this program.
// If not, see <https://www.gnu.org/licenses/>.
package matrix
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
// HTTP performs a http exchange with the matrix server. It takes the http method, the path including the query
// and pointers to request and response payload as arguments. Request payload may be nil.
func (c Client) HTTP(ctx context.Context, method, path string, request, response interface{}) error {
return HTTP(ctx, c.homeserver, c.token, method, path, request, response)
}
func httpRequest(
ctx context.Context, homeserver, token, method, path string, request interface{},
) (*http.Response, error) {
requestBody := &bytes.Buffer{}
if request != nil {
if err := json.NewEncoder(requestBody).Encode(request); err != nil {
return nil, fmt.Errorf("marshal request: %w", err)
}
}
httpReq, err := http.NewRequestWithContext(ctx, method, homeserver+path, requestBody)
if err != nil {
panic(fmt.Sprintf("new http req: %v", err))
}
if token != "" {
httpReq.Header.Set("Authorization", "Bearer "+token)
}
return http.DefaultClient.Do(httpReq)
}
func httpResponse(httpResp *http.Response, response interface{}) (err error) {
defer func() {
cErr := httpResp.Body.Close()
if err == nil {
err = cErr
}
}()
if httpResp.StatusCode != http.StatusOK {
return fmt.Errorf("status code: %v", httpResp.Status)
}
return json.NewDecoder(httpResp.Body).Decode(response)
}
// HTTP performs a http exchangewith a matrix server. It takes the homeserver url, the client token, the http method,
// the path including the query and pointers to request and response payload as arguments. Request payload may be nil.
// Token may be empty to send an unauthenticated request.
func HTTP(
ctx context.Context, homeserver, token, method, path string, request, response interface{},
) error {
httpResp, err := httpRequest(ctx, homeserver, token, method, path, request)
if err != nil {
return err
}
if err := httpResponse(httpResp, response); err != nil {
return err
}
return nil
}