Skip to content

Commit

Permalink
Merge pull request #50 from tamccall/add-get-field
Browse files Browse the repository at this point in the history
Add get ticket field
  • Loading branch information
nukosuke authored Mar 9, 2019
2 parents f91e726 + 1995eed commit 0196992
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ vendor/

# coverage by go test
coverage.txt

.idea
*.iml
.pre-commit-config.yaml
26 changes: 26 additions & 0 deletions fixture/GET/ticket_field.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"ticket_field": {
"url": "https://example.zendesk.com/api/v2/ticket_fields/360011737434.json",
"id": 360011737434,
"type": "subject",
"title": "Subject",
"raw_title": "Subject",
"description": "",
"raw_description": "",
"position": 0,
"active": true,
"required": false,
"collapsed_for_agents": false,
"regexp_for_validation": null,
"title_in_portal": "Subject",
"raw_title_in_portal": "Subject",
"visible_in_portal": true,
"editable_in_portal": true,
"required_in_portal": true,
"tag": null,
"created_at": "2018-11-23T16:05:12Z",
"updated_at": "2018-11-24T10:54:05Z",
"removable": false,
"agent_description": null
}
}
22 changes: 22 additions & 0 deletions zendesk/ticket_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zendesk

import (
"encoding/json"
"fmt"
"time"
)

Expand Down Expand Up @@ -81,3 +82,24 @@ func (z Client) CreateTicketField(ticketField TicketField) (TicketField, error)
}
return result.TicketField, nil
}

// GetTicketField gets a specified ticket field
// ref: https://developer.zendesk.com/rest_api/docs/support/ticket_fields#show-ticket-field
func (z Client) GetTicketField(ticketID int64) (TicketField, error) {
var result struct {
TicketField TicketField `json:"ticket_field"`
}

body, err := z.Get(fmt.Sprintf("/ticket_fields/%d.json", ticketID))

if err != nil {
return TicketField{}, err
}

err = json.Unmarshal(body, &result)
if err != nil {
return TicketField{}, err
}

return result.TicketField, err
}
16 changes: 16 additions & 0 deletions zendesk/ticket_field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,19 @@ func TestGetTicketFields(t *testing.T) {
t.Fatalf("expected length of ticket fields is , but got %d", len(ticketFields))
}
}

func TestGetTicketField(t *testing.T) {
mockAPI := newMockAPI(http.MethodGet, "ticket_field.json")
client := newTestClient(mockAPI)
defer mockAPI.Close()

ticketField, err := client.GetTicketField(123)
if err != nil {
t.Fatalf("Failed to get ticket fields: %s", err)
}

expectedID := int64(360011737434)
if ticketField.ID != expectedID {
t.Fatalf("Returned ticket field does not have the expected ID %d. Ticket id is %d", expectedID, ticketField.ID)
}
}

0 comments on commit 0196992

Please sign in to comment.