Skip to content

Commit

Permalink
Merge pull request digitalocean#335 from scottcrawford03/mp-2498/add-…
Browse files Browse the repository at this point in the history
…mp-1-click-api

add 1-click service and request
  • Loading branch information
bentranter authored Jun 1, 2020
2 parents d2202c2 + 04d89bc commit 3cae153
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
52 changes: 52 additions & 0 deletions 1-click.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package godo

import (
"context"
"fmt"
"net/http"
)

const oneClickBasePath = "v2/1-clicks"

// OneClickService is an interface for interacting with 1-clicks with the
// DigitalOcean API.
// See: https://developers.digitalocean.com/documentation/v2/#1-click-applications
type OneClickService interface {
List(context.Context, string) ([]*OneClick, *Response, error)
}

var _ OneClickService = &OneClickServiceOp{}

// OneClickServiceOp interfaces with 1-click endpoints in the DigitalOcean API.
type OneClickServiceOp struct {
client *Client
}

// OneClick is the structure of a 1-click
type OneClick struct {
Slug string `json:"slug"`
Type string `json:"type"`
}

// OneClicksRoot is the root of the json payload that contains a list of 1-clicks
type OneClicksRoot struct {
List []*OneClick `json:"1_clicks"`
}

// List returns a list of the available 1-click applications.
func (ocs *OneClickServiceOp) List(ctx context.Context, oneClickType string) ([]*OneClick, *Response, error) {
path := fmt.Sprintf(`%s?type=%s`, oneClickBasePath, oneClickType)

req, err := ocs.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

root := new(OneClicksRoot)
resp, err := ocs.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root.List, resp, nil
}
49 changes: 49 additions & 0 deletions 1-click_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package godo

import (
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var testOneClick = &OneClick{
Slug: "test-slug",
Type: "droplet",
}

var testOneClickJSON = `
{
"slug":"test-slug",
"type":"droplet"
}
`

func TestOneClick_List(t *testing.T) {
setup()
defer teardown()

svc := client.OneClick
path := "/v2/1-clicks"
want := []*OneClick{
testOneClick,
}

jsonBlob := `
{
"1_clicks": [
` + testOneClickJSON + `
]
}
`
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jsonBlob)
})

got, _, err := svc.List(ctx, "")
require.NoError(t, err)
assert.Equal(t, want, got)
}
2 changes: 2 additions & 0 deletions godo.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type Client struct {
Registry RegistryService
Databases DatabasesService
VPCs VPCsService
OneClick OneClickService

// Optional function called after every successful request made to the DO APIs
onRequestCompleted RequestCompletionCallback
Expand Down Expand Up @@ -211,6 +212,7 @@ func NewClient(httpClient *http.Client) *Client {
c.Registry = &RegistryServiceOp{client: c}
c.Databases = &DatabasesServiceOp{client: c}
c.VPCs = &VPCsServiceOp{client: c}
c.OneClick = &OneClickServiceOp{client: c}

return c
}
Expand Down

0 comments on commit 3cae153

Please sign in to comment.