-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
48 lines (40 loc) · 1.14 KB
/
auth.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
// Copyright 2018 hryyan. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package b2
import (
"net/http"
)
const AUTH_URL = "https://api.backblazeb2.com/b2api/v1/b2_authorize_account"
type AuthResponse struct {
AccountId string `json:"accountId"`
AuthorizationToken string `json:"authorizationToken"`
ApiUrl string `json:"apiUrl"`
DownloadUrl string `json:"downloadUrl"`
RecommendedPartSize int64 `json:"recommendedPartSize"`
AbsoluteMinimumPartSize int64 `json:"absoluteMinimumPartSize"`
}
// Auth your account
func (b *B2) Auth() error {
request, err := http.NewRequest("GET", AUTH_URL, nil)
if err != nil {
return err
}
request.SetBasicAuth(b.AccountId, b.ApplicationKey)
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return err
}
switch response.StatusCode {
case 200:
if err = unmarshalResponseBody(response, &b.auth); err != nil {
return err
}
return nil
case 401:
return handleErrorResponse(response)
default:
return handleUnknownResponse(response)
}
}