Skip to content

Commit

Permalink
Add require_signature_auth parameter (#28)
Browse files Browse the repository at this point in the history
* Add require_signature_auth parameter

* go fmt

* Change the logic for require_signature_auth from int to bool

* go fmt

* Use JSON marshalling and unmarshalling functions

Co-authored-by: Marius <[email protected]>
  • Loading branch information
Etienne-Carriere and Acconut authored Oct 22, 2020
1 parent 33620e5 commit e67b5c1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
67 changes: 63 additions & 4 deletions template.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package transloadit

import "context"
import (
"context"
"encoding/json"
)

// Template contains details about a single template.
type Template struct {
ID string `json:"id"`
Name string `json:"name"`
Content TemplateContent `json:"content"`
ID string
Name string
Content TemplateContent
RequireSignatureAuth bool
}

// TemplateContent contains details about the content of a single template.
Expand All @@ -20,6 +24,11 @@ type TemplateList struct {
Count int `json:"count"`
}

type templateListInternal struct {
Templates []templateInternal `json:"items"`
Count int `json:"count"`
}

// NewTemplate returns a new Template struct with initialized values. This
// template will not be saved to Transloadit. To do so, please use the
// Client.CreateTemplate function.
Expand All @@ -36,13 +45,58 @@ func (template *Template) AddStep(name string, step map[string]interface{}) {
template.Content.Steps[name] = step
}

// templateInternal is the struct we use for encoding/decoding the Template
// JSON since we need to convert between boolean and integer.
type templateInternal struct {
ID string `json:"id"`
Name string `json:"name"`
Content TemplateContent `json:"content"`
RequireSignatureAuth int `json:"require_signature_auth"`
}

func (template *Template) UnmarshalJSON(b []byte) error {
var internal templateInternal
if err := json.Unmarshal(b, &internal); err != nil {
return err
}

template.Name = internal.Name
template.Content = internal.Content
template.ID = internal.ID
if internal.RequireSignatureAuth == 1 {
template.RequireSignatureAuth = true
} else {
template.RequireSignatureAuth = false
}

return nil
}

func (template Template) MarshalJSON() ([]byte, error) {
var internal templateInternal

internal.Name = template.Name
internal.Content = template.Content
internal.ID = template.ID
if template.RequireSignatureAuth {
internal.RequireSignatureAuth = 1
} else {
internal.RequireSignatureAuth = 0
}

return json.Marshal(internal)
}

// CreateTemplate will save the provided template struct as a new template
// and return the ID of the new template.
func (client *Client) CreateTemplate(ctx context.Context, template Template) (string, error) {
content := map[string]interface{}{
"name": template.Name,
"template": template.Content,
}
if template.RequireSignatureAuth {
content["require_signature_auth"] = 1
}

if err := client.request(ctx, "POST", "templates", content, &template); err != nil {
return "", err
Expand Down Expand Up @@ -73,6 +127,11 @@ func (client *Client) UpdateTemplate(ctx context.Context, templateID string, new
"name": newTemplate.Name,
"template": newTemplate.Content,
}
if newTemplate.RequireSignatureAuth {
content["require_signature_auth"] = 1
} else {
content["require_signature_auth"] = 0
}

return client.request(ctx, "PUT", "templates/"+templateID, content, nil)
}
Expand Down
8 changes: 8 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func TestTemplate(t *testing.T) {

template := NewTemplate()
template.Name = templateName
template.RequireSignatureAuth = true
template.AddStep("resize", map[string]interface{}{
"robot": "/image/resize",
"width": 75,
Expand Down Expand Up @@ -39,6 +40,9 @@ func TestTemplate(t *testing.T) {
if template.Name != templateName {
t.Error("wrong template name")
}
if !template.RequireSignatureAuth {
t.Error("require_signature_auth is not enabled")
}
if _, found := template.Content.Steps["resize"]; !found {
t.Error("resize step missing")
}
Expand All @@ -51,6 +55,7 @@ func TestTemplate(t *testing.T) {
template.Name = newTemplateName
template.AddStep("bar", map[string]interface{}{})
template.AddStep("baz", map[string]interface{}{})
template.RequireSignatureAuth = false

// Step 3: Update previously created template
if err := client.UpdateTemplate(ctx, id, template); err != nil {
Expand All @@ -74,6 +79,9 @@ func TestTemplate(t *testing.T) {
if _, found := template.Content.Steps["baz"]; !found {
t.Error("baz step missing")
}
if template.RequireSignatureAuth {
t.Error("require_signature_auth was not disabled after an update")
}

// Step 5: Delete template
if err := client.DeleteTemplate(ctx, id); err != nil {
Expand Down

0 comments on commit e67b5c1

Please sign in to comment.