Skip to content

Commit

Permalink
Merge 30593df into e4e2f80
Browse files Browse the repository at this point in the history
  • Loading branch information
nukosuke committed Dec 3, 2018
2 parents e4e2f80 + 30593df commit 478459d
Show file tree
Hide file tree
Showing 7 changed files with 596 additions and 0 deletions.
97 changes: 97 additions & 0 deletions fixture/GET/dynamic_content/items.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"items": [
{
"url": "https://example.zendesk.com/api/v2/dynamic_content/items/360000346774.json",
"id": 360000346774,
"name": "title",
"placeholder": "{{dc.title}}",
"default_locale_id": 1,
"outdated": false,
"created_at": "2018-12-03T14:09:14Z",
"updated_at": "2018-12-03T14:14:23Z",
"variants": [
{
"url": "https://example.zendesk.com/api/v2/dynamic_content/items/360000346774/variants/360001100153.json",
"id": 360001100153,
"content": "ZendeskのためのTerraform provider",
"locale_id": 67,
"outdated": false,
"active": true,
"default": false,
"created_at": "2018-12-03T14:13:56Z",
"updated_at": "2018-12-03T14:13:56Z"
},
{
"url": "https://example.zendesk.com/api/v2/dynamic_content/items/360000346774/variants/360001113534.json",
"id": 360001113534,
"content": "Zendesk的Terraform provider",
"locale_id": 9,
"outdated": false,
"active": true,
"default": false,
"created_at": "2018-12-03T14:14:23Z",
"updated_at": "2018-12-03T14:14:23Z"
},
{
"url": "https://example.zendesk.com/api/v2/dynamic_content/items/360000346774/variants/360001113514.json",
"id": 360001113514,
"content": "Terraform provider for Zendesk",
"locale_id": 1,
"outdated": false,
"active": true,
"default": true,
"created_at": "2018-12-03T14:09:14Z",
"updated_at": "2018-12-03T14:09:14Z"
}
]
},
{
"url": "https://example.zendesk.com/api/v2/dynamic_content/items/360000354093.json",
"id": 360000354093,
"name": "description",
"placeholder": "{{dc.description}}",
"default_locale_id": 1,
"outdated": false,
"created_at": "2018-12-03T14:17:44Z",
"updated_at": "2018-12-03T14:19:26Z",
"variants": [
{
"url": "https://example.zendesk.com/api/v2/dynamic_content/items/360000354093/variants/360001100193.json",
"id": 360001100193,
"content": "使用example,您可以使用Terraform设置Zendesk",
"locale_id": 9,
"outdated": false,
"active": true,
"default": false,
"created_at": "2018-12-03T14:19:26Z",
"updated_at": "2018-12-03T14:19:26Z"
},
{
"url": "https://example.zendesk.com/api/v2/dynamic_content/items/360000354093/variants/360001113554.json",
"id": 360001113554,
"content": "exampleを使えばTerraformでZendeskをセットアップすることができます",
"locale_id": 67,
"outdated": false,
"active": true,
"default": false,
"created_at": "2018-12-03T14:19:16Z",
"updated_at": "2018-12-03T14:19:16Z"
},
{
"url": "https://example.zendesk.com/api/v2/dynamic_content/items/360000354093/variants/360001100173.json",
"id": 360001100173,
"content": "example can setup your Zendesk by Terraform",
"locale_id": 1,
"outdated": false,
"active": true,
"default": true,
"created_at": "2018-12-03T14:17:44Z",
"updated_at": "2018-12-03T14:17:44Z"
}
]
}
],
"next_page": null,
"previous_page": null,
"count": 2
}
47 changes: 47 additions & 0 deletions fixture/POST/dynamic_content/items.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"item": {
"url": "https://example.zendesk.com/api/v2/dynamic_content/items/360000346774.json",
"id": 360000346774,
"name": "title",
"placeholder": "{{dc.title}}",
"default_locale_id": 1,
"outdated": false,
"created_at": "2018-12-03T14:09:14Z",
"updated_at": "2018-12-03T14:14:23Z",
"variants": [
{
"url": "https://example.zendesk.com/api/v2/dynamic_content/items/360000346774/variants/360001100153.json",
"id": 360001100153,
"content": "ZendeskのためのTerraform provider",
"locale_id": 67,
"outdated": false,
"active": true,
"default": false,
"created_at": "2018-12-03T14:13:56Z",
"updated_at": "2018-12-03T14:13:56Z"
},
{
"url": "https://example.zendesk.com/api/v2/dynamic_content/items/360000346774/variants/360001113534.json",
"id": 360001113534,
"content": "Zendesk的Terraform provider",
"locale_id": 9,
"outdated": false,
"active": true,
"default": false,
"created_at": "2018-12-03T14:14:23Z",
"updated_at": "2018-12-03T14:14:23Z"
},
{
"url": "https://example.zendesk.com/api/v2/dynamic_content/items/360000346774/variants/360001113514.json",
"id": 360001113514,
"content": "Terraform provider for Zendesk",
"locale_id": 1,
"outdated": false,
"active": true,
"default": true,
"created_at": "2018-12-03T14:09:14Z",
"updated_at": "2018-12-03T14:09:14Z"
}
]
}
}
47 changes: 47 additions & 0 deletions script/create_locale_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

# create_locale_types.rb
# Create locale_types.go from public locale list from API
#
# (C) nukosuke <nukosuke@lavabit.com>

require 'json'
require 'uri'
require 'net/http'
require 'erb'

uri = URI.parse('https://terraform-provider-zendesk.zendesk.com/api/v2/locales/public.json')
req = Net::HTTP::Get.new(uri)
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
http.request(req)
end

if res.is_a? Net::HTTPSuccess
data = JSON.parse(res.body)
puts ERB.new(DATA.read).result(binding)
else
abort "failed to fetch from locale API: #{res.body.inspect}"
end

__END__
package zendesk

// DO NOT EDIT
// This file is generated by script/create_locale_types.rb
//
// <%= data['count'] %> locales are supported

const (<% data['locales'].each_with_index do |locale, idx| %>
// Locale<%= locale_key = locale['locale'].gsub('-', '').upcase %> <%= locale['presentation_name'] %>
Locale<%= locale_key %><% if idx == 0 %> = iota<% end %><% end %>
)

var localeTypeText = map[int]string {<% data['locales'].each do |locale| %>
// Locale<%= locale_key = locale['locale'].gsub('-', '').upcase %> <%= locale['presentation_name'] %>
Locale<%= locale_key %>: "<%= locale['locale'] %>",<% end %>
}

// LocaleTypeText returns locale type text
func LocaleTypeText(loc int) string {
return localeTypeText[loc]
}
73 changes: 73 additions & 0 deletions zendesk/dynamic_content.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package zendesk

import (
"encoding/json"
"time"
)

// DynamicContentItem is zendesk dynamic content item JSON payload format
// https://developer.zendesk.com/rest_api/docs/support/users
type DynamicContentItem struct {
ID int64 `json:"id,omitempty"`
URL string `json:"url,omitempty"`
Name string `json:"name"`
Placeholder string `json:"placeholder,omitempty"`
DefaultLocaleID int64 `json:"default_locale_id"`
Outdated bool `json:"outdated,omitempty"`
Variants []DynamicContentVariant `json:"variants"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}

// DynamicContentVariant is zendesk dynamic content variant JSON payload format
// https://developer.zendesk.com/rest_api/docs/support/dynamic_content#json-format-for-variants
type DynamicContentVariant struct {
ID int64 `json:"id,omitempty"`
URL string `json:"url,omitempty"`
Content string `json:"content"`
LocaleID int64 `json:"locale_id"`
Outdated bool `json:"outdated,omitempty"`
Active bool `json:"active,omitempty"`
Default bool `json:"default,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}

// GetDynamicContentItems fetches dynamic content item list
// https://developer.zendesk.com/rest_api/docs/support/dynamic_content#list-items
func (z *Client) GetDynamicContentItems() ([]DynamicContentItem, Page, error) {
var data struct {
Items []DynamicContentItem `json:"items"`
Page
}

body, err := z.Get("/dynamic_content/items.json")
if err != nil {
return []DynamicContentItem{}, Page{}, err
}

err = json.Unmarshal(body, &data)
if err != nil {
return []DynamicContentItem{}, Page{}, err
}
return data.Items, data.Page, nil
}

// CreateDynamicContentItem creates new dynamic content item
// https://developer.zendesk.com/rest_api/docs/support/dynamic_content#create-item
func (z *Client) CreateDynamicContentItem(item DynamicContentItem) (DynamicContentItem, error) {
var data, result struct {
Item DynamicContentItem `json:"item"`
}

body, err := z.Post("/groups.json", data)
if err != nil {
return DynamicContentItem{}, err
}

err = json.Unmarshal(body, &result)
if err != nil {
return DynamicContentItem{}, err
}
return result.Item, nil
}
43 changes: 43 additions & 0 deletions zendesk/dynamic_content_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package zendesk

import (
"net/http"
"testing"
)

func TestGetDynamicContentItems(t *testing.T) {
mockAPI := newMockAPI(http.MethodGet, "dynamic_content/items.json")
client := newTestClient(mockAPI)
defer mockAPI.Close()

items, page, err := client.GetDynamicContentItems()
if err != nil {
t.Fatalf("Failed to get dynamic content items: %s", err)
}

if len(items) != 2 {
t.Fatalf("expected length of dynamic content items is 2, but got %d", len(items))
}

if len(items[0].Variants) != 3 {
t.Fatalf("expected length of items[0].Variants is 3, but got %d", len(items[0].Variants))
}

if page.HasPrev() || page.HasNext() {
t.Fatalf("page fields are wrong: %v", page)
}
}

func TestCreateDynamicContentItem(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodPost, "dynamic_content/items.json", http.StatusCreated)
client := newTestClient(mockAPI)
defer mockAPI.Close()

item, err := client.CreateDynamicContentItem(DynamicContentItem{})
if err != nil {
t.Fatalf("Failed to get valid response: %s", err)
}
if item.ID == 0 {
t.Fatal("Failed to create dynamic content item")
}
}
16 changes: 16 additions & 0 deletions zendesk/locale.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package zendesk

import (
"time"
)

// Locale is zendesk locale JSON payload format
// https://developer.zendesk.com/rest_api/docs/support/locales
type Locale struct {
ID int64 `json:"id"`
URL string `json:"url"`
Locale string `json:"locale"`
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

0 comments on commit 478459d

Please sign in to comment.