From 4f7d6d1090cad2ded880fa49f7c583856d7e9e37 Mon Sep 17 00:00:00 2001 From: Gagan Trivedi Date: Mon, 15 Apr 2024 14:44:37 +0530 Subject: [PATCH] wip: Add more endpoints --- api_keys.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ client.go | 40 +++++++++++++++++++++++++++++++++++++ identity.go | 45 ++++++++++++++++++++++++++++++++++++++++++ models.go | 14 +++++++++++++ 4 files changed, 156 insertions(+) create mode 100644 api_keys.go create mode 100644 identity.go diff --git a/api_keys.go b/api_keys.go new file mode 100644 index 0000000..9358175 --- /dev/null +++ b/api_keys.go @@ -0,0 +1,57 @@ +package flagsmithapi + +import ( + "fmt" +) + +func (c *Client) GetServerSideEnvKeys(environmentKey string) (*[]ServerSideEnvKey, error) { + url := fmt.Sprintf("%s/environments/%s/api-keys/", c.baseURL, environmentKey) + keys := []ServerSideEnvKey{} + resp, err := c.client.R().SetResult(&keys).Get(url) + if err != nil { + return nil, err + } + + if !resp.IsSuccess() { + return nil, fmt.Errorf("flagsmithapi: Error deleting identity: %v", resp) + } + return &keys, nil +} +func (c *Client) CreateServerSideEnvKey(environmentKey string, key *ServerSideEnvKey) error { + url := fmt.Sprintf("%s/environments/%s/api-keys/", c.baseURL, environmentKey) + + resp, err := c.client.R().SetBody(key).SetResult(&key).Post(url) + if err != nil { + return err + } + if !resp.IsSuccess() { + return fmt.Errorf("flagsmithapi: Error creating server side environment key: %s", resp) + } + return nil + +} +func (c *Client) UpdateServerSideEnvKey(environmentKey string, key *ServerSideEnvKey) error { + url := fmt.Sprintf("%s/environments/%s/api-keys/%d/", c.baseURL, environmentKey, key.ID) + resp, err := c.client.R().SetBody(key).SetResult(&key).Put(url) + if err != nil { + return nil + } + + if !resp.IsSuccess() { + return fmt.Errorf("flagsmithapi: Error updating server side environment key: %s", resp) + } + return nil +} +func (c *Client) DeleteServerSideEnvKey(environmentKey string, keyID int64) error { + url := fmt.Sprintf("%s/environments/%s/api-keys/%d/", c.baseURL, environmentKey, keyID) + resp, err := c.client.R().Delete(url) + if err != nil { + return err + } + + if !resp.IsSuccess() { + return fmt.Errorf("flagsmithapi: Error deleting server side environment key: %s", resp) + } + return nil + +} diff --git a/client.go b/client.go index 31aa6de..ff839b3 100644 --- a/client.go +++ b/client.go @@ -143,6 +143,32 @@ func (c *Client) GetProjectByID(projectID int64) (*Project, error) { return &project, nil } +func (c *Client) DeleteProject(projectID int64) error { + url := fmt.Sprintf("%s/projects/%d/", c.baseURL, projectID) + + resp, err := c.client.R().Delete(url) + + if err != nil { + return err + } + + if !resp.IsSuccess() { + return fmt.Errorf("flagsmithapi: Error deleting project: %s", resp) + } + return nil +} +func (c *Client) UpdateProject(project *Project) error { + url := fmt.Sprintf("%s/projects/%d/", c.baseURL, project.ID) + resp, err := c.client.R().SetBody(project).SetResult(&project).Put(url) + if err != nil { + return err + } + if !resp.IsSuccess() { + return fmt.Errorf("flagsmithapi: Error updating project: %s", resp.Status()) + } + return nil +} + func (c *Client) GetFeature(featureUUID string) (*Feature, error) { url := fmt.Sprintf("%s/features/get-by-uuid/%s/", c.baseURL, featureUUID) feature := Feature{} @@ -450,6 +476,20 @@ func (c *Client) GetEnvironment(apiKey string) (*Environment, error) { return &environment, nil } +func (c *Client) CreateEnvironment(environment *Environment) error { + url := fmt.Sprintf("%s/environments/", c.baseURL) + resp, err := c.client.R().SetBody(environment).SetResult(environment).Post(url) + + if err != nil { + return err + } + + if !resp.IsSuccess() { + return fmt.Errorf("flagsmithapi: Error creating environment: %s", resp) + } + + return nil +} func (c *Client) GetFeatureSegmentByID(featureSegmentID int64) (*FeatureSegment, error) { url := fmt.Sprintf("%s/features/feature-segments/%d/", c.baseURL, featureSegmentID) diff --git a/identity.go b/identity.go new file mode 100644 index 0000000..f306e5a --- /dev/null +++ b/identity.go @@ -0,0 +1,45 @@ +package flagsmithapi + +import ( + "fmt" +) + +func (c *Client) GetIdentity(environmentKey string, identityID int64) (*Identity, error) { + url := fmt.Sprintf("%s/environments/%s/identities/%d/", c.baseURL, environmentKey, identityID) + identity := Identity{} + resp, err := c.client.R().SetResult(&identity).Get(url) + if err != nil { + return nil, err + } + + if !resp.IsSuccess() { + return nil, fmt.Errorf("flagsmithapi: Error deleting identity: %v", resp) + } + return &identity, nil +} +func (c *Client) CreateIdentity(environmentKey string, identity *Identity) error { + url := fmt.Sprintf("%s/environments/%s/identities/", c.baseURL, environmentKey) + + resp, err := c.client.R().SetBody(identity).SetResult(&identity).Post(url) + if err != nil { + return err + } + if !resp.IsSuccess() { + return fmt.Errorf("flagsmithapi: Error creating identity: %s", resp) + } + return nil + +} +func (c *Client) DeleteIdentity(environmentKey string, identityID int64) error { + url := fmt.Sprintf("%s/environments/%s/identities/%d/", c.baseURL, environmentKey, identityID) + resp, err := c.client.R().Delete(url) + if err != nil { + return err + } + + if !resp.IsSuccess() { + return fmt.Errorf("flagsmithapi: Error deleting identity: %s", resp) + } + return nil + +} diff --git a/models.go b/models.go index 3cb3ceb..6134b54 100644 --- a/models.go +++ b/models.go @@ -3,6 +3,7 @@ package flagsmithapi import ( "encoding/json" "log" + "time" ) type Project struct { @@ -223,3 +224,16 @@ type Tag struct { ProjectUUID string `json:"-"` ProjectID *int64 `json:"project,omitempty"` } + +type Identity struct { + ID *int64 `json:"id,omitempty"` + Identifier string `json:"identifier"` +} + +type ServerSideEnvKey struct { + ID *int64 `json:"id,omitempty"` + Active bool `json:"active,omitempty"` + Name string `json:"name,omitempty"` + Key string `json:"key,omitempty"` + ExpiresAt time.Time `json:"expires_at,omitempty"` +}