|
1 | 1 | package turso |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
4 | 6 | "fmt" |
5 | 7 | "net/http" |
6 | 8 | ) |
7 | 9 |
|
8 | 10 | type ApiToken struct { |
9 | | - ID string `json:"dbId"` |
10 | | - Name string |
11 | | - Owner uint |
12 | | - PubKey []byte |
| 11 | + ID string `json:"id"` |
| 12 | + Name string `json:"name"` |
| 13 | + Organization string `json:"organization,omitempty"` |
| 14 | + CreatedAt string `json:"created_at"` |
| 15 | + Owner uint `json:"-"` |
| 16 | + PubKey []byte `json:"-"` |
13 | 17 | } |
14 | 18 |
|
15 | 19 | type ApiTokensClient client |
@@ -39,9 +43,31 @@ type CreateApiToken struct { |
39 | 43 | } |
40 | 44 |
|
41 | 45 | func (a *ApiTokensClient) Create(name string) (CreateApiToken, error) { |
| 46 | + return a.CreateWithOrg(name, "") |
| 47 | +} |
| 48 | + |
| 49 | +func (a *ApiTokensClient) CreateWithOrg(name string, organization string) (CreateApiToken, error) { |
42 | 50 | url := fmt.Sprintf("/v2/auth/api-tokens/%s", name) |
43 | 51 |
|
44 | | - res, err := a.client.Post(url, nil) |
| 52 | + var res *http.Response |
| 53 | + var err error |
| 54 | + |
| 55 | + if organization != "" { |
| 56 | + reqBody := struct { |
| 57 | + Organization string `json:"organization"` |
| 58 | + }{ |
| 59 | + Organization: organization, |
| 60 | + } |
| 61 | + jsonData, marshalErr := json.Marshal(reqBody) |
| 62 | + if marshalErr != nil { |
| 63 | + return CreateApiToken{}, fmt.Errorf("failed to marshal request body: %w", marshalErr) |
| 64 | + } |
| 65 | + body := bytes.NewReader(jsonData) |
| 66 | + res, err = a.client.Post(url, body) |
| 67 | + } else { |
| 68 | + res, err = a.client.Post(url, nil) |
| 69 | + } |
| 70 | + |
45 | 71 | if err != nil { |
46 | 72 | return CreateApiToken{}, fmt.Errorf("failed to create token: %s", err) |
47 | 73 | } |
|
0 commit comments