-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
50 lines (42 loc) · 941 Bytes
/
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
package tvdb
import (
"context"
"github.com/dashotv/tvdb/openapi"
"github.com/dashotv/tvdb/openapi/models/operations"
"github.com/dashotv/tvdb/openapi/models/shared"
)
type Client struct {
Key string
Token string
sdk *openapi.SDK
ctx context.Context
}
// Login creates a new client by logging in with an apikey
func Login(apikey string) (*Client, error) {
s := openapi.New()
c := &Client{
Key: apikey,
Token: "",
sdk: s,
ctx: context.Background(),
}
p := operations.PostLoginRequestBody{
Apikey: c.Key,
}
r, err := c.PostLogin(p)
if err != nil {
return nil, err
}
c.Token = *r.Data.Token
return New(apikey, *r.Data.Token), nil
}
// New creates a client with an existing token
func New(apikey, token string) *Client {
s := openapi.New(openapi.WithSecurity(shared.Security{BearerAuth: token}))
return &Client{
Key: apikey,
Token: token,
sdk: s,
ctx: context.Background(),
}
}