-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkey.go
More file actions
114 lines (103 loc) · 3.64 KB
/
key.go
File metadata and controls
114 lines (103 loc) · 3.64 KB
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
// 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 (
"fmt"
)
// CreateKey creates a new application key.
// See "b2_create_key" for an introduction:
// https://www.backblaze.com/b2/docs/b2_create_key.html
//
// Parameter capabilities, keyName are required.
// CreateKey return an ApplicationKey pointer and an error.
func (b *B2) CreateKey(capabilities []string, keyName string, validDurationInSeconds int64, bucketId string, namePrefix string) (*ApplicationKey, error) {
var (
url = fmt.Sprintf("%s/b2api/v1/b2_create_key", b.auth.ApiUrl)
requestBody = &struct {
AccountId string `json:"accountId"`
Capabilities []string `json:"capabilities"`
KeyName string `json:"keyName"`
ValidDurationInSeconds int64 `json:"validDurationInSeconds,omitempty"`
BucketId string `json:"bucketId,omitempty"`
NamePrefix string `json:"namePrefix,omitempty"`
}{b.auth.AccountId, capabilities, keyName, validDurationInSeconds, bucketId, namePrefix}
responseBody = &ApplicationKey{}
)
response, err := b.makeAuthedRequest(url, requestBody)
if err != nil {
return nil, err
}
switch {
case response.StatusCode == 200:
if err = unmarshalResponseBody(response, responseBody); err != nil {
return nil, err
}
return responseBody, nil
case response.StatusCode == 400 || response.StatusCode == 401:
return nil, handleErrorResponse(response)
default:
return nil, handleUnknownResponse(response)
}
}
// DeleteKey deletes the application key specified.
// See "b2_delete_key" for an introduction:
// https://www.backblaze.com/b2/docs/b2_delete_key.html
//
// Parameter key is required.
// DeleteKey return nil if delete successd or error if something error happened.
func (b *B2) DeleteKey(key *ApplicationKey) error {
var (
url = fmt.Sprintf("%s/b2api/v1/b2_delete_key", b.auth.ApiUrl)
requestBody = &struct {
Application string `json:"applicationKeyId"`
}{key.ApplicationKeyId}
responseBody = &ApplicationKey{}
)
response, err := b.makeAuthedRequest(url, requestBody)
if err != nil {
return err
}
switch {
case response.StatusCode == 200:
if err = unmarshalResponseBody(response, responseBody); err != nil {
return err
}
return nil
case response.StatusCode == 400 || response.StatusCode == 401:
return handleErrorResponse(response)
default:
return handleUnknownResponse(response)
}
}
// ListKeys list application keys associated with an account.
// See "b2_list_keys" for an introduction:
// https://www.backblaze.com/b2/docs/b2_list_keys.html
// All parameters are optional.
// ListKeys return an list of application pointer and an error.
func (b *B2) ListKeys(maxKeyCount int64, startApplicationKeyId string) (*ApplicationKeys, error) {
var (
url = fmt.Sprintf("%s/b2api/v1/b2_list_keys", b.auth.ApiUrl)
requestBody = &struct {
AccountId string `json:"accountId"`
MaxKeyCount int64 `json:"maxKeyCount,omitempty"`
StartApplicationKeyId string `json:"startApplicationKeyId,omitempty"`
}{b.auth.AccountId, maxKeyCount, startApplicationKeyId}
responseBody = &ApplicationKeys{}
)
response, err := b.makeAuthedRequest(url, requestBody)
if err != nil {
return nil, err
}
switch {
case response.StatusCode == 200:
if err = unmarshalResponseBody(response, responseBody); err != nil {
return nil, err
}
return responseBody, nil
case response.StatusCode == 400 || response.StatusCode == 401:
return nil, handleErrorResponse(response)
default:
return nil, handleUnknownResponse(response)
}
}