Skip to content

Commit 7f7eb6c

Browse files
committed
Switched eager api to poison, fixed issues
1 parent 9f79573 commit 7f7eb6c

File tree

5 files changed

+52
-55
lines changed

5 files changed

+52
-55
lines changed

lib/code_corps/github/api/eager_api.ex

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,33 @@ defmodule CodeCorps.GitHub.EagerAPI do
1111
"""
1212

1313
def eager_get_all(url, headers, options) do
14-
{:ok, _status, headers, body} =
15-
:hackney.request(:get, url, headers, "", options)
14+
HTTPoison.start
15+
{:ok, response} = HTTPoison.get(url, headers, options)
1616

17-
first_page = Poison.decode!(body)
18-
19-
case headers |> retrieve_total_pages do
17+
first_page = Poison.decode!(response.body)
18+
case response.headers |> retrieve_total_pages do
2019
1 -> first_page
21-
total -> first_page ++ get_remaining_pages(total, url, headers, options)
20+
total ->
21+
first_page ++ get_remaining_pages(total, url, headers, options) |> List.flatten
2222
end
2323
end
2424

25-
defp get_remaining_pages(total, url, headers, options) do
26-
uri_struct = url |> URI.parse
27-
base_query =
28-
uri_struct
29-
|> Map.get(:query)
30-
|> URI.decode_query
31-
|> Map.put_new("per_page", 100)
32-
33-
page_links =
34-
2..total
35-
|> Enum.to_list
36-
|> Enum.map(fn page ->
37-
uri_struct |> Map.put(:query, base_query |> Map.put("page", page) |> URI.encode_query)
38-
|> URI.to_string
39-
end)
40-
41-
page_links
42-
|> Enum.map(&fetch_page(&1, headers, options))
43-
|> List.flatten
25+
defp get_remaining_pages(next, total, url, headers, options) do
26+
2..total
27+
|> Enum.to_list
28+
|> Enum.map(&Task.async(fn ->
29+
params = options[:params] ++ [page: &1]
30+
HTTPoison.get(url, headers, options ++ [params: params] |> IO.inspect)
31+
end))
32+
|> Enum.map(&Task.await(&1, 10000))
33+
|> Enum.map(&handle_page_response/1)
4434
end
4535

46-
defp fetch_page(url, headers, options) do
47-
{:ok, _status, headers, body} =
48-
:hackney.request(:get, url, headers, "", options)
36+
defp handle_page_response({:ok, %{body: body}}), do: Poison.decode!(body)
4937

50-
Poison.decode!(body)
38+
defp fetch_page(url, headers, options) do
39+
{:ok, response} = HTTPoison.get(url, headers, options)
40+
Poison.decode!(response.body)
5141
end
5242

5343
def retrieve_total_pages(headers) do

lib/code_corps/github/api/headers.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ defmodule CodeCorps.GitHub.API.Headers do
3333
end
3434

3535
@spec add_access_token_header(%{String.t => String.t}, list) :: %{String.t => String.t}
36-
defp add_access_token_header(%{} = headers, [access_token: nil]), do: headers
37-
defp add_access_token_header(%{} = headers, [access_token: access_token]) do
38-
Map.put(headers, "Authorization", "token #{access_token}")
36+
defp add_access_token_header(%{} = headers, opts) do
37+
case opts[:access_token] do
38+
nil -> headers
39+
token -> headers |> Map.put("Authorization", "token #{token}")
40+
end
3941
end
40-
defp add_access_token_header(headers, []), do: headers
4142

4243
@spec add_jwt_header(%{String.t => String.t}) :: %{String.t => String.t}
4344
defp add_jwt_header(%{} = headers) do

lib/code_corps/github/api/repository.ex

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,30 @@ defmodule CodeCorps.GitHub.API.Repository do
2323
end
2424

2525
defp fetch_issues(%GithubRepo{github_app_installation: %GithubAppInstallation{github_account_login: owner}, name: repo}, access_token) do
26-
path = "repos/#{owner}/#{repo}/issues?state=all&per_page=100"
27-
opts = [access_token: access_token]
26+
per_page = 5
27+
path = "repos/#{owner}/#{repo}/issues"
28+
params = [per_page: per_page, state: "all"]
29+
opts = [access_token: access_token, params: params]
2830

2931
# stream/lazy
30-
before_operation = Timex.now
31-
results = path |> fetch_lazy(opts)
32-
after_operation = Timex.now
33-
IO.puts("Retrieving #{results |> Enum.count} took #{Timex.diff(after_operation, before_operation)} microseconds")
34-
35-
# eager
3632
# before_operation = Timex.now
37-
# results = path |> fetch_eager(opts)
33+
# results = path |> fetch_lazy(opts)
3834
# after_operation = Timex.now
39-
# IO.puts("Retrieving #{results |> Enum.count} took #{Timex.diff(after_operation, before_operation)} microseconds")
35+
# count = results |> Enum.count
36+
# pages = count / per_page |> Float.round
37+
# elapsed = Timex.diff(after_operation, before_operation)
38+
# IO.puts("Stream: #{count} records, #{per_page} records per page, #{pages} pages total, #{elapsed} microseconds")
39+
40+
# eager
41+
before_operation = Timex.now
42+
43+
results = path |> GitHub.eager_get_all(%{}, opts)
44+
45+
after_operation = Timex.now
46+
count = results |> Enum.count
47+
pages = count / per_page |> Float.round
48+
elapsed = Timex.diff(after_operation, before_operation)
49+
IO.puts("Stream: #{count} records, #{per_page} records per page, #{pages} pages total, #{elapsed} microseconds")
4050

4151
results
4252
end
@@ -47,10 +57,4 @@ defmodule CodeCorps.GitHub.API.Repository do
4757
|> Enum.to_list
4858
|> List.flatten
4959
end
50-
51-
defp fetch_eager(path, opts) do
52-
path
53-
|> GitHub.eager_get_all(%{}, opts)
54-
|> List.flatten
55-
end
5660
end

mix.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ defmodule CodeCorps.Mixfile do
5454
{:cowboy, "~> 1.0"},
5555
{:benchfella, "~> 0.3.0", only: :dev},
5656
{:bypass, "~> 0.8.1", only: :test},
57-
{:cloudex, "~> 0.1.17"},
57+
{:cloudex, "~> 0.2.2"},
5858
{:comeonin, "~> 3.1"},
5959
{:corsica, "~> 1.0"}, # CORS
6060
{:credo, "~> 0.8", only: [:dev, :test]}, # Code style suggestions
@@ -66,6 +66,7 @@ defmodule CodeCorps.Mixfile do
6666
{:ex_machina, "~> 2.0", only: :test}, # test factories
6767
{:guardian, "~> 0.14.5"}, # Authentication (JWT)
6868
{:hackney, ">= 1.4.4"},
69+
{:httpoison, "~> 0.13"},
6970
{:inch_ex, "~> 0.5", only: [:dev, :test]}, # Inch CI
7071
{:inflex, "~> 1.8.1"},
7172
{:ja_serializer, "~> 0.12"}, # JSON API

mix.lock

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"benchfella": {:hex, :benchfella, "0.3.5", "b2122c234117b3f91ed7b43b6e915e19e1ab216971154acd0a80ce0e9b8c05f5", [:mix], []},
55
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], []},
66
"bypass": {:hex, :bypass, "0.8.1", "16d409e05530ece4a72fabcf021a3e5c7e15dcc77f911423196a0c551f2a15ca", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: false]}, {:plug, "~> 1.0", [hex: :plug, optional: false]}]},
7-
"certifi": {:hex, :certifi, "0.7.0", "861a57f3808f7eb0c2d1802afeaae0fa5de813b0df0979153cbafcd853ababaf", [:rebar3], []},
8-
"cloudex": {:hex, :cloudex, "0.1.20", "c8b0f36da0d33c4756cfbfdd7bf2bcd4722d58a51cb48da75a6c54d6186bad30", [:mix], [{:httpoison, "~> 0.11.0", [hex: :httpoison, optional: false]}, {:poison, "~> 3.1.0", [hex: :poison, optional: false]}, {:timex, "~> 3.1.7", [hex: :timex, optional: false]}, {:tzdata, "~> 0.5.11", [hex: :tzdata, optional: false]}]},
7+
"certifi": {:hex, :certifi, "2.0.0", "a0c0e475107135f76b8c1d5bc7efb33cd3815cb3cf3dea7aefdd174dabead064", [:rebar3], []},
8+
"cloudex": {:hex, :cloudex, "0.2.2", "da554dab88672163346a1614917df2a27ae82d07e956ee31de758f0b04c163bf", [:mix], [{:httpoison, "~> 0.13.0", [hex: :httpoison, optional: false]}, {:poison, "~> 3.1.0", [hex: :poison, optional: false]}, {:timex, "~> 3.1.7", [hex: :timex, optional: false]}, {:tzdata, "~> 0.5.11", [hex: :tzdata, optional: false]}]},
99
"combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], []},
1010
"comeonin": {:hex, :comeonin, "3.2.0", "cb10995a22aed6812667efb3856f548818c85d85394d8132bc116fbd6995c1ef", [:make, :mix], [{:elixir_make, "~> 0.4", [hex: :elixir_make, optional: false]}]},
1111
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [], []},
@@ -28,9 +28,9 @@
2828
"fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], []},
2929
"gettext": {:hex, :gettext, "0.13.1", "5e0daf4e7636d771c4c71ad5f3f53ba09a9ae5c250e1ab9c42ba9edccc476263", [:mix], []},
3030
"guardian": {:hex, :guardian, "0.14.5", "6d4e89b673accdacbc092ad000dc7494019426bd898eebf699caf1d19000cdcd", [:mix], [{:jose, "~> 1.8", [hex: :jose, optional: false]}, {:phoenix, "~> 1.2 and < 1.4.0", [hex: :phoenix, optional: true]}, {:plug, "~> 1.3", [hex: :plug, optional: false]}, {:poison, ">= 1.3.0 and < 4.0.0", [hex: :poison, optional: false]}, {:uuid, ">=1.1.1", [hex: :uuid, optional: false]}]},
31-
"hackney": {:hex, :hackney, "1.6.5", "8c025ee397ac94a184b0743c73b33b96465e85f90a02e210e86df6cbafaa5065", [:rebar3], [{:certifi, "0.7.0", [hex: :certifi, optional: false]}, {:idna, "1.2.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
32-
"httpoison": {:hex, :httpoison, "0.11.0", "b9240a9c44fc46fcd8618d17898859ba09a3c1b47210b74316c0ffef10735e76", [:mix], [{:hackney, "~> 1.6.3", [hex: :hackney, optional: false]}]},
33-
"idna": {:hex, :idna, "1.2.0", "ac62ee99da068f43c50dc69acf700e03a62a348360126260e87f2b54eced86b2", [:rebar3], []},
31+
"hackney": {:hex, :hackney, "1.10.1", "c38d0ca52ea80254936a32c45bb7eb414e7a96a521b4ce76d00a69753b157f21", [:rebar3], [{:certifi, "2.0.0", [hex: :certifi, optional: false]}, {:idna, "5.1.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
32+
"httpoison": {:hex, :httpoison, "0.13.0", "bfaf44d9f133a6599886720f3937a7699466d23bb0cd7a88b6ba011f53c6f562", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, optional: false]}]},
33+
"idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, optional: false]}]},
3434
"inch_ex": {:hex, :inch_ex, "0.5.6", "418357418a553baa6d04eccd1b44171936817db61f4c0840112b420b8e378e67", [:mix], [{:poison, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :poison, optional: false]}]},
3535
"inflex": {:hex, :inflex, "1.8.1", "9fa9684ff1a872eab7415c0be500cc1b7782f28da6ed75423081e75f92831b1c", [:mix], []},
3636
"ja_serializer": {:hex, :ja_serializer, "0.12.0", "ba4ec5fc7afa6daba815b5cb2b9bd0de410554ac4f0ed54e954d39decb353ca4", [:mix], [{:inflex, "~> 1.4", [hex: :inflex, optional: false]}, {:plug, "> 1.0.0", [hex: :plug, optional: false]}, {:poison, ">= 1.4.0", [hex: :poison, optional: false]}, {:scrivener, "~> 1.2 or ~> 2.0", [hex: :scrivener, optional: true]}]},
@@ -63,5 +63,6 @@
6363
"timex": {:hex, :timex, "3.1.24", "d198ae9783ac807721cca0c5535384ebdf99da4976be8cefb9665a9262a1e9e3", [:mix], [{:combine, "~> 0.7", [hex: :combine, optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5", [hex: :tzdata, optional: false]}]},
6464
"timex_ecto": {:hex, :timex_ecto, "3.0.5", "3ec6c25e10d2c0020958e5df64d2b5e690e441faa2c2259da8bc6bd3d7f39256", [:mix], [{:ecto, "~> 2.0", [hex: :ecto, optional: false]}, {:timex, "~> 3.0", [hex: :timex, optional: false]}]},
6565
"tzdata": {:hex, :tzdata, "0.5.12", "1c17b68692c6ba5b6ab15db3d64cc8baa0f182043d5ae9d4b6d35d70af76f67b", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, optional: false]}]},
66+
"unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [:rebar3], []},
6667
"uri_query": {:hex, :uri_query, "0.1.2", "ae35b83b472f3568c2c159eee3f3ccf585375d8a94fb5382db1ea3589e75c3b4", [:mix], []},
6768
"uuid": {:hex, :uuid, "1.1.7", "007afd58273bc0bc7f849c3bdc763e2f8124e83b957e515368c498b641f7ab69", [:mix], []}}

0 commit comments

Comments
 (0)