-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from pusher/pusher-functions
Pusher Functions
- Loading branch information
Showing
23 changed files
with
1,191 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.