-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from jlsalmon/resource/organization
Organization support
- Loading branch information
Showing
7 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"organization": { | ||
"url": "https://example.zendesk.com/api/v2/organizations/361898904439.json", | ||
"id": 361898904439, | ||
"name": "Rebel Alliance", | ||
"shared_tickets": true, | ||
"shared_comments": true, | ||
"created_at": "2019-09-17T21:22:18Z", | ||
"updated_at": "2019-09-17T21:22:18Z", | ||
"domain_names": [ | ||
"hoth.com", | ||
"dantooine.com" | ||
], | ||
"group_id": null, | ||
"tags": [ | ||
"test" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"organization": { | ||
"url": "https://example.zendesk.com/api/v2/organizations/361898904439.json", | ||
"id": 361898904439, | ||
"name": "Rebel Alliance", | ||
"shared_tickets": true, | ||
"shared_comments": true, | ||
"created_at": "2019-09-17T21:22:18Z", | ||
"updated_at": "2019-09-17T21:22:18Z", | ||
"domain_names": [ | ||
"hoth.com", | ||
"dantooine.com" | ||
], | ||
"group_id": null, | ||
"tags": [ | ||
"test" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"organization": { | ||
"url": "https://example.zendesk.com/api/v2/organizations/361898904439.json", | ||
"id": 361898904439, | ||
"name": "Rebel Alliance", | ||
"shared_tickets": true, | ||
"shared_comments": true, | ||
"created_at": "2019-09-17T21:22:18Z", | ||
"updated_at": "2019-09-17T21:22:18Z", | ||
"domain_names": [ | ||
"hoth.com", | ||
"dantooine.com" | ||
], | ||
"group_id": null, | ||
"tags": [ | ||
"test" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ type API interface { | |
TriggerAPI | ||
TargetAPI | ||
UserAPI | ||
OrganizationAPI | ||
} | ||
|
||
var _ API = (*Client)(nil) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package zendesk | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// Organization is struct for organization payload | ||
// https://developer.zendesk.com/rest_api/docs/support/organizations | ||
type Organization struct { | ||
ID int64 `json:"id,omitempty"` | ||
URL string `json:"url,omitempty"` | ||
Name string `json:"name"` | ||
DomainNames []string `json:"domain_names"` | ||
GroupID int64 `json:"group_id"` | ||
SharedTickets bool `json:"shared_tickets"` | ||
SharedComments bool `json:"shared_comments"` | ||
Tags []string `json:"tags"` | ||
CreatedAt time.Time `json:"created_at,omitempty"` | ||
UpdatedAt time.Time `json:"updated_at,omitempty"` | ||
} | ||
|
||
// OrganizationAPI an interface containing all methods associated with zendesk organizations | ||
type OrganizationAPI interface { | ||
CreateOrganization(ctx context.Context, org Organization) (Organization, error) | ||
GetOrganization(ctx context.Context, orgID int64) (Organization, error) | ||
UpdateOrganization(ctx context.Context, orgID int64, org Organization) (Organization, error) | ||
DeleteOrganization(ctx context.Context, orgID int64) error | ||
} | ||
|
||
// CreateOrganization creates new organization | ||
// https://developer.zendesk.com/rest_api/docs/support/organizations#create-organization | ||
func (z *Client) CreateOrganization(ctx context.Context, org Organization) (Organization, error) { | ||
var data, result struct { | ||
Organization Organization `json:"organization"` | ||
} | ||
|
||
data.Organization = org | ||
|
||
body, err := z.post(ctx, "/organizations.json", data) | ||
if err != nil { | ||
return Organization{}, err | ||
} | ||
|
||
err = json.Unmarshal(body, &result) | ||
if err != nil { | ||
return Organization{}, err | ||
} | ||
|
||
return result.Organization, nil | ||
} | ||
|
||
// GetOrganization gets a specified organization | ||
// ref: https://developer.zendesk.com/rest_api/docs/support/organizations#show-organization | ||
func (z *Client) GetOrganization(ctx context.Context, orgID int64) (Organization, error) { | ||
var result struct { | ||
Organization Organization `json:"organization"` | ||
} | ||
|
||
body, err := z.get(ctx, fmt.Sprintf("/organizations/%d.json", orgID)) | ||
|
||
if err != nil { | ||
return Organization{}, err | ||
} | ||
|
||
err = json.Unmarshal(body, &result) | ||
if err != nil { | ||
return Organization{}, err | ||
} | ||
|
||
return result.Organization, err | ||
} | ||
|
||
// UpdateOrganization updates a organization with the specified organization | ||
// ref: https://developer.zendesk.com/rest_api/docs/support/organizations#update-organization | ||
func (z *Client) UpdateOrganization(ctx context.Context, orgID int64, org Organization) (Organization, error) { | ||
var result, data struct { | ||
Organization Organization `json:"organization"` | ||
} | ||
|
||
data.Organization = org | ||
|
||
body, err := z.put(ctx, fmt.Sprintf("/organizations/%d.json", orgID), data) | ||
|
||
if err != nil { | ||
return Organization{}, err | ||
} | ||
|
||
err = json.Unmarshal(body, &result) | ||
if err != nil { | ||
return Organization{}, err | ||
} | ||
|
||
return result.Organization, err | ||
} | ||
|
||
// DeleteOrganization deletes the specified organization | ||
// ref: https://developer.zendesk.com/rest_api/docs/support/organizations#delete-organization | ||
func (z *Client) DeleteOrganization(ctx context.Context, orgID int64) error { | ||
err := z.delete(ctx, fmt.Sprintf("/organizations/%d.json", orgID)) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package zendesk | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestCreateOrganization(t *testing.T) { | ||
mockAPI := newMockAPIWithStatus(http.MethodPost, "organization.json", http.StatusCreated) | ||
client := newTestClient(mockAPI) | ||
defer mockAPI.Close() | ||
|
||
_, err := client.CreateOrganization(ctx, Organization{}) | ||
if err != nil { | ||
t.Fatalf("Failed to send request to create organization: %s", err) | ||
} | ||
} | ||
|
||
func TestGetOrganization(t *testing.T) { | ||
mockAPI := newMockAPI(http.MethodGet, "organization.json") | ||
client := newTestClient(mockAPI) | ||
defer mockAPI.Close() | ||
|
||
org, err := client.GetOrganization(ctx, 123) | ||
if err != nil { | ||
t.Fatalf("Failed to get organization: %s", err) | ||
} | ||
|
||
expectedID := int64(361898904439) | ||
if org.ID != expectedID { | ||
t.Fatalf("Returned organization does not have the expected ID %d. Organization ID is %d", expectedID, org.ID) | ||
} | ||
} | ||
|
||
func TestUpdateOrganization(t *testing.T) { | ||
mockAPI := newMockAPIWithStatus(http.MethodPut, "organization.json", http.StatusOK) | ||
client := newTestClient(mockAPI) | ||
defer mockAPI.Close() | ||
|
||
updatedOrg, err := client.UpdateOrganization(ctx, int64(1234), Organization{}) | ||
if err != nil { | ||
t.Fatalf("Failed to send request to create organization: %s", err) | ||
} | ||
|
||
expectedID := int64(361898904439) | ||
if updatedOrg.ID != expectedID { | ||
t.Fatalf("Updated organization %v did not have expected id %d", updatedOrg, expectedID) | ||
} | ||
} | ||
|
||
func TestDeleteOrganization(t *testing.T) { | ||
mockAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusNoContent) | ||
w.Write(nil) | ||
})) | ||
|
||
c := newTestClient(mockAPI) | ||
err := c.DeleteOrganization(ctx, 1234) | ||
if err != nil { | ||
t.Fatalf("Failed to delete organization: %s", err) | ||
} | ||
} |