Base64 encode the code in json objects.
This will make the API easier for clients. Also add a ?raw=1 flag to the function GET API, to get just the code of the function.
This commit is contained in:
@@ -56,6 +56,10 @@ func TestFunctionApi(t *testing.T) {
|
|||||||
uid1 := m.Uid
|
uid1 := m.Uid
|
||||||
log.Printf("Created function %v: %v", m.Name, m.Uid)
|
log.Printf("Created function %v: %v", m.Name, m.Uid)
|
||||||
|
|
||||||
|
code, err := g.client.FunctionGetRaw(m)
|
||||||
|
panicIf(err)
|
||||||
|
assert(string(code) == testFunc.Code, "code from FunctionGetRaw must match created function")
|
||||||
|
|
||||||
testFunc.Code = "code2"
|
testFunc.Code = "code2"
|
||||||
m, err = g.client.FunctionUpdate(testFunc)
|
m, err = g.client.FunctionUpdate(testFunc)
|
||||||
panicIf(err)
|
panicIf(err)
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -97,6 +98,10 @@ func (c *Client) handleResponse(resp *http.Response) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) FunctionCreate(f *fission.Function) (*fission.Metadata, error) {
|
func (c *Client) FunctionCreate(f *fission.Function) (*fission.Metadata, error) {
|
||||||
|
orig := f.Code
|
||||||
|
f.Code = base64.StdEncoding.EncodeToString([]byte(f.Code))
|
||||||
|
defer func() { f.Code = orig }()
|
||||||
|
|
||||||
reqbody, err := json.Marshal(f)
|
reqbody, err := json.Marshal(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -149,10 +154,33 @@ func (c *Client) FunctionGet(m *fission.Metadata) (*fission.Function, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dec, err := base64.StdEncoding.DecodeString(f.Code)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
f.Code = string(dec)
|
||||||
|
|
||||||
return &f, nil
|
return &f, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) FunctionGetRaw(m *fission.Metadata) ([]byte, error) {
|
||||||
|
relativeUrl := fmt.Sprintf("functions/%v?raw=1", m.Name)
|
||||||
|
if len(m.Uid) > 0 {
|
||||||
|
relativeUrl += fmt.Sprintf("&uid=%v", m.Uid)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := http.Get(c.url(relativeUrl))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
return c.handleResponse(resp)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) FunctionUpdate(f *fission.Function) (*fission.Metadata, error) {
|
func (c *Client) FunctionUpdate(f *fission.Function) (*fission.Metadata, error) {
|
||||||
|
f.Code = base64.StdEncoding.EncodeToString([]byte(f.Code))
|
||||||
|
|
||||||
reqbody, err := json.Marshal(f)
|
reqbody, err := json.Marshal(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import (
|
|||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
|
||||||
|
"encoding/base64"
|
||||||
"github.com/platform9/fission"
|
"github.com/platform9/fission"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -56,6 +57,13 @@ func (api *API) FunctionApiCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dec, err := base64.StdEncoding.DecodeString(f.Code)
|
||||||
|
if err != nil {
|
||||||
|
api.respondWithError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
f.Code = string(dec)
|
||||||
|
|
||||||
uid, err := api.FunctionStore.Create(&f)
|
uid, err := api.FunctionStore.Create(&f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.respondWithError(w, err)
|
api.respondWithError(w, err)
|
||||||
@@ -78,6 +86,7 @@ func (api *API) FunctionApiGet(w http.ResponseWriter, r *http.Request) {
|
|||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
m.Name = vars["function"]
|
m.Name = vars["function"]
|
||||||
m.Uid = r.FormValue("uid") // empty if uid is absent
|
m.Uid = r.FormValue("uid") // empty if uid is absent
|
||||||
|
raw := r.FormValue("raw") // just the code
|
||||||
|
|
||||||
f, err := api.FunctionStore.Get(&m)
|
f, err := api.FunctionStore.Get(&m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -85,12 +94,17 @@ func (api *API) FunctionApiGet(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := json.Marshal(f)
|
var resp []byte
|
||||||
if err != nil {
|
if raw != "" {
|
||||||
api.respondWithError(w, err)
|
resp = []byte(f.Code)
|
||||||
return
|
} else {
|
||||||
|
f.Code = base64.StdEncoding.EncodeToString([]byte(f.Code))
|
||||||
|
resp, err = json.Marshal(f)
|
||||||
|
if err != nil {
|
||||||
|
api.respondWithError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
api.respondWithSuccess(w, resp)
|
api.respondWithSuccess(w, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,6 +130,13 @@ func (api *API) FunctionApiUpdate(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dec, err := base64.StdEncoding.DecodeString(f.Code)
|
||||||
|
if err != nil {
|
||||||
|
api.respondWithError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
f.Code = string(dec)
|
||||||
|
|
||||||
uid, err := api.FunctionStore.Update(&f)
|
uid, err := api.FunctionStore.Update(&f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.respondWithError(w, err)
|
api.respondWithError(w, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user