Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paginated response #8

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions api/baseline/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,28 @@ func ConfigureStack(token string, params map[string]interface{}) error {
}

// ListWorkgroups retrieves a paginated list of baseline workgroups scoped to the given API token
func ListWorkgroups(token, applicationID string, params map[string]interface{}) ([]*Workgroup, error) {
status, resp, err := InitBaselineService(token).Get("workgroups", params)
func ListWorkgroups(token, applicationID string, params map[string]interface{}) ([]*Workgroup, *common.Response, error) {
status, resp, err := InitBaselineService(token).GetPaginated("workgroups", params)
if err != nil {
return nil, err
return nil, nil, err
}

if status != 200 {
return nil, fmt.Errorf("failed to list baseline workgroups; status: %v", status)
return nil, nil, fmt.Errorf("failed to list baseline workgroups; status: %v", status)
}

workgroups := make([]*Workgroup, 0)
for _, item := range resp.([]interface{}) {
for _, item := range resp.Results.([]interface{}) {
workgroup := &Workgroup{}
workgroupraw, _ := json.Marshal(item)
json.Unmarshal(workgroupraw, &workgroup)
workgroups = append(workgroups, workgroup)
}

return workgroups, nil
response := &common.Response{
TotalCount: resp.TotalCount,
}
return workgroups, response, nil
}

// CreateWorkgroup initializes a new or previously-joined workgroup on the local baseline stack
Expand Down Expand Up @@ -115,25 +118,28 @@ func UpdateWorkgroup(id, token string, params map[string]interface{}) error {
}

// ListWorkflows retrieves a paginated list of baseline workflows scoped to the given API token
func ListWorkflows(token, applicationID string, params map[string]interface{}) ([]*Workflow, error) {
status, resp, err := InitBaselineService(token).Get("workflows", params)
func ListWorkflows(token, applicationID string, params map[string]interface{}) ([]*Workflow, *common.Response, error) {
status, resp, err := InitBaselineService(token).GetPaginated("workflows", params)
if err != nil {
return nil, err
return nil, nil, err
}

if status != 200 {
return nil, fmt.Errorf("failed to list baseline workflows; status: %v", status)
return nil, nil, fmt.Errorf("failed to list baseline workflows; status: %v", status)
}

workflows := make([]*Workflow, 0)
for _, item := range resp.([]interface{}) {
for _, item := range resp.Results.([]interface{}) {
workflow := &Workflow{}
workflowraw, _ := json.Marshal(item)
json.Unmarshal(workflowraw, &workflow)
workflows = append(workflows, workflow)
}

return workflows, nil
response := &common.Response{
TotalCount: resp.TotalCount,
}
return workflows, response, nil
}

// CreateWorkflow initializes a new workflow on the local baseline stack
Expand All @@ -155,25 +161,28 @@ func CreateWorkflow(token string, params map[string]interface{}) (*Workflow, err
}

// ListWorksteps retrieves a paginated list of baseline worksteps scoped to the given API token
func ListWorksteps(token, applicationID string, params map[string]interface{}) ([]*Workstep, error) {
status, resp, err := InitBaselineService(token).Get("worksteps", params)
func ListWorksteps(token, applicationID string, params map[string]interface{}) ([]*Workstep, *common.Response, error) {
status, resp, err := InitBaselineService(token).GetPaginated("worksteps", params)
if err != nil {
return nil, err
return nil, nil, err
}

if status != 200 {
return nil, fmt.Errorf("failed to list baseline worksteps; status: %v", status)
return nil, nil, fmt.Errorf("failed to list baseline worksteps; status: %v", status)
}

worksteps := make([]*Workstep, 0)
for _, item := range resp.([]interface{}) {
for _, item := range resp.Results.([]interface{}) {
workstep := &Workstep{}
workstepraw, _ := json.Marshal(item)
json.Unmarshal(workstepraw, &workstep)
worksteps = append(worksteps, workstep)
}

return worksteps, nil
response := &common.Response{
TotalCount: resp.TotalCount,
}
return worksteps, response, nil
}

// CreateWorkstep initializes a new workstep on the local baseline stack
Expand Down
30 changes: 18 additions & 12 deletions api/c2/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,29 @@ func InitC2Service(token string) *Service {
}

// ListNodes list nodes for the given authorization scope
func ListNodes(token string, params map[string]interface{}) ([]*Node, error) {
func ListNodes(token string, params map[string]interface{}) ([]*Node, *common.Response, error) {
uri := fmt.Sprintf("nodes")
status, resp, err := InitC2Service(token).Get(uri, params)
status, resp, err := InitC2Service(token).GetPaginated(uri, params)
if err != nil {
return nil, err
return nil, nil, err
}

if status != 200 {
return nil, fmt.Errorf("failed to list nodes; status: %v", status)
return nil, nil, fmt.Errorf("failed to list nodes; status: %v", status)
}

nodes := make([]*Node, 0)
for _, item := range resp.([]interface{}) {
for _, item := range resp.Results.([]interface{}) {
node := &Node{}
raw, _ := json.Marshal(item)
json.Unmarshal(raw, &node)
nodes = append(nodes, node)
}

return nodes, nil
response := &common.Response{
TotalCount: resp.TotalCount,
}
return nodes, response, nil
}

// CreateNode creates and deploys a new node for the given authorization scope
Expand Down Expand Up @@ -183,25 +186,28 @@ func DeleteNode(token, nodeID string) (*Node, error) {
}

// ListLoadBalancers list load balancers for the given authorization scope
func ListLoadBalancers(token string, params map[string]interface{}) ([]*LoadBalancer, error) {
status, resp, err := InitC2Service(token).Get("load_balancers", params)
func ListLoadBalancers(token string, params map[string]interface{}) ([]*LoadBalancer, *common.Response, error) {
status, resp, err := InitC2Service(token).GetPaginated("load_balancers", params)
if err != nil {
return nil, err
return nil, nil, err
}

if status != 200 {
return nil, fmt.Errorf("failed to list load balancers; status: %v", status)
return nil, nil, fmt.Errorf("failed to list load balancers; status: %v", status)
}

balancers := make([]*LoadBalancer, 0)
for _, item := range resp.([]interface{}) {
for _, item := range resp.Results.([]interface{}) {
balancer := &LoadBalancer{}
raw, _ := json.Marshal(item)
json.Unmarshal(raw, &balancer)
balancers = append(balancers, balancer)
}

return balancers, nil
response := &common.Response{
TotalCount: resp.TotalCount,
}
return balancers, response, nil
}

// CreateLoadBalancer creates and deploys a new load balancer for the given authorization scope
Expand Down
17 changes: 17 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ type Client struct {
Password *string
}

type PaginatedResponse struct {
Results interface{}
TotalCount string
}

func requestTimeout() time.Duration {
if customRequestTimeout != nil {
return *customRequestTimeout
Expand Down Expand Up @@ -273,6 +278,18 @@ func (c *Client) Get(uri string, params map[string]interface{}) (status int, res
return c.parseResponse(resp)
}

// Get paginated constructs and synchronously sends an API GET request
func (c *Client) GetPaginated(uri string, params map[string]interface{}) (status int, response *PaginatedResponse, err error) {
url := c.buildURL(uri)
resp, err := c.sendRequest("GET", url, defaultContentType, params)
status, results, err := c.parseResponse(resp)
paginatedResponse := &PaginatedResponse{
Results: results,
TotalCount: resp.Header.Get("x-total-results-count"),
}
return status, paginatedResponse, err
}

// Head constructs and synchronously sends an API HEAD request; returns the headers
func (c *Client) Head(uri string, params map[string]interface{}) (status int, response map[string][]string, err error) {
url := c.buildURL(uri)
Expand Down
2 changes: 1 addition & 1 deletion api/ident/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ type User struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
Permissions uint32 `json:"permissions,omitempty,omitempty"`
Permissions uint32 `json:"permissions,omitempty"`
PrivacyPolicyAgreedAt *time.Time `json:"privacy_policy_agreed_at,omitempty"`
TermsOfServiceAgreedAt *time.Time `json:"terms_of_service_agreed_at,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Expand Down
Loading