Skip to content

Commit

Permalink
Merge pull request #1 from pusher/pusher-functions
Browse files Browse the repository at this point in the history
Pusher Functions
  • Loading branch information
robertoles authored Oct 7, 2022
2 parents 0220e7e + e84ce9a commit 123eb3c
Show file tree
Hide file tree
Showing 23 changed files with 1,191 additions and 38 deletions.
8 changes: 4 additions & 4 deletions api/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import (
)

//isAPIKeyValid returns true if the stored API key is valid.
func isAPIKeyValid() bool {
func (p *PusherApi) isAPIKeyValid() bool {
if viper.GetString("token") != "" {
_, err := GetAllApps()
_, err := p.GetAllApps()
if err == nil {
return true
}
}
return false
}

func validateKeyOrDie() {
if !isAPIKeyValid() {
func (p *PusherApi) validateKeyOrDie() {
if !p.isAPIKeyValid() {
fmt.Println("Your API key isn't valid. Add one with the `login` command.")
os.Exit(1)
}
Expand Down
10 changes: 5 additions & 5 deletions api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ type App struct {
Cluster string `json:"cluster"`
}

const getAppsAPIEndpoint = "/apps.json"
const GetAppsAPIEndpoint = "/apps.json"

func GetAllApps() ([]App, error) {
response, err := makeRequest("GET", getAppsAPIEndpoint, nil)
func (p *PusherApi) GetAllApps() ([]App, error) {
response, err := p.makeRequest("GET", GetAppsAPIEndpoint, nil)
if err != nil {
return nil, err
}
Expand All @@ -29,8 +29,8 @@ func GetAllApps() ([]App, error) {
return apps, nil
}

func GetApp(appID string) (*App, error) {
apps, err := GetAllApps()
func (p *PusherApi) GetApp(appID string) (*App, error) {
apps, err := p.GetAllApps()
if err != nil {
return nil, err
}
Expand Down
173 changes: 173 additions & 0 deletions api/function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package api

import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
)

type FunctionService interface {
GetAllFunctionsForApp(name string) ([]Function, error)
CreateFunction(appID string, name string, events []string, body string) (Function, error)
DeleteFunction(appID string, functionID string) error
GetFunction(appID string, functionID string) (Function, error)
UpdateFunction(appID string, functionID string, name string, events []string, body string) (Function, error)
}

type Function struct {
ID int `json:"id"`
Name string `json:"name"`
Events []string `json:"events"`
Body string `json:"body"`
}

type FunctionRequestBody struct {
Function FunctionRequestBodyFunction `json:"function"`
}

type FunctionRequestBodyFunction struct {
Name string `json:"name"`
Events []string `json:"events"`
Body string `json:"body"`
}

const FunctionsApiEndpoint = "/apps/%s/functions.json"
const FunctionApiEndpoint = "/apps/%s/functions/%s.json"

func NewFunctionRequestBody(name string, events []string, body string) FunctionRequestBody {
return FunctionRequestBody{
Function: FunctionRequestBodyFunction{
Name: name,
Events: events,
Body: body,
},
}
}

func (p *PusherApi) GetAllFunctionsForApp(appID string) ([]Function, error) {
response, err := p.makeRequest("GET", fmt.Sprintf(FunctionsApiEndpoint, appID), nil)
if err != nil {
return nil, errors.New("that app ID wasn't recognised as linked to your account")
}
functions := []Function{}
err = json.Unmarshal([]byte(response), &functions)
if err != nil {
return nil, errors.New("Response from Pusher API was not valid json, please retry")
}
return functions, nil
}

func (p *PusherApi) CreateFunction(appID string, name string, events []string, body string) (Function, error) {
encoded := base64.StdEncoding.EncodeToString([]byte(body))

request := NewFunctionRequestBody(name, events, encoded)

requestJson, err := json.Marshal(&request)
if err != nil {
return Function{}, errors.New("Could not create function")
}
response, err := p.makeRequest("POST", fmt.Sprintf(FunctionsApiEndpoint, appID), requestJson)
if err != nil {
switch err.(type) {
case *HttpStatusError:
e := err.(*HttpStatusError)
switch e.StatusCode {
case http.StatusUnprocessableEntity:
return Function{}, errors.New(response)
default:
return Function{}, errors.New("Pusher encountered an error, please retry")
}
default:
return Function{}, errors.New("Pusher encountered an error, please retry")
}
}

function := Function{}
err = json.Unmarshal([]byte(response), &function)
if err != nil {
return Function{}, errors.New("Response from Pusher API was not valid json, please retry")
}
return function, nil
}

func (p *PusherApi) DeleteFunction(appID string, functionID string) error {
_, err := p.makeRequest("DELETE", fmt.Sprintf(FunctionApiEndpoint, appID, functionID), nil)
if err != nil {
switch err.(type) {
case *HttpStatusError:
e := err.(*HttpStatusError)
switch e.StatusCode {
case http.StatusNotFound:
return fmt.Errorf("Funciton with id: %s, could not be found", functionID)
default:
return errors.New("Pusher encountered an error, please retry")
}
default:
return errors.New("Pusher encountered an error, please retry")
}
}

return nil
}

func (p *PusherApi) GetFunction(appID string, functionID string) (Function, error) {
response, err := p.makeRequest("GET", fmt.Sprintf(FunctionApiEndpoint, appID, functionID), nil)
if err != nil {
switch err.(type) {
case *HttpStatusError:
e := err.(*HttpStatusError)
if e.StatusCode == http.StatusNotFound {
return Function{}, errors.New("Function could not be found")
} else {
return Function{}, errors.New("Pusher encountered an error, please retry")
}
default:
return Function{}, errors.New("Pusher encountered an error, please retry")
}
}
function := Function{}
err = json.Unmarshal([]byte(response), &function)
if err != nil {
return Function{}, errors.New("Response from Pusher API was not valid json, please retry")
}
decodedBody, err := base64.StdEncoding.DecodeString(function.Body)
if err != nil {
return Function{}, errors.New("Response from Pusher API did not include a valid function Body, please retry")
}
function.Body = string(decodedBody)
return function, nil
}

func (p *PusherApi) UpdateFunction(appID string, functionID string, name string, events []string, body string) (Function, error) {
encoded := base64.StdEncoding.EncodeToString([]byte(body))

request := NewFunctionRequestBody(name, events, encoded)

requestJson, err := json.Marshal(&request)
if err != nil {
return Function{}, errors.New("Could not serialize function")
}
response, err := p.makeRequest("PUT", fmt.Sprintf(FunctionApiEndpoint, appID, functionID), requestJson)
if err != nil {
switch err.(type) {
case *HttpStatusError:
e := err.(*HttpStatusError)
if e.StatusCode == http.StatusUnprocessableEntity {
return Function{}, errors.New(response)
} else {
return Function{}, errors.New("Pusher encountered an error, please retry")
}
default:
return Function{}, errors.New("Pusher encountered an error, please retry")
}
}

function := Function{}
err = json.Unmarshal([]byte(response), &function)
if err != nil {
return Function{}, errors.New("Response from Pusher API was not valid json, please retry")
}
return function, nil
}
Loading

0 comments on commit 123eb3c

Please sign in to comment.