|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "github.com/conductorone/baton-sdk/pkg/uhttp" |
| 11 | + "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + baseUrl = "https://www.notion.so/scim/v2" |
| 16 | + DefaultUserSchema = "urn:ietf:params:scim:schemas:core:2.0:User" |
| 17 | +) |
| 18 | + |
| 19 | +type NotionClient struct { |
| 20 | + client *uhttp.BaseHttpClient |
| 21 | + scimToken string |
| 22 | +} |
| 23 | + |
| 24 | +func (c *NotionClient) GetUsers(ctx context.Context, pageOps PaginationOptions) ([]User, string, error) { |
| 25 | + var nextPage string |
| 26 | + requestURL := fmt.Sprint(baseUrl, "/Users") |
| 27 | + |
| 28 | + var res UsersResponse |
| 29 | + _, err := c.doRequest( |
| 30 | + ctx, |
| 31 | + http.MethodGet, |
| 32 | + requestURL, |
| 33 | + &res, |
| 34 | + nil, |
| 35 | + WithPageSize(pageOps.PerPage), |
| 36 | + WithStartIndex(pageOps.StartIndex), |
| 37 | + ) |
| 38 | + if err != nil { |
| 39 | + return nil, "", err |
| 40 | + } |
| 41 | + |
| 42 | + if (int64(pageOps.StartIndex) + res.ItemsPerPage) < res.TotalResults { |
| 43 | + nextPage = strconv.FormatInt(int64(pageOps.StartIndex)+res.ItemsPerPage, 10) |
| 44 | + } |
| 45 | + |
| 46 | + return res.Resources, nextPage, nil |
| 47 | +} |
| 48 | + |
| 49 | +// GetGroups returns all Notion groups. |
| 50 | +func (c *NotionClient) GetGroups(ctx context.Context, pageOps PaginationOptions) ([]Group, string, error) { |
| 51 | + var nextPage string |
| 52 | + requestURL := fmt.Sprint(baseUrl, "/Groups") |
| 53 | + |
| 54 | + var res GroupsResponse |
| 55 | + _, err := c.doRequest( |
| 56 | + ctx, |
| 57 | + http.MethodGet, |
| 58 | + requestURL, |
| 59 | + &res, |
| 60 | + nil, |
| 61 | + WithPageSize(pageOps.PerPage), |
| 62 | + WithStartIndex(pageOps.StartIndex), |
| 63 | + ) |
| 64 | + if err != nil { |
| 65 | + return nil, "", err |
| 66 | + } |
| 67 | + |
| 68 | + if (int64(pageOps.StartIndex) + res.ItemsPerPage) < res.TotalResults { |
| 69 | + nextPage = strconv.FormatInt(int64(pageOps.StartIndex)+res.ItemsPerPage, 10) |
| 70 | + } |
| 71 | + |
| 72 | + return res.Resources, nextPage, nil |
| 73 | +} |
| 74 | + |
| 75 | +// GetGroup returns group details by group ID. |
| 76 | +func (c *NotionClient) GetGroup(ctx context.Context, groupId string) (Group, error) { |
| 77 | + requestURL := fmt.Sprint(baseUrl, "/Groups/", groupId) |
| 78 | + |
| 79 | + var groupResponse Group |
| 80 | + _, err := c.doRequest(ctx, http.MethodGet, requestURL, &groupResponse, nil) |
| 81 | + if err != nil { |
| 82 | + return Group{}, err |
| 83 | + } |
| 84 | + |
| 85 | + return groupResponse, nil |
| 86 | +} |
| 87 | + |
| 88 | +func (c *NotionClient) GetUser(ctx context.Context, userID string) (*User, error) { |
| 89 | + var userData *User |
| 90 | + requestURL := fmt.Sprint(baseUrl, "/Users/", userID) |
| 91 | + |
| 92 | + _, err := c.doRequest(ctx, http.MethodGet, requestURL, &userData, nil) |
| 93 | + if err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + |
| 97 | + return userData, nil |
| 98 | +} |
| 99 | + |
| 100 | +func (c *NotionClient) CreateUser(ctx context.Context, user *User) (*User, error) { |
| 101 | + var newUser *User |
| 102 | + requestURL := fmt.Sprint(baseUrl, "/Users") |
| 103 | + |
| 104 | + _, err := c.doRequest(ctx, http.MethodPost, requestURL, &newUser, user) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + |
| 109 | + return newUser, nil |
| 110 | +} |
| 111 | + |
| 112 | +func (c *NotionClient) DeleteUser(ctx context.Context, userID string) error { |
| 113 | + requestURL := fmt.Sprint(baseUrl, "/Users/", userID) |
| 114 | + |
| 115 | + _, err := c.doRequest(ctx, http.MethodDelete, requestURL, nil, nil) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func (c *NotionClient) doRequest( |
| 124 | + ctx context.Context, |
| 125 | + method string, |
| 126 | + endpointUrl string, |
| 127 | + res interface{}, |
| 128 | + body interface{}, |
| 129 | + reqOpts ...ReqOpt, |
| 130 | +) (http.Header, error) { |
| 131 | + var resp *http.Response |
| 132 | + |
| 133 | + urlAddress, err := url.Parse(endpointUrl) |
| 134 | + if err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + |
| 138 | + for _, o := range reqOpts { |
| 139 | + o(urlAddress) |
| 140 | + } |
| 141 | + |
| 142 | + opts := []uhttp.RequestOption{uhttp.WithBearerToken(c.scimToken)} |
| 143 | + if body != nil { |
| 144 | + opts = append(opts, uhttp.WithAcceptJSONHeader(), uhttp.WithContentTypeJSONHeader(), uhttp.WithJSONBody(body)) |
| 145 | + } |
| 146 | + |
| 147 | + req, err := c.client.NewRequest( |
| 148 | + ctx, |
| 149 | + method, |
| 150 | + urlAddress, |
| 151 | + opts..., |
| 152 | + ) |
| 153 | + if err != nil { |
| 154 | + return nil, err |
| 155 | + } |
| 156 | + |
| 157 | + switch method { |
| 158 | + case http.MethodGet, http.MethodPut, http.MethodPost, http.MethodPatch: |
| 159 | + var doOptions []uhttp.DoOption |
| 160 | + if res != nil { |
| 161 | + doOptions = append(doOptions, uhttp.WithResponse(&res)) |
| 162 | + } |
| 163 | + resp, err = c.client.Do(req, doOptions...) |
| 164 | + if resp != nil { |
| 165 | + defer resp.Body.Close() |
| 166 | + } |
| 167 | + |
| 168 | + case http.MethodDelete: |
| 169 | + resp, err = c.client.Do(req) |
| 170 | + if resp != nil { |
| 171 | + defer resp.Body.Close() |
| 172 | + } |
| 173 | + } |
| 174 | + if err != nil { |
| 175 | + return nil, err |
| 176 | + } |
| 177 | + |
| 178 | + return resp.Header, nil |
| 179 | +} |
| 180 | + |
| 181 | +func New(ctx context.Context, scimToken string) (*NotionClient, error) { |
| 182 | + httpClient, err := uhttp.NewClient(ctx, uhttp.WithLogger(true, ctxzap.Extract(ctx))) |
| 183 | + if err != nil { |
| 184 | + return nil, err |
| 185 | + } |
| 186 | + |
| 187 | + cli, err := uhttp.NewBaseHttpClientWithContext(ctx, httpClient) |
| 188 | + if err != nil { |
| 189 | + return nil, err |
| 190 | + } |
| 191 | + |
| 192 | + notionClient := NotionClient{ |
| 193 | + client: cli, |
| 194 | + scimToken: scimToken, |
| 195 | + } |
| 196 | + |
| 197 | + return ¬ionClient, nil |
| 198 | +} |
0 commit comments