ex_zendesk v0.1.0 ExZendesk

Simple HTTPoison based helper library to interact with the Zendesk API

Configuration

First you must configure the subdomain, username, and password. The library will check environment variables first and fall back to application configuration.

This library currently supports Basic authentication or API token authentication.

  • subdomain, taken from ZENDESK_SUBDOMAIN environment variable or Application.get_env(:ex_zendesk, :subdomain). This is your zendesk base url, such as https://obscura.zendesk.com

  • user, taken from ZENDESK_USER environment variable or Application.get_env(:ex_zendesk, :user). If using basic authentication, this is your zendesk login (ex. user@gmail.com). If using api token authentication this is your zendesk login followed by /token (ex. user@gmail.com/token)

  • password, taken from ZENDESK_PASSWORD environment variable or Application.get_env(:ex_zendesk, :password). If using basic authentication this is your zendesk password. If using api token authentication put your api token here.

Examples

ExZendesk.start

ExZendesk.get "/api/v2/tickets.json?per_page=1"
  # => {:ok,
  # %HTTPoison.Response{body: %{count: 114,
  #  next_page: "https://storestartup.zendesk.com/api/v2/tickets.json?page=2&per_page=1",
  #  previous_page: nil,
  #  tickets: [%{allow_channelback: false, assignee_id: 1802891567,
  #     brand_id: 572847, collaborator_ids: [],
  #     created_at: "2016-03-01T07:03:12Z", custom_fields: [],
  #     description: "Hello,
This is a test ticket",
  #     due_at: nil, external_id: nil, fields: [], followup_ids: [],
  #     forum_topic_id: nil, group_id: 26814467, has_incidents: false, id: 38,
  #     organization_id: nil, priority: nil, problem_id: nil,
  #     raw_subject: "Test ticket", recipient: nil,
  #     requester_id: 5022882178, satisfaction_rating: nil,
  #     sharing_agreement_ids: [], status: "closed",
  #     subject: "Test Ticket",
  #     submitter_id: 5022882178, tags: [], type: nil,
  #     updated_at: "2016-05-07T17:01:56Z",
  #     url: "https://storestartup.zendesk.com/api/v2/tickets/38.json",
  #     via: %{channel: "web", source: %{from: %{}, rel: nil, to: %{}}}}]},
  # headers: [{"Server", "nginx"}, {"Date", "Fri, 12 Aug 2016 16:33:01 GMT"},
  # {"Content-Type", "application/json; charset=UTF-8"},
  # {"Content-Length", "1291"}, {"Connection", "keep-alive"},
  # {"X-Zendesk-API-Version", "v2"},
  # {"X-Zendesk-Application-Version", "v3.94.2"},
  # {"X-Zendesk-API-Warn",
  #  "Removed restricted keys ["_json"] from parameters according to whitelist"},
  # {"X-Frame-Options", "SAMEORIGIN"}, {"X-Rate-Limit", "700"},
  # {"X-Rate-Limit-Remaining", "699"},
  # {"Strict-Transport-Security", "max-age=31536000;"},
  # {"X-UA-Compatible", "IE=Edge,chrome=1"},
  # {"ETag", ""8cd9c3920ce86f5b21141e6bcef72358""},
  # {"Cache-Control", "must-revalidate, private, max-age=0"},
  # {"X-Zendesk-Origin-Server", "app41.pod6.iad1.zdsys.com"},
  # {"X-Request-Id", "5aa337af-21ba-4ef0-ce53-b8ca3a6bb1dc"},
  # {"X-Runtime", "0.188008"}, {"X-Rack-Cache", "miss"},
  # {"X-Zendesk-Request-Id", "1fcdee3a31262d9d3064"},
  # {"X-Content-Type-Options", "nosniff"}], status_code: 200}}

sample_ticket = %{ticket: %{subject: "testing", comment: %{body: "this is a test"}, requester: %{name: "John Doe", email: "jdoe@gmail.com"}}}

ExZendesk.post "/api/v2/tickets.json", sample_ticket
  # => {:ok,
  # %HTTPoison.Response{body: %{audit: %{author_id: 1802891567,
  #    created_at: "2016-08-12T16:40:14Z",
  #    events: [%{attachments: [], audit_id: 145382499768, author_id: 9343852848,
  #       body: "this is a test",
  #       html_body: "<div class="zd-comment"><p dir="auto">this is a test</p></div>",
  #       id: 145382499848, public: true, type: "Comment"},
  #     %{field_name: "subject", id: 145382500008, type: "Create",
  #       value: "testing"},
  #     %{field_name: "status", id: 145382500068, type: "Create", value: "open"},
  #     %{field_name: "priority", id: 145382500148, type: "Create", value: nil},
  #     %{field_name: "type", id: 145382500188, type: "Create", value: nil},
  #     %{field_name: "assignee_id", id: 145382500248, type: "Create",
  #       value: "1802891567"},
  #     %{field_name: "group_id", id: 145382500288, type: "Create",
  #       value: "26814467"},
  #     %{field_name: "requester_id", id: 145382500368, type: "Create",
  #       value: "9343852848"},
  #     %{body: "Your request ({{ticket.id}}) has been received and is being reviewed by our support staff.

To add additional # # comments, reply to this email.

{{ticket.comments_formatted}}",
  #       id: 145382500448, recipients: [9343852848],
  #       subject: "[Request received] {{ticket.title}}", type: "Notification",
  #       via: %{channel: "rule",
  #         source: %{from: %{id: 64041887,
  #             title: "Notify requester of received request"}, rel: "trigger",
  #           to: %{}}}},
  #     %{body: "A ticket (#{{ticket.id}}) by {{ticket.requester.name}} has been received. It is unassigned.

{{ticket.comments_formatted}}",
  #       id: 145382500608, recipients: [1802891567],
  #       subject: "[{{ticket.account}}] {{ticket.title}}", type: "Notification",
  #       via: %{channel: "rule",
  #         source: %{from: %{id: 64041947,
  #             title: "Notify all agents of received request"}, rel: "trigger",
  #           to: %{}}}}], id: 145382499768,
  #    metadata: %{custom: %{},
  #      system: %{client: "Elixir/ExZendesk", ip_address: "173.65.209.214",
  #        latitude: 27.88669999999999, location: "Tampa, FL, United States",
  #        longitude: -82.5117}}, ticket_id: 173,
  #    via: %{channel: "api", source: %{from: %{}, rel: nil, to: %{}}}},
  #  ticket: %{allow_channelback: false, assignee_id: 1802891567,
  #    brand_id: 572847, collaborator_ids: [],
  #    created_at: "2016-08-12T16:40:14Z", custom_fields: [],
  #    description: "this is a test", due_at: nil, external_id: nil, fields: [],
  #    forum_topic_id: nil, group_id: 26814467, has_incidents: false, id: 173,
  #    organization_id: nil, priority: nil, problem_id: nil,
  #    raw_subject: "testing", recipient: nil, requester_id: 9343852848,
  #    satisfaction_rating: nil, sharing_agreement_ids: [], status: "open",
  #    subject: "testing", submitter_id: 9343852848, tags: [], type: nil,
  #    updated_at: "2016-08-12T16:40:14Z",
  #    url: "https://storestartup.zendesk.com/api/v2/tickets/173.json",
  #    via: %{channel: "api", source: %{from: %{}, rel: nil, to: %{}}}}},
  # headers: [{"Server", "nginx"}, {"Date", "Fri, 12 Aug 2016 16:40:14 GMT"},
  # {"Content-Type", "application/json; charset=UTF-8"},
  # {"Content-Length", "2665"}, {"Connection", "keep-alive"},
  # {"X-Zendesk-API-Version", "v2"},
  # {"X-Zendesk-Application-Version", "v3.94.2"},
  # {"X-Frame-Options", "SAMEORIGIN"},
  # {"Location", "https://partially.zendesk.com/api/v2/tickets/173.json"},
  # {"X-Rate-Limit", "700"}, {"X-Rate-Limit-Remaining", "699"},
  # {"Strict-Transport-Security", "max-age=31536000;"},
  # {"X-UA-Compatible", "IE=Edge,chrome=1"},
  # {"ETag", ""71ffe41cfa9ce1e92dfd15a1de13822d""},
  # {"Cache-Control", "max-age=0, private, must-revalidate"},
  # {"X-Zendesk-Origin-Server", "app44.pod6.iad1.zdsys.com"},
  # {"X-Request-Id", "deeaf0fd-e352-4102-c1e6-b8ca3a6bbce0"},
  # {"X-Runtime", "0.679025"}, {"X-Rack-Cache", "invalidate, pass"},
  # {"X-Zendesk-Request-Id", "4e1deda0879800940aeb"},
  # {"X-Content-Type-Options", "nosniff"}], status_code: 201}}

Summary

Functions

Issues a DELETE request to the given url

Issues a DELETE request to the given url, raising an exception in case of failure

Issues a GET request to the given url

Issues a GET request to the given url, raising an exception in case of failure

Issues a HEAD request to the given url

Issues a HEAD request to the given url, raising an exception in case of failure

Issues an OPTIONS request to the given url

Issues a OPTIONS request to the given url, raising an exception in case of failure

Issues a PATCH request to the given url

Issues a PATCH request to the given url, raising an exception in case of failure

Issues a POST request to the given url

Issues a POST request to the given url, raising an exception in case of failure

Issues a PUT request to the given url

Issues a PUT request to the given url, raising an exception in case of failure

Issues an HTTP request with the given method to the given url

Issues an HTTP request with the given method to the given url, raising an exception in case of failure

Starts HTTPoison and its dependencies

Types

body ::
  binary |
  {:form, [{atom, any}]} |
  {:file, binary}
headers ::
  [{binary, binary}] |
  %{optional(binary) => binary}

Functions

delete(url, headers \\ [], options \\ [])

Specs

delete(binary, headers, Keyword.t) ::
  {:ok, HTTPoison.Response.t | HTTPoison.AsyncResponse.t} |
  {:error, HTTPoison.Error.t}

Issues a DELETE request to the given url.

Returns {:ok, response} if the request is successful, {:error, reason} otherwise.

See request/5 for more detailed information.

delete!(url, headers \\ [], options \\ [])

Specs

delete!(binary, headers, Keyword.t) ::
  HTTPoison.Response.t |
  HTTPoison.AsyncResponse.t

Issues a DELETE request to the given url, raising an exception in case of failure.

If the request does not fail, the response is returned.

See request!/5 for more detailed information.

get(url, headers \\ [], options \\ [])

Specs

get(binary, headers, Keyword.t) ::
  {:ok, HTTPoison.Response.t | HTTPoison.AsyncResponse.t} |
  {:error, HTTPoison.Error.t}

Issues a GET request to the given url.

Returns {:ok, response} if the request is successful, {:error, reason} otherwise.

See request/5 for more detailed information.

get!(url, headers \\ [], options \\ [])

Specs

get!(binary, headers, Keyword.t) ::
  HTTPoison.Response.t |
  HTTPoison.AsyncResponse.t

Issues a GET request to the given url, raising an exception in case of failure.

If the request does not fail, the response is returned.

See request!/5 for more detailed information.

head(url, headers \\ [], options \\ [])

Specs

head(binary, headers, Keyword.t) ::
  {:ok, HTTPoison.Response.t | HTTPoison.AsyncResponse.t} |
  {:error, HTTPoison.Error.t}

Issues a HEAD request to the given url.

Returns {:ok, response} if the request is successful, {:error, reason} otherwise.

See request/5 for more detailed information.

head!(url, headers \\ [], options \\ [])

Specs

head!(binary, headers, Keyword.t) ::
  HTTPoison.Response.t |
  HTTPoison.AsyncResponse.t

Issues a HEAD request to the given url, raising an exception in case of failure.

If the request does not fail, the response is returned.

See request!/5 for more detailed information.

options(url, headers \\ [], options \\ [])

Specs

options(binary, headers, Keyword.t) ::
  {:ok, HTTPoison.Response.t | HTTPoison.AsyncResponse.t} |
  {:error, HTTPoison.Error.t}

Issues an OPTIONS request to the given url.

Returns {:ok, response} if the request is successful, {:error, reason} otherwise.

See request/5 for more detailed information.

options!(url, headers \\ [], options \\ [])

Specs

options!(binary, headers, Keyword.t) ::
  HTTPoison.Response.t |
  HTTPoison.AsyncResponse.t

Issues a OPTIONS request to the given url, raising an exception in case of failure.

If the request does not fail, the response is returned.

See request!/5 for more detailed information.

patch(url, body, headers \\ [], options \\ [])

Specs

patch(binary, body, headers, Keyword.t) ::
  {:ok, HTTPoison.Response.t | HTTPoison.AsyncResponse.t} |
  {:error, HTTPoison.Error.t}

Issues a PATCH request to the given url.

Returns {:ok, response} if the request is successful, {:error, reason} otherwise.

See request/5 for more detailed information.

patch!(url, body, headers \\ [], options \\ [])

Specs

patch!(binary, body, headers, Keyword.t) ::
  HTTPoison.Response.t |
  HTTPoison.AsyncResponse.t

Issues a PATCH request to the given url, raising an exception in case of failure.

If the request does not fail, the response is returned.

See request!/5 for more detailed information.

post(url, body, headers \\ [], options \\ [])

Specs

post(binary, body, headers, Keyword.t) ::
  {:ok, HTTPoison.Response.t | HTTPoison.AsyncResponse.t} |
  {:error, HTTPoison.Error.t}

Issues a POST request to the given url.

Returns {:ok, response} if the request is successful, {:error, reason} otherwise.

See request/5 for more detailed information.

post!(url, body, headers \\ [], options \\ [])

Specs

post!(binary, body, headers, Keyword.t) ::
  HTTPoison.Response.t |
  HTTPoison.AsyncResponse.t

Issues a POST request to the given url, raising an exception in case of failure.

If the request does not fail, the response is returned.

See request!/5 for more detailed information.

process_request_body(body)
process_request_headers(headers)
process_response_body(body)
process_url(url)
put(url, body, headers \\ [], options \\ [])

Specs

put(binary, body, headers, Keyword.t) ::
  {:ok, HTTPoison.Response.t | HTTPoison.AsyncResponse.t} |
  {:error, HTTPoison.Error.t}

Issues a PUT request to the given url.

Returns {:ok, response} if the request is successful, {:error, reason} otherwise.

See request/5 for more detailed information.

put!(url, body, headers \\ [], options \\ [])

Specs

put!(binary, body, headers, Keyword.t) ::
  HTTPoison.Response.t |
  HTTPoison.AsyncResponse.t

Issues a PUT request to the given url, raising an exception in case of failure.

If the request does not fail, the response is returned.

See request!/5 for more detailed information.

request(method, url, body \\ "", headers \\ [], options \\ [])

Specs

request(atom, binary, body, headers, Keyword.t) ::
  {:ok, HTTPoison.Response.t | HTTPoison.AsyncResponse.t} |
  {:error, HTTPoison.Error.t}

Issues an HTTP request with the given method to the given url.

This function is usually used indirectly by get/3, post/4, put/4, etc

Args:

  • method - HTTP method as an atom (:get, :head, :post, :put, :delete, etc.)
  • url - target url as a binary string or char list
  • body - request body. See more below
  • headers - HTTP headers as an orddict (e.g., [{"Accept", "application/json"}])
  • options - Keyword list of options

Body:

  • binary, char list or an iolist
  • {:form, [{K, V}, ...]} - send a form url encoded
  • {:file, "/path/to/file"} - send a file

Options:

  • :timeout - timeout to establish a connection, in milliseconds. Default is 8000
  • :recv_timeout - timeout used when receiving a connection. Default is 5000
  • :stream_to - a PID to stream the response to
  • :proxy - a proxy to be used for the request; it can be a regular url or a {Host, Proxy} tuple
  • :proxy_auth - proxy authentication {User, Password} tuple
  • :ssl - SSL options supported by the ssl erlang module
  • :follow_redirect - a boolean that causes redirects to be followed
  • :max_redirect - an integer denoting the maximum number of redirects to follow
  • :params - an enumerable consisting of two-item tuples that will be appended to the url as query string parameters

Timeouts can be an integer or :infinity

This function returns {:ok, response} or {:ok, async_response} if the request is successful, {:error, reason} otherwise.

Examples

request(:post, "https://my.website.com", "{\"foo\": 3}", [{"Accept", "application/json"}])
request!(method, url, body \\ "", headers \\ [], options \\ [])

Specs

request!(atom, binary, body, headers, Keyword.t) :: HTTPoison.Response.t

Issues an HTTP request with the given method to the given url, raising an exception in case of failure.

request!/5 works exactly like request/5 but it returns just the response in case of a successful request, raising an exception in case the request fails.

start()

Starts HTTPoison and its dependencies.