Skip to content

Commit

Permalink
Enable bare requests to be made from ExTrello:
Browse files Browse the repository at this point in the history
- get, post, put, delete requests can be constructed using simple function calls to
- signed parameter values removed from query params (this was bad, so now there's no worries there)
- correctly use :headers option for HTTPotion
- correctly use request body when using post, put, delete
  • Loading branch information
Christopher Yammine committed Aug 12, 2016
1 parent 1c76b1a commit 04b935b
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ TODO:
- [x] ~~Create & Edit Cards~~
- [x] ~~Implement own OAuth 1.0 library to remove dependency on `erlang-oauth` (or investigate existing solutions)~~
- [ ] Add models for label, checklist, ~~member~~, notification, ~~organization~~, session, token, ~~action~~
- [ ] Usage tutorial. (For now use: https://hexdocs.pm/ex_trello/0.4.1/ExTrello.html)
- [ ] Usage tutorial. (For now use: https://hexdocs.pm/ex_trello/0.4.2/ExTrello.html)
- [ ] Tests
- [ ] Pagination

Expand All @@ -25,7 +25,7 @@ TODO:
```elixir
def deps do
[
{:ex_trello, "~> 0.4.1"}
{:ex_trello, "~> 0.4.2"}
]
end
```
Expand Down
72 changes: 72 additions & 0 deletions lib/ex_trello.ex
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,78 @@ defmodule ExTrello do
@spec action(String.t) :: ExTrello.Model.Action.t
defdelegate action(action_id), to: ExTrello.API.Actions

@doc """
GET request to Trello
## Examples
ExTrello.get("boards/57ae3940f43e6d960e0c45da/boardStars")
"""
@spec get(String.t) :: String.t
defdelegate get(path), to: ExTrello.API.BareRequests

@doc """
GET request to Trello
## Examples
ExTrello.get("boards/57ae3940f43e6d960e0c45da/boardStars", filter: "mine")
"""
@spec get(String.t, Keyword.t) :: String.t
defdelegate get(path, params), to: ExTrello.API.BareRequests

@doc """
POST request to Trello with no body.
"""
@spec post(String.t) :: String.t
defdelegate post(path), to: ExTrello.API.BareRequests

@doc """
POST request to Trello
## Examples
ExTrello.post("boards/57ae3940f43e6d960e0c45da/lists", name: "Best List", pos: "top")
"""
@spec post(String.t, Keyword.t) :: String.t
defdelegate post(path, params), to: ExTrello.API.BareRequests

@doc """
PUT request to Trello with no body.
"""
@spec put(String.t) :: String.t
defdelegate put(path), to: ExTrello.API.BareRequests

@doc """
PUT request to Trello
## Examples
ExTrello.put("boards/57ae3940f43e6d960e0c45da/labelNames/blue", value: "Bluey")
"""
@spec put(String.t, Keyword.t) :: String.t
defdelegate put(path, params), to: ExTrello.API.BareRequests

@doc """
DELETE request to Trello with no body.
## Examples
ExTrello.delete("boards/57ae3940f43e6d960e0c45da/powerUps/calendar")
"""
@spec delete(String.t) :: String.t
defdelegate delete(path), to: ExTrello.API.BareRequests

@doc """
DELETE request to Trello
## Examples
ExTrello.delete("boards/57ae3940f43e6d960e0c45da/powerUps/calendar")
"""
@spec delete(String.t, Keyword.t) :: String.t
defdelegate delete(path, params), to: ExTrello.API.BareRequests

@doc """
GET OAuthGetRequestToken
Expand Down
33 changes: 33 additions & 0 deletions lib/ex_trello/api/bare_requests.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
defmodule ExTrello.API.BareRequests do
@moduledoc """
This module exists to provide ExTrello users with an interface to easily make requests to Trello that have not yet
been implemented in the wrapper.
TODO: Add some 'best guess' parsing of responses to provide anyone who uses these functions the same structured responses
as the other implemented functions.
Still not 100% on this, may just leave bare responses exposed for people who don't find value in the named structs.
"""

import ExTrello.API.Base

def get(path), do: get(path, [])
def get(path, params) when is_list(params) do
request(:get, path, params)
end

def post(path), do: post(path, [])
def post(path, params) when is_list(params) do
request(:post, path, params)
end

def put(path), do: put(path, [])
def put(path, params) when is_list(params) do
request(:put, path, params)
end

def delete(path), do: delete(path, [])
def delete(path, params) when is_list(params) do
request(:delete, path, params)
end
end
2 changes: 1 addition & 1 deletion lib/ex_trello/models/board.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule ExTrello.Model.Board do
defstruct id: nil, closed: nil, date_last_activity: nil, date_last_view: nil, desc: nil, desc_data: nil,
id_organization: nil, invitations: [], label_names: %{}, memberships: [], name: nil, pinned: nil,
prefs: %{}, short_link: nil, short_url: nil, starred: nil, subscribed: nil, url: nil, cards: nil,
actions: nil, lists: nil, members: nil
actions: nil, lists: nil, members: nil, power_ups: nil

@type t :: %__MODULE__{}
end
13 changes: 8 additions & 5 deletions lib/ex_trello/oauth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ defmodule ExTrello.OAuth do
"""
def request(method, url, params, credentials, opts \\ []) do
signed_params = OAuther.sign(to_string(method), url, stringify_params(params), credentials)
{header, _request_params} = OAuther.header(signed_params)
{header, request_params} = OAuther.header(signed_params)

content_type = if opts[:content_type], do: opts[:content_type], else: "application/x-www-form-urlencoded"

HTTPotion.request(method, url <> "?#{URI.encode_query(signed_params)}", header: [{"Content-Type", content_type}, header])
case method do
:get ->
HTTPotion.request(method, url <> "?#{URI.encode_query(request_params)}", headers: [header])
_ ->
HTTPotion.request(method, url, headers: [header, {"Content-Type", "application/x-www-form-urlencoded"}], body: URI.encode_query(request_params))
end
end

defp stringify_params(params) when is_list(params) do
Enum.map(params, fn({k, v}) -> {to_string(k), v} end)
end
defp stringify_params(params) when is_binary(params), do: params # Likely JSON or plaintext
defp stringify_params(params) when is_binary(params), do: params # plaintext
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule ExTrello.Mixfile do

def project do
[app: :ex_trello,
version: "0.4.1",
version: "0.4.2",
elixir: "~> 1.3",
description: "An Elixir package to interface with the Trello API",
build_embedded: Mix.env == :prod,
Expand Down

0 comments on commit 04b935b

Please sign in to comment.