Skip to content
This repository has been archived by the owner on Mar 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #26 from onizuka8/feat/identity
Browse files Browse the repository at this point in the history
Add support for User Identity
  • Loading branch information
otoyo authored Aug 6, 2022
2 parents 1ff751c + 858584a commit d4c7164
Show file tree
Hide file tree
Showing 5 changed files with 380 additions and 2 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# zen\_ex
# zen_ex

[Zendesk REST API](https://developer.zendesk.com/rest_api) client for Elixir

zen\_ex is composed of Models and Entities. See [Usage](#usage).
zen_ex is composed of Models and Entities. See [Usage](#usage).

## Installation

Expand Down Expand Up @@ -58,12 +58,14 @@ ticket = ZenEx.Model.Ticket.create(%ZenEx.Entity.Ticket{subject: "My printer is
See also under ZenEx.Model.

## Supporting multiple Zendesk configs

You may need to interact with more than one instance of Zendesk. In order to facilitate that there is a small override
that can be put into the Process dictionary that will tell it to look for config settings keyed against a class name.

For example:

config/config.exs

```
config :zen_ex,
subdomain: System.get_env("ZENDESK_SUBDOMAIN"),
Expand Down Expand Up @@ -109,6 +111,15 @@ default `zen_ex` config settings.
- `create_or_update_many`
- `destroy_many`
- `search`
- User Identities
- `list`
- `show`
- `create`
- `update`
- `make_primary`
- `verify`
- `request_user_verification`
- `delete`
- Tickets
- `list`
- `show`
Expand Down
20 changes: 20 additions & 0 deletions lib/zen_ex/core/entities/user_identity.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule ZenEx.Entity.UserIdentity do
@derive Jason.Encoder
defstruct [
:id,
:created_at,
:deliverable_state,
:primary,
:type,
:undeliverable_count,
:updated_at,
:url,
:user_id,
:value,
:verified
]

@moduledoc """
User Identity entity corresponding to Zendesk User Identity
"""
end
139 changes: 139 additions & 0 deletions lib/zen_ex/core/models/user_identity.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
defmodule ZenEx.Model.UserIdentity do
alias ZenEx.HTTPClient
alias ZenEx.Query
alias ZenEx.Entity.{User, UserIdentity}

@moduledoc """
Provides functions to operate Zendesk User Identities.
"""

@doc """
List user's identities.
## Examples
iex> ZenEx.Model.UserIdentity.list(%ZenEx.Entity.User{id: xxx})
%ZenEx.Collection{}
"""
@spec list(%User{}) :: %ZenEx.Collection{} | {:error, String.t()}
def list(%User{id: user_id}, opts \\ []) when is_list(opts) do
"/api/v2/users/#{user_id}/identities#{Query.build(opts)}"
|> HTTPClient.get(identities: [UserIdentity])
end

@doc """
Show user's identity specified by id.
## Examples
iex> ZenEx.Model.UserIdentity.show(%ZenEx.Entity.User{id: xxx}, 1)
%ZenEx.Entity.UserIdentity{id: 1, type: "xxx", ...}
"""
@spec show(%User{}, integer) :: %UserIdentity{} | {:error, String.t()}
def show(%User{id: user_id}, id) when is_integer(id) do
HTTPClient.get("/api/v2/users/#{user_id}/identities/#{id}.json", identity: UserIdentity)
end

@doc """
Create user's identity.
## Examples
iex> ZenEx.Model.UserIdentity.create(%ZenEx.Entity.User{id: xxx}, %ZenEx.Entity.UserIdentity{type: "xxx", value: "xxx"})
%ZenEx.Entity.UserIdentity{id: xxx, value: "xxx", ...}
"""
@spec create(%User{}, %UserIdentity{}) :: %UserIdentity{} | {:error, String.t()} | nil
def create(%User{id: user_id}, %UserIdentity{} = identity) do
HTTPClient.post("/api/v2/users/#{user_id}/identities.json", %{identity: identity},
identity: UserIdentity
)
end

@doc """
Update user's identity specified by id.
## Examples
iex> ZenEx.Model.UserIdentity.update(%ZenEx.Entity.User{id: 1}, %ZenEx.Entity.UserIdentity{id: xxx, type: "xxx"})
%ZenEx.Entity.UserIdentity{id: 1, type: "xxx", ...}
"""
@spec update(%User{}, %UserIdentity{}) :: %UserIdentity{} | {:error, String.t()}
def update(%User{id: user_id}, %UserIdentity{} = identity) do
HTTPClient.put(
"/api/v2/users/#{user_id}/identities/#{identity.id}.json",
%{identity: identity},
identity: UserIdentity
)
end

@doc """
Make primary user's identity specified by id.
## Examples
iex> ZenEx.Model.UserIdentity.make_primary(%ZenEx.Entity.User{id: 1}, 1)
%ZenEx.Collection{}
"""
@spec make_primary(%User{}, integer) :: %ZenEx.Collection{} | {:error, String.t()}
def make_primary(%User{id: user_id}, id) when is_integer(id) do
HTTPClient.put("/api/v2/users/#{user_id}/identities/#{id}/make_primary", %{},
identities: [UserIdentity]
)
end

@doc """
Verify user's identity specified by id.
## Examples
iex> ZenEx.Model.UserIdentity.verify(%ZenEx.Entity.User{id: 1}, 1)
:ok
"""
@spec verify(%User{}, integer) :: :ok | :error
def verify(%User{id: user_id}, id) when is_integer(id) do
case HTTPClient.put("/api/v2/users/#{user_id}/identities/#{id}/verify", %{}).status do
200 -> :ok
_ -> :error
end
end

@doc """
Request user verification for user's identity specified by id.
## Examples
iex> ZenEx.Model.UserIdentity.request_user_verification(%ZenEx.Entity.User{id: 1}, 1)
%ZenEx.Collection{}
"""
@spec request_user_verification(%User{}, integer) :: :ok | :error
def request_user_verification(%User{id: user_id}, id) when is_integer(id) do
case HTTPClient.put("/api/v2/users/#{user_id}/identities/#{id}/request_verification", %{}).status do
200 -> :ok
_ -> :error
end
end

@doc """
Delete user's identity specified by id.
## Examples
iex> ZenEx.Model.UserIdentity.destroy(%ZenEx.Entity.User{id: 1}, 1)
:ok
"""
@spec destroy(%User{}, integer) :: :ok | :error
def destroy(%User{id: user_id}, id) when is_integer(id) do
case HTTPClient.delete("/api/v2/users/#{user_id}/identities/#{id}.json").status do
204 -> :ok
_ -> :error
end
end
end
6 changes: 6 additions & 0 deletions lib/zen_ex/http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ defmodule ZenEx.HTTPClient do
end
end

def put(endpoint, %{} = param) do
with {:ok, response} <- Tesla.put(auth_conn(), build_url(endpoint), param) do
response
end
end

def delete(endpoint, decode_as), do: delete(endpoint) |> _build_entity(decode_as)

def delete(endpoint) do
Expand Down
Loading

0 comments on commit d4c7164

Please sign in to comment.