-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
187 lines (149 loc) · 4.76 KB
/
api.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
Package go_hidrive is a simple client SDK library for HiDrive cloud storage
(mainly provided by [Strato](https://www.strato.de/cloud-speicher/) provider)
Currently, the following implementation are available: [Dir], [File] and [Share].
All methods accept url.Values as a set of request parameters.
You can also use [Parameters] objects to simplify parameters gathering required for request.
Example reading file from HiDrive:
package main
import (
"log"
"context"
"io"
"fmt"
"golang.org/x/oauth2"
hidrive "github.com/Burmuley/go-hidrive"
)
func main() {
oauth2config := oauth2.Config{
ClientID: "hi_drive_client_id",
ClientSecret: "hi_drive_client_secret",
Endpoint: oauth2.Endpoint{
AuthURL: hidrive.StratoHiDriveAuthURL,
TokenURL: hidrive.StratoHiDriveTokenURL,
AuthStyle: 0,
},
Scopes: []string{"user", "rw"},
}
token := &oauth2.Token{
RefreshToken: "hi_drive_oauth2_refresh_token",
}
client := oauth2config.Client(context.Background(), token)
fileApi := hidrive.NewFile(client, hidrive.StratoHiDriveAPIV21)
rdr, err := fileApi.Get(context.Background(), hidrive.NewParameters().SetPath("/public/test_file.txt").Values)
if err != nil {
log.Fatal(err)
}
contents, err := io.ReadAll(rdr)
if err != nil {
log.Fatal(err)
}
fmt.Println(contents)
}
*/
package go_hidrive
import (
"context"
"encoding/json"
"io"
"net/http"
"net/url"
"strings"
)
const (
StratoHiDriveAPIV21 = "https://api.hidrive.strato.com/2.1" // Default HiDrive API endpoint
StratoHiDriveAuthURL = "https://my.hidrive.com/client/authorize" // Default HiDrive authentication URL
StratoHiDriveTokenURL = "https://my.hidrive.com/oauth2/token" // Default HiDrive token operations URL
)
/*
Api - basic structure defining common logic for API interaction.
Property `HTTPClient` should be a [http.Client] type and retrieved from `oauth2` package,
i.e. it should be pre-configured to perform OAuth2 authentication against HiDrive API before
underlying method send any data.
Property `APIEndpoint` should be set to proper HiDrive API endpoint.
Use [NewApi] function to create new instances of this type, it supports empty `endpoint` and
injects default from [StratoHiDriveAPIV21] constant.
*/
type Api struct {
APIEndpoint string
HTTPClient *http.Client
}
func NewApi(client *http.Client, endpoint string) Api {
if endpoint == "" {
endpoint = StratoHiDriveAPIV21
}
return Api{
APIEndpoint: endpoint,
HTTPClient: client,
}
}
func (a Api) newHTTPRequest(ctx context.Context, method, uri string, r io.Reader) (*http.Request, error) {
return http.NewRequestWithContext(ctx, method, strings.Join([]string{a.APIEndpoint, uri}, "/"), r)
}
func (a Api) doGET(ctx context.Context, uri string, params url.Values, okCodes []int) (*http.Response, error) {
return a.doHTTPRequest(ctx, "GET", uri, params, okCodes, nil)
}
func (a Api) doDELETE(ctx context.Context, uri string, params url.Values, okCodes []int) (*http.Response, error) {
return a.doHTTPRequest(ctx, "DELETE", uri, params, okCodes, nil)
}
func (a Api) doPOST(ctx context.Context, uri string, params url.Values, okCodes []int, body io.ReadCloser) (*http.Response, error) {
return a.doHTTPRequest(ctx, "POST", uri, params, okCodes, body)
}
func (a Api) doPUT(ctx context.Context, uri string, params url.Values, okCodes []int, body io.ReadCloser) (*http.Response, error) {
return a.doHTTPRequest(ctx, "PUT", uri, params, okCodes, body)
}
func (a Api) doPATCH(ctx context.Context, uri string, params url.Values, okCodes []int, body io.ReadCloser) (*http.Response, error) {
return a.doHTTPRequest(ctx, "PATCH", uri, params, okCodes, body)
}
func (a Api) doHTTPRequest(ctx context.Context, method, uri string, params url.Values, okCodes []int, body io.ReadCloser) (*http.Response, error) {
var (
req *http.Request
res *http.Response
)
{
var err error
if req, err = a.newHTTPRequest(ctx, method, uri, body); err != nil {
return nil, err
}
}
req.URL.RawQuery = params.Encode()
{
var err error
if res, err = a.HTTPClient.Do(req); err != nil {
return nil, err
}
}
{
var err error
if err = a.checkHTTPStatusError(okCodes, res); err != nil {
return nil, err
}
}
return res, nil
}
func (a Api) checkHTTPStatusError(okCodes []int, res *http.Response) error {
var err error
var body []byte
if !isItemInSlice(okCodes, res.StatusCode) {
hdErr := &Error{}
if body, err = io.ReadAll(res.Body); err != nil {
return err
}
if err := json.Unmarshal(body, hdErr); err != nil {
return err
}
return hdErr
}
return nil
}
func (a Api) unmarshalBody(res *http.Response, obj any) error {
var body []byte
var err error
if body, err = io.ReadAll(res.Body); err != nil {
return err
}
if err := json.Unmarshal(body, obj); err != nil {
return err
}
return nil
}