Skip to content

Commit

Permalink
Merge pull request #27 from stephanwilliams/aqlfunctions
Browse files Browse the repository at this point in the history
Add AQL function requests
  • Loading branch information
solher authored Jul 26, 2017
2 parents bbba7fd + 29c4a7d commit 993fe24
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
79 changes: 79 additions & 0 deletions requests/aqlfunction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package requests

import (
"encoding/json"
)

// CreateAQLFunction creates a new AQL user function.
type CreateAQLFunction struct {
Name string `json:"name"`
Code string `json:"code"`
IsDeterministic bool `json:"isDeterministic"`
}

func (r *CreateAQLFunction) Path() string {
return "/_api/aqlfunction"
}

func (r *CreateAQLFunction) Method() string {
return "POST"
}

func (r *CreateAQLFunction) Generate() []byte {
m, _ := json.Marshal(r)
return m
}

// DeleteAQLFunction removes an existing AQL user function.
type DeleteAQLFunction struct {
Name string
Group bool
}

func (r *DeleteAQLFunction) Path() string {
path := "/_api/aqlfunction/" + r.Name

if r.Group {
path += "?group=true"
}

return path
}

func (r *DeleteAQLFunction) Method() string {
return "DELETE"
}

func (r *DeleteAQLFunction) Generate() []byte {
return nil
}

// GetAQLFunctions gets all registered AQL functions.
type GetAQLFunctions struct {
Namespace string
}

func (r *GetAQLFunctions) Path() string {
path := "/_api/aqlfunction"

if r.Namespace != "" {
path += "?namespace=" + r.Namespace
}

return path
}

func (r *GetAQLFunctions) Method() string {
return "GET"
}

func (r *GetAQLFunctions) Generate() []byte {
return nil
}

type AQLFunction struct {
Name string `json:"name"`
Code string `json:"code"`
}

type GetAQLFunctionsResult []AQLFunction
4 changes: 3 additions & 1 deletion senders.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func (s *basicSender) Send(ctx context.Context, cli *http.Client, req *http.Requ
}

parsed := parsedResponse{}
if strings.Contains(res.Header.Get("Content-Type"), "application/json") {
// Some API calls (such as /_api/aqlfunction) return arrays, so we have to check that
// the body is a JSON object before trying to unmarshal.
if strings.Contains(res.Header.Get("Content-Type"), "application/json") && raw[0] == '{' {
if err := json.Unmarshal(raw, &parsed); err != nil {
return nil, withMessage(err, "could not decode the json database response")
}
Expand Down

0 comments on commit 993fe24

Please sign in to comment.