forked from edgurgel/tentacat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
172 additions
and
173 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
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 |
---|---|---|
@@ -1,5 +1,96 @@ | ||
defmodule Tentacat do | ||
defrecord Client, auth: nil do | ||
record_type auth: [user: binary, password: binary] | [access_token: binary] | ||
use HTTPoison.Base | ||
|
||
defmodule Client do | ||
defstruct auth: nil :: %{user: binary, password: binary} | %{access_token: binary} | ||
end | ||
|
||
@user_agent [{"User-agent", "tentacat"}] | ||
|
||
@type response :: {integer, any} | :jsx.json_term | ||
|
||
def process_url(url) do | ||
"https://api.github.com/" <> url | ||
end | ||
|
||
@spec process_response(HTTPoison.Response.t) :: response | ||
def process_response(response) do | ||
status_code = response.status_code | ||
headers = response.headers | ||
body = response.body | ||
response = unless body == "", do: body |> JSEX.decode!, | ||
else: nil | ||
|
||
if (status_code == 200), do: response, | ||
else: {status_code, response} | ||
end | ||
|
||
def delete(url, auth \\ nil, body \\ "") do | ||
_request(:delete, url, auth, body) | ||
end | ||
|
||
def post(url, auth \\ nil, body \\ "") do | ||
_request(:post, url, auth, body) | ||
end | ||
|
||
def patch(url, auth \\ nil, body \\ "") do | ||
_request(:patch, url, auth, body) | ||
end | ||
|
||
def get(url, auth \\ nil, params \\ []) do | ||
url = <<url :: binary, build_qs(params) :: binary>> | ||
_request(:get, url, auth) | ||
end | ||
|
||
def _request(method, url, auth, body \\ "") do | ||
json_request(method, url, body, authorization_header(auth, @user_agent)) | ||
end | ||
|
||
def json_request(method, url, body \\ "", headers \\ [], options \\ []) do | ||
request(method, url, JSEX.encode!(body), headers, options) |> process_response | ||
end | ||
|
||
def raw_request(method, url, body \\ "", headers \\ [], options \\ []) do | ||
request(method, url, body, headers, options) |> process_response | ||
end | ||
|
||
@spec build_qs([{atom, binary}]) :: binary | ||
defp build_qs([]), do: "" | ||
defp build_qs(kvs), do: to_string('?' ++ URI.encode_query(kvs)) | ||
|
||
@doc """ | ||
There are two ways to authenticate through GitHub API v3: | ||
* Basic authentication | ||
* OAuth2 Token | ||
This function accepts both. | ||
## Examples | ||
iex> Tentacat.authorization_header(%{user: "user", password: "password"}, []) | ||
[{"Authorization", "Basic dXNlcjpwYXNzd29yZA=="}] | ||
iex> Tentacat.authorization_header(%{access_token: "92873971893"}, []) | ||
[{"Authorization", "token 92873971893"}] | ||
## More info | ||
http:\\developer.github.com/v3/#authentication | ||
""" | ||
@spec authorization_header(Client.auth, list) :: list | ||
def authorization_header(%{user: user, password: password}, headers) do | ||
userpass = "#{user}:#{password}" | ||
headers ++ [{"Authorization", "Basic #{:base64.encode(userpass)}"}] | ||
end | ||
|
||
def authorization_header(%{access_token: token}, headers) do | ||
headers ++ [{"Authorization", "token #{token}"}] | ||
end | ||
|
||
def authorization_header(_, headers), do: headers | ||
|
||
@doc """ | ||
Same as `authorization_header/2` but defaults initial headers to include `@user_agent`. | ||
""" | ||
def authorization_header(options), do: authorization_header(options, @user_agent) | ||
end |
This file was deleted.
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
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
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
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
Oops, something went wrong.