diff --git a/template.go b/template.go index beff9f0..898efd6 100644 --- a/template.go +++ b/template.go @@ -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. @@ -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. @@ -36,6 +45,48 @@ 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) { @@ -43,6 +94,9 @@ func (client *Client) CreateTemplate(ctx context.Context, template Template) (st "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 @@ -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) } diff --git a/template_test.go b/template_test.go index 83f5fdd..045e201 100644 --- a/template_test.go +++ b/template_test.go @@ -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, @@ -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") } @@ -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 { @@ -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 {