-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorganization.go
60 lines (48 loc) · 1.57 KB
/
organization.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
package turso
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
const (
organizationEndpoint = "v1/organizations"
)
type OrganizationService service
type organizationService interface {
// ListOrganizations lists all organizations for the authorized user
ListOrganizations(ctx context.Context) (*[]Organization, error)
}
// Organization is the struct for the Turso Organization object
type Organization struct {
Name string `json:"name"`
Slug string `json:"slug"`
Type string `json:"type"`
PlanID string `json:"plan_id"`
Overages bool `json:"overages"`
BlockedReads bool `json:"blocked_reads"`
BlockedWrites bool `json:"blocked_writes"`
PlanTimeline string `json:"plan_timeline"`
Memory int `json:"memory"`
}
// getOrganizationEndpoint returns the endpoint for the Turso API organization service
func getOrganizationEndpoint(baseURL string) string {
return fmt.Sprintf("%s/%s", baseURL, organizationEndpoint)
}
// ListOrganizations satisfies the organizationService interface
func (s *OrganizationService) ListOrganizations(ctx context.Context) (*[]Organization, error) {
endpoint := getOrganizationEndpoint(s.client.cfg.BaseURL)
resp, err := s.client.DoRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var out []Organization
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, newBadRequestError("organizations", "listing", resp.StatusCode)
}
return &out, nil
}