Skip to content

Commit

Permalink
wip: Add more endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
gagantrivedi committed Apr 15, 2024
1 parent cdb9d1f commit 4f7d6d1
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
57 changes: 57 additions & 0 deletions api_keys.go
Original file line number Diff line number Diff line change
@@ -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

}
40 changes: 40 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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)
Expand Down
45 changes: 45 additions & 0 deletions identity.go
Original file line number Diff line number Diff line change
@@ -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

}
14 changes: 14 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package flagsmithapi
import (
"encoding/json"
"log"
"time"
)

type Project struct {
Expand Down Expand Up @@ -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"`
}

0 comments on commit 4f7d6d1

Please sign in to comment.