Skip to content

Commit

Permalink
Including path and scheme (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcanizalez authored Sep 12, 2021
1 parent 303f9ea commit 2dec648
Show file tree
Hide file tree
Showing 17 changed files with 100 additions and 104 deletions.
10 changes: 5 additions & 5 deletions client/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
Expand All @@ -22,12 +21,13 @@ type Client struct {
Job *JobClient
Environment *EnvironmentClient
HttpClient *http.Client
BasePath string
}

const basePath = "/api/v1/"
var defaultPath string = "/api/v1/"

func (c *Client) newRequest(method, path string, body interface{}) (*http.Request, error) {
rel := &url.URL{Path: path}
rel := &url.URL{Path: c.BasePath + path}
u := c.BaseURL.ResolveReference(rel)
var buf io.ReadWriter
if body != nil {
Expand Down Expand Up @@ -58,15 +58,14 @@ func (c *Client) do(req *http.Request, v interface{}) (*http.Response, error) {
err = json.NewDecoder(resp.Body).Decode(v)
}

fmt.Println(resp.StatusCode)

return resp, err
}

func NewClient(httpClient *http.Client, token string, baseUrl *url.URL) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}

c := &Client{HttpClient: httpClient}
c.BaseURL = baseUrl
c.Token = token
Expand All @@ -77,5 +76,6 @@ func NewClient(httpClient *http.Client, token string, baseUrl *url.URL) *Client
c.Environment = &EnvironmentClient{Client: c}
c.Secret = &SecretClient{Client: c}
c.Job = &JobClient{Client: c}
c.BasePath = baseUrl.Path + defaultPath
return c
}
8 changes: 4 additions & 4 deletions client/client/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type EnvironmentClient struct {
}

func (c *EnvironmentClient) List(organizationId string, workspaceId string, filter string) ([]*models.Environment, error) {
req, err := c.Client.newRequest(http.MethodGet, basePath+fmt.Sprintf("organization/%v/workspace/%v/environment", organizationId, workspaceId), nil)
req, err := c.Client.newRequest(http.MethodGet, fmt.Sprintf("organization/%v/workspace/%v/environment", organizationId, workspaceId), nil)
if err != nil {
return nil, err
}
Expand All @@ -25,7 +25,7 @@ func (c *EnvironmentClient) Create(organizationId string, workspaceId string, en
Data: &environment,
}

req, err := c.Client.newRequest(http.MethodPost, basePath+fmt.Sprintf("organization/%v/workspace/%v/environment", organizationId, workspaceId), reqBody)
req, err := c.Client.newRequest(http.MethodPost, fmt.Sprintf("organization/%v/workspace/%v/environment", organizationId, workspaceId), reqBody)
if err != nil {
return nil, err
}
Expand All @@ -35,7 +35,7 @@ func (c *EnvironmentClient) Create(organizationId string, workspaceId string, en
}

func (c *EnvironmentClient) Delete(organizationId string, workspaceId string, environmentId string) error {
req, err := c.Client.newRequest(http.MethodDelete, fmt.Sprintf(basePath+"organization/%v/workspace/%v/environment/%v", organizationId, workspaceId, environmentId), nil)
req, err := c.Client.newRequest(http.MethodDelete, fmt.Sprintf("organization/%v/workspace/%v/environment/%v", organizationId, workspaceId, environmentId), nil)
if err != nil {
return err
}
Expand All @@ -49,7 +49,7 @@ func (c *EnvironmentClient) Update(organizationId string, workspaceId string, en
Data: &environment,
}

req, err := c.Client.newRequest(http.MethodPatch, fmt.Sprintf(basePath+"organization/%v/workspace/%v/environment/%v", organizationId, workspaceId, environment.ID), reqBody)
req, err := c.Client.newRequest(http.MethodPatch, fmt.Sprintf("organization/%v/workspace/%v/environment/%v", organizationId, workspaceId, environment.ID), reqBody)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions client/client/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type JobClient struct {
}

func (c *JobClient) List(organizationId string, filter string) ([]*models.Job, error) {
req, err := c.Client.newRequest(http.MethodGet, basePath+fmt.Sprintf("organization/%v/job", organizationId), nil)
req, err := c.Client.newRequest(http.MethodGet, fmt.Sprintf("organization/%v/job", organizationId), nil)
if err != nil {
return nil, err
}
Expand All @@ -25,7 +25,7 @@ func (c *JobClient) Create(organizationId string, job models.Job) (*models.Job,
Data: &job,
}

req, err := c.Client.newRequest(http.MethodPost, basePath+fmt.Sprintf("organization/%v/job", organizationId), reqBody)
req, err := c.Client.newRequest(http.MethodPost, fmt.Sprintf("organization/%v/job", organizationId), reqBody)
if err != nil {
return nil, err
}
Expand All @@ -35,7 +35,7 @@ func (c *JobClient) Create(organizationId string, job models.Job) (*models.Job,
}

func (c *JobClient) Delete(organizationID string, moduleId string) error {
req, err := c.Client.newRequest(http.MethodDelete, fmt.Sprintf(basePath+"organization/%v/module/%v", organizationID, moduleId), nil)
req, err := c.Client.newRequest(http.MethodDelete, fmt.Sprintf("organization/%v/module/%v", organizationID, moduleId), nil)
if err != nil {
return err
}
Expand All @@ -49,7 +49,7 @@ func (c *JobClient) Update(organizationId string, module models.Module) error {
Data: &module,
}

req, err := c.Client.newRequest(http.MethodPatch, fmt.Sprintf(basePath+"organization/%v/module/%v", organizationId, module.ID), reqBody)
req, err := c.Client.newRequest(http.MethodPatch, fmt.Sprintf("organization/%v/module/%v", organizationId, module.ID), reqBody)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions client/client/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type ModuleClient struct {
}

func (c *ModuleClient) List(organizationId string, filter string) ([]*models.Module, error) {
req, err := c.Client.newRequest(http.MethodGet, basePath+fmt.Sprintf("organization/%v/module", organizationId), nil)
req, err := c.Client.newRequest(http.MethodGet, fmt.Sprintf("organization/%v/module", organizationId), nil)
if err != nil {
return nil, err
}
Expand All @@ -25,7 +25,7 @@ func (c *ModuleClient) Create(organizationId string, module models.Module) (*mod
Data: &module,
}

req, err := c.Client.newRequest(http.MethodPost, basePath+fmt.Sprintf("organization/%v/module", organizationId), reqBody)
req, err := c.Client.newRequest(http.MethodPost, fmt.Sprintf("organization/%v/module", organizationId), reqBody)
if err != nil {
return nil, err
}
Expand All @@ -35,7 +35,7 @@ func (c *ModuleClient) Create(organizationId string, module models.Module) (*mod
}

func (c *ModuleClient) Delete(organizationID string, moduleId string) error {
req, err := c.Client.newRequest(http.MethodDelete, fmt.Sprintf(basePath+"organization/%v/module/%v", organizationID, moduleId), nil)
req, err := c.Client.newRequest(http.MethodDelete, fmt.Sprintf("organization/%v/module/%v", organizationID, moduleId), nil)
if err != nil {
return err
}
Expand All @@ -49,7 +49,7 @@ func (c *ModuleClient) Update(organizationId string, module models.Module) error
Data: &module,
}

req, err := c.Client.newRequest(http.MethodPatch, fmt.Sprintf(basePath+"organization/%v/module/%v", organizationId, module.ID), reqBody)
req, err := c.Client.newRequest(http.MethodPatch, fmt.Sprintf("organization/%v/module/%v", organizationId, module.ID), reqBody)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions client/client/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type OrganizationClient struct {
}

func (c *OrganizationClient) List(filter string) ([]*models.Organization, error) {
req, err := c.Client.newRequest(http.MethodGet, basePath+"organization", nil)
req, err := c.Client.newRequest(http.MethodGet, "organization", nil)
if err != nil {
return nil, err
}
Expand All @@ -25,7 +25,7 @@ func (c *OrganizationClient) Create(organization models.Organization) (*models.O
Data: &organization,
}

req, err := c.Client.newRequest(http.MethodPost, basePath+"organization", reqBody)
req, err := c.Client.newRequest(http.MethodPost, "organization", reqBody)
if err != nil {
return nil, err
}
Expand All @@ -35,7 +35,7 @@ func (c *OrganizationClient) Create(organization models.Organization) (*models.O
}

func (c *OrganizationClient) Delete(organizationID string) error {
req, err := c.Client.newRequest(http.MethodDelete, fmt.Sprintf(basePath+"organization/%v", organizationID), nil)
req, err := c.Client.newRequest(http.MethodDelete, fmt.Sprintf("organization/%v", organizationID), nil)
if err != nil {
return err
}
Expand All @@ -49,7 +49,7 @@ func (c *OrganizationClient) Update(organization models.Organization) error {
Data: &organization,
}

req, err := c.Client.newRequest(http.MethodPatch, fmt.Sprintf(basePath+"organization/%v", organization.ID), reqBody)
req, err := c.Client.newRequest(http.MethodPatch, fmt.Sprintf("organization/%v", organization.ID), reqBody)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions client/client/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type SecretClient struct {
}

func (c *SecretClient) List(organizationId string, workspaceId string, filter string) ([]*models.Secret, error) {
req, err := c.Client.newRequest(http.MethodGet, basePath+fmt.Sprintf("organization/%v/workspace/%v/secret", organizationId, workspaceId), nil)
req, err := c.Client.newRequest(http.MethodGet, fmt.Sprintf("organization/%v/workspace/%v/secret", organizationId, workspaceId), nil)
if err != nil {
return nil, err
}
Expand All @@ -25,7 +25,7 @@ func (c *SecretClient) Create(organizationId string, workspaceId string, secret
Data: &secret,
}

req, err := c.Client.newRequest(http.MethodPost, basePath+fmt.Sprintf("organization/%v/workspace/%v/secret", organizationId, workspaceId), reqBody)
req, err := c.Client.newRequest(http.MethodPost, fmt.Sprintf("organization/%v/workspace/%v/secret", organizationId, workspaceId), reqBody)
if err != nil {
return nil, err
}
Expand All @@ -35,7 +35,7 @@ func (c *SecretClient) Create(organizationId string, workspaceId string, secret
}

func (c *SecretClient) Delete(organizationId string, workspaceId string, secretId string) error {
req, err := c.Client.newRequest(http.MethodDelete, fmt.Sprintf(basePath+"organization/%v/workspace/%v/secret/%v", organizationId, workspaceId, secretId), nil)
req, err := c.Client.newRequest(http.MethodDelete, fmt.Sprintf("organization/%v/workspace/%v/secret/%v", organizationId, workspaceId, secretId), nil)
if err != nil {
return err
}
Expand All @@ -49,7 +49,7 @@ func (c *SecretClient) Update(organizationId string, workspaceId string, secret
Data: &secret,
}

req, err := c.Client.newRequest(http.MethodPatch, fmt.Sprintf(basePath+"organization/%v/workspace/%v/secret/%v", organizationId, workspaceId, secret.ID), reqBody)
req, err := c.Client.newRequest(http.MethodPatch, fmt.Sprintf("organization/%v/workspace/%v/secret/%v", organizationId, workspaceId, secret.ID), reqBody)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions client/client/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type VariableClient struct {
}

func (c *VariableClient) List(organizationId string, workspaceId string, filter string) ([]*models.Variable, error) {
req, err := c.Client.newRequest(http.MethodGet, basePath+fmt.Sprintf("organization/%v/workspace/%v/variable", organizationId, workspaceId), nil)
req, err := c.Client.newRequest(http.MethodGet, fmt.Sprintf("organization/%v/workspace/%v/variable", organizationId, workspaceId), nil)
if err != nil {
return nil, err
}
Expand All @@ -25,7 +25,7 @@ func (c *VariableClient) Create(organizationId string, workspaceId string, varia
Data: &variable,
}

req, err := c.Client.newRequest(http.MethodPost, basePath+fmt.Sprintf("organization/%v/workspace/%v/variable", organizationId, workspaceId), reqBody)
req, err := c.Client.newRequest(http.MethodPost, fmt.Sprintf("organization/%v/workspace/%v/variable", organizationId, workspaceId), reqBody)
if err != nil {
return nil, err
}
Expand All @@ -35,7 +35,7 @@ func (c *VariableClient) Create(organizationId string, workspaceId string, varia
}

func (c *VariableClient) Delete(organizationId string, workspaceId string, variableId string) error {
req, err := c.Client.newRequest(http.MethodDelete, fmt.Sprintf(basePath+"organization/%v/workspace/%v/variable/%v", organizationId, workspaceId, variableId), nil)
req, err := c.Client.newRequest(http.MethodDelete, fmt.Sprintf("organization/%v/workspace/%v/variable/%v", organizationId, workspaceId, variableId), nil)
if err != nil {
return err
}
Expand All @@ -49,7 +49,7 @@ func (c *VariableClient) Update(organizationId string, workspaceId string, varia
Data: &variable,
}

req, err := c.Client.newRequest(http.MethodPatch, fmt.Sprintf(basePath+"organization/%v/workspace/%v/variable/%v", organizationId, workspaceId, variable.ID), reqBody)
req, err := c.Client.newRequest(http.MethodPatch, fmt.Sprintf("organization/%v/workspace/%v/variable/%v", organizationId, workspaceId, variable.ID), reqBody)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions client/client/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type WorkspaceClient struct {
}

func (c *WorkspaceClient) List(organizationId string, filter string) ([]*models.Workspace, error) {
req, err := c.Client.newRequest(http.MethodGet, basePath+fmt.Sprintf("organization/%v/workspace", organizationId), nil)
req, err := c.Client.newRequest(http.MethodGet, fmt.Sprintf("organization/%v/workspace", organizationId), nil)
if err != nil {
return nil, err
}
Expand All @@ -25,7 +25,7 @@ func (c *WorkspaceClient) Create(organizationId string, workspace models.Workspa
Data: &workspace,
}

req, err := c.Client.newRequest(http.MethodPost, basePath+fmt.Sprintf("organization/%v/workspace", organizationId), reqBody)
req, err := c.Client.newRequest(http.MethodPost, fmt.Sprintf("organization/%v/workspace", organizationId), reqBody)
if err != nil {
return nil, err
}
Expand All @@ -35,7 +35,7 @@ func (c *WorkspaceClient) Create(organizationId string, workspace models.Workspa
}

func (c *WorkspaceClient) Delete(organizationID string, workspaceId string) error {
req, err := c.Client.newRequest(http.MethodDelete, fmt.Sprintf(basePath+"organization/%v/workspace/%v", organizationID, workspaceId), nil)
req, err := c.Client.newRequest(http.MethodDelete, fmt.Sprintf("organization/%v/workspace/%v", organizationID, workspaceId), nil)
if err != nil {
return err
}
Expand All @@ -49,7 +49,7 @@ func (c *WorkspaceClient) Update(organizationId string, workspace models.Workspa
Data: &workspace,
}

req, err := c.Client.newRequest(http.MethodPatch, fmt.Sprintf(basePath+"organization/%v/workspace/%v", organizationId, workspace.ID), reqBody)
req, err := c.Client.newRequest(http.MethodPatch, fmt.Sprintf("organization/%v/workspace/%v", organizationId, workspace.ID), reqBody)
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions client/models/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ type JobRelationshipsOrganization struct {

type JobRelationshipsWorkspace struct {

// data
Data *JobRelationshipsWorkspaceData `json:"data"`
}

type JobRelationshipsWorkspaceData struct {

// id
ID string `json:"id,omitempty"`

Expand Down
10 changes: 1 addition & 9 deletions cmd/environment_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package cmd

import (
"azb/client/models"
"encoding/json"
"fmt"
"log"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -56,11 +54,5 @@ func createEnvironment() {
return
}

prettyJSON, err := json.MarshalIndent(resp, "", " ")
if err != nil {
log.Fatal("Failed to generate json", err)
}

fmt.Printf("%s\n", string(prettyJSON))

renderOutput(resp, output)
}
16 changes: 5 additions & 11 deletions cmd/job_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package cmd

import (
"azb/client/models"
"encoding/json"
"fmt"
"log"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -45,8 +43,10 @@ func createJob() {
Type: "job",
Relationships: &models.JobRelationships{
Workspace: &models.JobRelationshipsWorkspace{
Type: "workspace",
ID: JobCreateWorkspaceId,
Data: &models.JobRelationshipsWorkspaceData{
Type: "workspace",
ID: JobCreateWorkspaceId,
},
},
},
}
Expand All @@ -58,11 +58,5 @@ func createJob() {
return
}

prettyJSON, err := json.MarshalIndent(resp, "", " ")
if err != nil {
log.Fatal("Failed to generate json", err)
}

fmt.Printf("%s\n", string(prettyJSON))

renderOutput(resp, output)
}
9 changes: 1 addition & 8 deletions cmd/job_list.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package cmd

import (
"encoding/json"
"fmt"
"log"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -40,10 +38,5 @@ func listJobs() {
return
}

prettyJSON, err := json.MarshalIndent(resp, "", " ")
if err != nil {
log.Fatal("Failed to generate json", err)
}

fmt.Printf("%s\n", string(prettyJSON))
renderOutput(resp, output)
}
Loading

0 comments on commit 2dec648

Please sign in to comment.