From f837e1a448a6beeb3bcb9b111a45d95178cba64f Mon Sep 17 00:00:00 2001 From: jamesallison Date: Thu, 26 Apr 2018 16:27:54 +0100 Subject: [PATCH 1/5] parse admin avatar --- admin.go | 9 +++++---- admin_api_test.go | 3 +++ fixtures/admins.json | 32 +++++++++++++++++--------------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/admin.go b/admin.go index 7b3e3ac..7e3d955 100644 --- a/admin.go +++ b/admin.go @@ -7,10 +7,11 @@ import ( // Admin represents an Admin in Intercom. type Admin struct { - ID json.Number `json:"id"` - Type string `json:"type"` - Name string `json:"name"` - Email string `json:"email"` + ID json.Number `json:"id"` + Type string `json:"type"` + Name string `json:"name"` + Email string `json:"email"` + Avatar string `json:"avatar"` } // AdminList represents an object holding list of Admins diff --git a/admin_api_test.go b/admin_api_test.go index 3a92f89..47f0875 100644 --- a/admin_api_test.go +++ b/admin_api_test.go @@ -12,6 +12,9 @@ func TestAdminAPIList(t *testing.T) { if adminList.Admins[0].ID != "1" { t.Errorf("ID was %s, expected 1", adminList.Admins[0].ID) } + if adminList.Admins[0].Avatar != "https://intercom.io/testA.png" { + t.Errorf("Avatar was %s, expected https://intercom.io/testA.png", adminList.Admins[0].Avatar) + } } type TestAdminHTTPClient struct { diff --git a/fixtures/admins.json b/fixtures/admins.json index 5a719a5..1a93921 100644 --- a/fixtures/admins.json +++ b/fixtures/admins.json @@ -1,17 +1,19 @@ { - "type": "admin.list", - "admins": [ - { - "type": "admin", - "email": "admin_a@example.io", - "id": "1", - "name": "Admin A" - }, - { - "type": "admin", - "email": "admin_b@example.io", - "id": "2", - "name": "Admin B" - } - ] + "type": "admin.list", + "admins": [ + { + "type": "admin", + "email": "admin_a@example.io", + "id": "1", + "name": "Admin A", + "avatar": "https://intercom.io/testA.png" + }, + { + "type": "admin", + "email": "admin_b@example.io", + "id": "2", + "name": "Admin B", + "avatar": "https://intercom.io/testB.png" + } + ] } From 8f56363bef4df1539b484ea09658a98dc1f7cb0d Mon Sep 17 00:00:00 2001 From: jamesallison Date: Thu, 26 Apr 2018 16:29:06 +0100 Subject: [PATCH 2/5] json formatting --- fixtures/admins.json | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/fixtures/admins.json b/fixtures/admins.json index 1a93921..e1a03e3 100644 --- a/fixtures/admins.json +++ b/fixtures/admins.json @@ -1,19 +1,19 @@ { - "type": "admin.list", - "admins": [ - { - "type": "admin", - "email": "admin_a@example.io", - "id": "1", - "name": "Admin A", - "avatar": "https://intercom.io/testA.png" - }, - { - "type": "admin", - "email": "admin_b@example.io", - "id": "2", - "name": "Admin B", - "avatar": "https://intercom.io/testB.png" - } - ] -} + "type": "admin.list", + "admins": [ + { + "type": "admin", + "email": "admin_a@example.io", + "id": 1, + "name": "Admin A", + "avatar": "https://intercom.io/testA.png" + }, + { + "type": "admin", + "email": "admin_b@example.io", + "id": 2, + "name": "Admin B", + "avatar": "https://intercom.io/testB.png" + } + ] +} \ No newline at end of file From 4d479561ca51c6c0578d0ac2f51728968e5966d0 Mon Sep 17 00:00:00 2001 From: jamesallison Date: Thu, 26 Apr 2018 17:14:53 +0100 Subject: [PATCH 3/5] admin id --- fixtures/admins.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fixtures/admins.json b/fixtures/admins.json index e1a03e3..6fdbeca 100644 --- a/fixtures/admins.json +++ b/fixtures/admins.json @@ -4,14 +4,14 @@ { "type": "admin", "email": "admin_a@example.io", - "id": 1, + "id": "1", "name": "Admin A", "avatar": "https://intercom.io/testA.png" }, { "type": "admin", "email": "admin_b@example.io", - "id": 2, + "id": "2", "name": "Admin B", "avatar": "https://intercom.io/testB.png" } From 3d35be4e7ebaeaea905c1202d0416aae6b52d507 Mon Sep 17 00:00:00 2001 From: jamesallison Date: Thu, 26 Apr 2018 19:26:30 +0100 Subject: [PATCH 4/5] fix avatar obj --- admin.go | 7 ++++++- admin_api_test.go | 2 +- fixtures/admins.json | 40 ++++++++++++++++++++++------------------ 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/admin.go b/admin.go index 7e3d955..7981342 100644 --- a/admin.go +++ b/admin.go @@ -5,13 +5,18 @@ import ( "fmt" ) +// AdminAvatar represents an admin's avatar +type AdminAvatar struct { + ImageURL string `json:"image_url"` +} + // Admin represents an Admin in Intercom. type Admin struct { ID json.Number `json:"id"` Type string `json:"type"` Name string `json:"name"` Email string `json:"email"` - Avatar string `json:"avatar"` + Avatar *UserAvatar `json:"avatar"` } // AdminList represents an object holding list of Admins diff --git a/admin_api_test.go b/admin_api_test.go index 47f0875..4685256 100644 --- a/admin_api_test.go +++ b/admin_api_test.go @@ -12,7 +12,7 @@ func TestAdminAPIList(t *testing.T) { if adminList.Admins[0].ID != "1" { t.Errorf("ID was %s, expected 1", adminList.Admins[0].ID) } - if adminList.Admins[0].Avatar != "https://intercom.io/testA.png" { + if adminList.Admins[0].Avatar.ImageURL != "https://intercom.io/testA.png" { t.Errorf("Avatar was %s, expected https://intercom.io/testA.png", adminList.Admins[0].Avatar) } } diff --git a/fixtures/admins.json b/fixtures/admins.json index 6fdbeca..96c55d6 100644 --- a/fixtures/admins.json +++ b/fixtures/admins.json @@ -1,19 +1,23 @@ { - "type": "admin.list", - "admins": [ - { - "type": "admin", - "email": "admin_a@example.io", - "id": "1", - "name": "Admin A", - "avatar": "https://intercom.io/testA.png" - }, - { - "type": "admin", - "email": "admin_b@example.io", - "id": "2", - "name": "Admin B", - "avatar": "https://intercom.io/testB.png" - } - ] -} \ No newline at end of file + "type": "admin.list", + "admins": [ + { + "type": "admin", + "email": "admin_a@example.io", + "id": "1", + "name": "Admin A", + "avatar": { + "image_url": "https://intercom.io/testA.png" + } + }, + { + "type": "admin", + "email": "admin_b@example.io", + "id": "2", + "name": "Admin B", + "avatar": { + "image_url": "https://intercom.io/testB.png" + } + } + ] +} From 235745feb6d7c1344e507b1eade5bd7d63eac3ac Mon Sep 17 00:00:00 2001 From: jamesallison Date: Thu, 26 Apr 2018 19:59:44 +0100 Subject: [PATCH 5/5] admin read endpoint --- admin.go | 10 +++++----- admin_api.go | 10 ++++++++++ admin_api_test.go | 14 ++++++++++++-- fixtures/admin.json | 10 ++++++++++ fixtures/admins.json | 10 ++-------- 5 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 fixtures/admin.json diff --git a/admin.go b/admin.go index 7981342..b32d6a0 100644 --- a/admin.go +++ b/admin.go @@ -12,11 +12,11 @@ type AdminAvatar struct { // Admin represents an Admin in Intercom. type Admin struct { - ID json.Number `json:"id"` - Type string `json:"type"` - Name string `json:"name"` - Email string `json:"email"` - Avatar *UserAvatar `json:"avatar"` + ID json.Number `json:"id"` + Type string `json:"type"` + Name string `json:"name"` + Email string `json:"email"` + Avatar *AdminAvatar `json:"avatar"` } // AdminList represents an object holding list of Admins diff --git a/admin_api.go b/admin_api.go index 2671348..1c12424 100644 --- a/admin_api.go +++ b/admin_api.go @@ -24,3 +24,13 @@ func (api AdminAPI) list() (AdminList, error) { err = json.Unmarshal(data, &adminList) return adminList, err } + +func (api AdminAPI) read(adminID string) (Admin, error) { + admin := Admin{} + data, err := api.httpClient.Get("/admins/"+adminID, nil) + if err != nil { + return admin, err + } + err = json.Unmarshal(data, &admin) + return admin, err +} diff --git a/admin_api_test.go b/admin_api_test.go index 4685256..b445e60 100644 --- a/admin_api_test.go +++ b/admin_api_test.go @@ -12,8 +12,18 @@ func TestAdminAPIList(t *testing.T) { if adminList.Admins[0].ID != "1" { t.Errorf("ID was %s, expected 1", adminList.Admins[0].ID) } - if adminList.Admins[0].Avatar.ImageURL != "https://intercom.io/testA.png" { - t.Errorf("Avatar was %s, expected https://intercom.io/testA.png", adminList.Admins[0].Avatar) +} + +func TestAdminAPIRead(t *testing.T) { + http := TestAdminHTTPClient{fixtureFilename: "fixtures/admin.json", expectedURI: "/admins/123", t: t} + api := AdminAPI{httpClient: &http} + admin, err := api.read("123") + if err != nil { + t.Errorf("Error reading admin: %v", err) + } + + if admin.Avatar.ImageURL != "https://intercom.io/testA.png" { + t.Errorf("Expected: https://intercom.io/testA.png, got %s", admin.Avatar.ImageURL) } } diff --git a/fixtures/admin.json b/fixtures/admin.json new file mode 100644 index 0000000..d907ec7 --- /dev/null +++ b/fixtures/admin.json @@ -0,0 +1,10 @@ +{ + "type": "admin", + "type": "admin", + "email": "admin_a@example.io", + "id": "123", + "name": "Admin A", + "avatar": { + "image_url": "https://intercom.io/testA.png" + } +} diff --git a/fixtures/admins.json b/fixtures/admins.json index 96c55d6..8689b3a 100644 --- a/fixtures/admins.json +++ b/fixtures/admins.json @@ -5,19 +5,13 @@ "type": "admin", "email": "admin_a@example.io", "id": "1", - "name": "Admin A", - "avatar": { - "image_url": "https://intercom.io/testA.png" - } + "name": "Admin A" }, { "type": "admin", "email": "admin_b@example.io", "id": "2", - "name": "Admin B", - "avatar": { - "image_url": "https://intercom.io/testB.png" - } + "name": "Admin B" } ] }