-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
64 lines (53 loc) · 1.58 KB
/
client.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
package seafile
import (
"fmt"
"io"
"net/http"
"strings"
)
//Seafile客户端
type Client struct {
Addr string
authToken string
}
//新建一个Seafile客户端
//三种用法:
// cli := New(addr) //未认证的客户端,需要手动调用cli.Auth(user,pass)认证
// cli := New(addr, token) //预设置Token的客户端
// cli := New(addr, user, pass) //带用户信息的客户端,会自动调用Auth以获取Token(忽略错误)
func New(addr string, authParams ...string) *Client {
client := &Client{
Addr: strings.TrimSuffix(addr, "/"),
}
if len(authParams) == 1 {
client.authToken = authParams[0]
} else if len(authParams) == 2 {
err := client.Auth(authParams[0], authParams[1])
if err != nil {
fmt.Printf("用户认证失败: %s", err)
}
}
return client
}
//发起携带Token的Seafile WEB API请求
func (cli *Client) requestApi(apiPrefix, method, uri string, header http.Header, body io.Reader) (*http.Response, error) {
return cli.request(method, cli.Addr+apiPrefix+uri, header, body)
}
func (cli *Client) request(method, uri string, header http.Header, body io.Reader) (*http.Response, error) {
//检查Token是否为空
if cli.authToken == "" {
return nil, fmt.Errorf("没有合法的Token")
}
req, err := http.NewRequest(method, uri, body)
if err != nil {
return nil, fmt.Errorf("创建请求错误:%s", err)
}
req.Header.Set("Authorization", "Token "+cli.authToken)
//如果外部传入Header则设置之
for k, v := range header {
for _, vv := range v {
req.Header.Add(k, vv)
}
}
return http.DefaultClient.Do(req)
}