Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Replace JSX with Jason #189

Merged
merged 9 commits into from
May 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ using:

```elixir
# To have Atom keys
config :tentacat, :deserialization_options, [labels: :atoms]
config :tentacat, :deserialization_options, [keys: :atoms]
```

See: https://github.com/talentdeficit/exjsx#decodejson-opts for available options.
See: https://hexdocs.pm/jason/Jason.html#decode/2-options for available options.

## Contributing

Expand Down
3 changes: 1 addition & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use Mix.Config

config :exvcr, [
config :exvcr,
vcr_cassette_library_dir: "test/fixture/vcr_cassettes",
filter_sensitive_data: [
[pattern: "token [^\"]+", placeholder: "token yourtokencomeshere"]
]
]
2 changes: 1 addition & 1 deletion guides/Getting Started.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ config :tentacat,
request_options: []
```

* `:deserialization_options`: List of options to be passed through `JSX.decode!/2` to `:jsx`. See [the :jsx documentation](https://github.com/talentdeficit/exjsx#decodejson-opts) for more information.
* `:deserialization_options`: List of options to be passed through `Jason.decode!/2`. See [the Jason documentation](https://hexdocs.pm/jason/Jason.html#decode/2-options) for more information.
* `:extra_headers`: List of two-element tuples defining additional HTTP headers for `HTTPoison.request/5`. For alternative ways of specifying headers, see `t:HTTPoison.Base.headers/0`.
* `:pagination`: Specifies the default method of pagination (can also be set on a per-request basis). See `Tentacat.get/4` for more information.
* `:request_options`: Keyword list of options to be passed to `HTTPoison.request/5`.
Expand Down
30 changes: 23 additions & 7 deletions lib/tentacat.ex
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
defmodule Tentacat do
use HTTPoison.Base
alias Tentacat.Client
alias Jason

@user_agent [{"User-agent", "tentacat"}]

@type response ::
{:ok, :jsx.json_term(), HTTPoison.Response.t()}
{:ok, term, HTTPoison.Response.t()}
| {integer, any, HTTPoison.Response.t()}
| pagination_response

@type pagination_response :: {response, binary | nil, Client.auth()}

defimpl Jason.Encoder, for: Tuple do
def encode(tuple, opts) when is_tuple(tuple) do
[tuple]
|> Enum.into(%{})
|> Jason.Encode.map(opts)
end
end

@spec process_response_body(binary) :: term
def process_response_body(""), do: nil
def process_response_body(body), do: JSX.decode!(body, deserialization_options())
def process_response_body(body), do: Jason.decode!(body, deserialization_options())

@spec process_response(HTTPoison.Response.t() | {integer, any, HTTPoison.Response.t()}) :: response
@spec process_response(HTTPoison.Response.t() | {integer, any, HTTPoison.Response.t()}) ::
response
def process_response(%HTTPoison.Response{status_code: status_code, body: body} = resp),
do: {status_code, body, resp}

def process_response({_status_code, _, %HTTPoison.Response{} = resp}),
do: process_response(resp)

Expand Down Expand Up @@ -83,7 +94,7 @@ defmodule Tentacat do

@spec json_request(atom, binary, any, keyword, keyword) :: response
def json_request(method, url, body \\ "", headers \\ [], options \\ []) do
raw_request(method, url, JSX.encode!(body), headers, options)
raw_request(method, url, Jason.encode!(body), headers, options)
end

defp extra_options do
Expand Down Expand Up @@ -112,7 +123,7 @@ defmodule Tentacat do
@spec request_stream(atom, binary, Client.auth(), any, :one_page | nil | :stream) ::
Enumerable.t() | response
def request_stream(method, url, auth, body \\ "", override \\ nil) do
request_with_pagination(method, url, auth, JSX.encode!(body))
request_with_pagination(method, url, auth, Jason.encode!(body))
|> stream_if_needed(override)
end

Expand Down Expand Up @@ -152,7 +163,7 @@ defmodule Tentacat do
request!(
method,
url,
JSX.encode!(body),
Jason.encode!(body),
authorization_header(auth, extra_headers() ++ @user_agent),
extra_options()
)
Expand All @@ -166,16 +177,21 @@ defmodule Tentacat do
end
end

@spec build_pagination_response(HTTPoison.Response.t() | {integer, any, HTTPoison.Response.t()}, Client.auth()) :: pagination_response
@spec build_pagination_response(
HTTPoison.Response.t() | {integer, any, HTTPoison.Response.t()},
Client.auth()
) :: pagination_response
defp build_pagination_response(%HTTPoison.Response{:headers => headers} = resp, auth) do
{process_response(resp), next_link(headers), auth}
end

defp build_pagination_response({_, _, %HTTPoison.Response{} = resp}, auth) do
build_pagination_response(resp, auth)
end

defp location_header({_, _, resp}),
do: location_header(resp)

defp location_header(resp) do
[{"Location", url}] = Enum.filter(resp.headers, &match?({"Location", _}, &1))
url
Expand Down
2 changes: 1 addition & 1 deletion lib/tentacat/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Tentacat.Client do

@spec new(map(), binary) :: t
def new(auth = %{jwt: _}, endpoint) do
pnew(auth, endpoint)
pnew(auth, endpoint)
end

@spec new(auth, binary) :: t
Expand Down
1 change: 0 additions & 1 deletion lib/tentacat/repositories/contents.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ defmodule Tentacat.Repositories.Contents do
def content(client \\ %Client{}, owner, repo, path) do
get("repos/#{owner}/#{repo}/contents/#{path}", client)
end

end
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ defmodule Tentacat.Mixfile do
end

def application do
[applications: [:httpoison, :exjsx]]
[applications: [:httpoison]]
end

defp deps do
[
{:httpoison, "~> 1.0"},
{:exjsx, "~> 4.0"},
{:jason, "~> 1.2"},
{:earmark, "~> 1.2", only: :dev},
{:ex_doc, "~> 0.19", only: :dev},
{:inch_ex, "~> 0.5", only: :dev},
Expand Down
3 changes: 1 addition & 2 deletions test/client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ defmodule Tentacat.ClientTest do
test "custom endpoint" do
expected = "https://ghe.foo.com/api/v3/"

creds = %{user: "bob", password: "bob"}
creds = %{user: "bob", password: "bob"}

client = new(creds, "https://ghe.foo.com/api/v3/")
assert client.endpoint == expected


client = new("https://ghe.foo.com/api/v3/")
assert client.endpoint == expected

Expand Down
2 changes: 1 addition & 1 deletion test/fixture/vcr_cassettes/gists#fork.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"url": "https://api.github.com/gists/0feff8bfe771b85ee0c3840afd78d177/forks"
},
"response": {
"body": "{'url':'https://api.github.com/gists/aa5a315d61ae9438b18d','forks_url':'https://api.github.com/gists/aa5a315d61ae9438b18d/forks','commits_url':'https://api.github.com/gists/aa5a315d61ae9438b18d/commits','id':'aa5a315d61ae9438b18d','node_id':'MDQ6R2lzdGFhNWEzMTVkNjFhZTk0MzhiMThk','git_pull_url':'https://gist.github.com/aa5a315d61ae9438b18d.git','git_push_url':'https://gist.github.com/aa5a315d61ae9438b18d.git','html_url':'https://gist.github.com/aa5a315d61ae9438b18d','files':{'hello_world.rb':{'filename':'hello_world.rb','type':'application/x-ruby','language':'Ruby','raw_url':'https://gist.githubusercontent.com/octocat/6cad326836d38bd3a7ae/raw/db9c55113504e46fa076e7df3a04ce592e2e86d8/hello_world.rb','size':167}},'public':true,'created_at':'2010-04-14T02:15:15Z','updated_at':'2011-06-20T11:34:15Z','description':'Hello World Examples','comments':0,'user':null,'comments_url':'https://api.github.com/gists/aa5a315d61ae9438b18d/comments/','owner':{'login':'octocat','id':1,'node_id':'MDQ6VXNlcjE=','avatar_url':'https://github.com/images/error/octocat_happy.gif','gravatar_id':'','url':'https://api.github.com/users/octocat','html_url':'https://github.com/octocat','followers_url':'https://api.github.com/users/octocat/followers','following_url':'https://api.github.com/users/octocat/following{/other_user}','gists_url':'https://api.github.com/users/octocat/gists{/gist_id}','starred_url':'https://api.github.com/users/octocat/starred{/owner}{/repo}','subscriptions_url':'https://api.github.com/users/octocat/subscriptions','organizations_url':'https://api.github.com/users/octocat/orgs','repos_url':'https://api.github.com/users/octocat/repos','events_url':'https://api.github.com/users/octocat/events{/privacy}','received_events_url':'https://api.github.com/users/octocat/received_events','type':'User','site_admin':false},'truncated':false}",
"body": "{\"comments\":0,\"comments_url\":\"https://api.github.com/gists/aa5a315d61ae9438b18d/comments/\",\"commits_url\":\"https://api.github.com/gists/aa5a315d61ae9438b18d/commits\",\"created_at\":\"2010-04-14T02:15:15Z\",\"description\":\"Hello World Examples\",\"files\":{\"hello_world.rb\":{\"filename\":\"hello_world.rb\",\"language\":\"Ruby\",\"raw_url\":\"https://gist.githubusercontent.com/octocat/6cad326836d38bd3a7ae/raw/db9c55113504e46fa076e7df3a04ce592e2e86d8/hello_world.rb\",\"size\":167,\"type\":\"application/x-ruby\"}},\"forks_url\":\"https://api.github.com/gists/aa5a315d61ae9438b18d/forks\",\"git_pull_url\":\"https://gist.github.com/aa5a315d61ae9438b18d.git\",\"git_push_url\":\"https://gist.github.com/aa5a315d61ae9438b18d.git\",\"html_url\":\"https://gist.github.com/aa5a315d61ae9438b18d\",\"id\":\"aa5a315d61ae9438b18d\",\"node_id\":\"MDQ6R2lzdGFhNWEzMTVkNjFhZTk0MzhiMThk\",\"owner\":{\"avatar_url\":\"https://github.com/images/error/octocat_happy.gif\",\"events_url\":\"https://api.github.com/users/octocat/events{/privacy}\",\"followers_url\":\"https://api.github.com/users/octocat/followers\",\"following_url\":\"https://api.github.com/users/octocat/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/octocat/gists{/gist_id}\",\"gravatar_id\":\"\",\"html_url\":\"https://github.com/octocat\",\"id\":1,\"login\":\"octocat\",\"node_id\":\"MDQ6VXNlcjE=\",\"organizations_url\":\"https://api.github.com/users/octocat/orgs\",\"received_events_url\":\"https://api.github.com/users/octocat/received_events\",\"repos_url\":\"https://api.github.com/users/octocat/repos\",\"site_admin\":false,\"starred_url\":\"https://api.github.com/users/octocat/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/octocat/subscriptions\",\"type\":\"User\",\"url\":\"https://api.github.com/users/octocat\"},\"public\":true,\"truncated\":false,\"updated_at\":\"2011-06-20T11:34:15Z\",\"url\":\"https://api.github.com/gists/aa5a315d61ae9438b18d\",\"user\":null}",
"headers": {
"Server": "github.com",
"Date": "Thu, 10 Dec 2015 22:17:43 GMT",
Expand Down
Loading