Skip to content

Commit db55359

Browse files
committed
Remove StreamAPI
1 parent 6e576a7 commit db55359

File tree

5 files changed

+33
-121
lines changed

5 files changed

+33
-121
lines changed

lib/code_corps/github/api/api.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ defmodule CodeCorps.GitHub.API do
1414
|> marshall_response()
1515
end
1616

17-
defdelegate eager_get_all(url, headers, opts), to: CodeCorps.GitHub.EagerAPI
18-
defdelegate lazy_get_all(url, headers, opts), to: CodeCorps.GitHub.StreamAPI
17+
defdelegate get_all(url, headers, opts), to: CodeCorps.GitHub.EagerAPI
1918

2019
@doc """
2120
Get access token headers for a given `CodeCorps.User` and
Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
defmodule CodeCorps.GitHub.EagerAPI do
22
@moduledoc """
3-
This module attempts to implement eager loading of a resource, by trying to
4-
fetch all of its pages in parallel.
5-
6-
This should technically be faster than lazy loading. However, it fails due to
7-
timeout errors, even when loading just two pages.
8-
9-
The assumption is that hackney needs to be configured to allow multiple
10-
requests.
3+
Eager loads a resource from the GitHub API by fetching all of its pages in
4+
parallel.
115
"""
126

13-
def eager_get_all(url, headers, options) do
7+
def get_all(url, headers, options) do
148
HTTPoison.start
159
{:ok, response} = HTTPoison.get(url, headers, options)
1610

@@ -43,43 +37,47 @@ defmodule CodeCorps.GitHub.EagerAPI do
4337
end
4438

4539
defp extract_total_pages(links_string) do
46-
# Unfortunately, the paginating info we get from GitHub's responses is not
47-
# suitable for easy extraction.
40+
# We use regex to parse the pagination info from the GitHub API response
41+
# headers.
4842
#
49-
# The information is stored in the following response header:
43+
# The headers render pages in the following format:
5044
#
5145
# ```
5246
# {"Link", '<https://api.github.com/search/code?q=addClass+user%3Amozilla&page=15>; rel="next",
5347
# <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=34>; rel="last",
5448
# <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=1>; rel="first",
5549
# <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=13>; rel="prev"'
56-
#
5750
# ```
5851
#
59-
# If the response has no list header, then that means we got all the records
60-
# and there's just that one page.
52+
# If the response has no list header, then we have received all the records
53+
# from the only possible page.
6154
#
6255
# If the response has a list header, the value will contain at least the
6356
# "last" relation.
64-
#
65-
# Unfortunatly, the only way to parse it is via regex.
6657
links_string
6758
|> String.split(", ")
6859
|> Enum.map(fn link ->
69-
# Searches for `rel=`
70-
rel = Regex.run(~r{rel="([a-z]+)"}, link) |> List.last
71-
# Searches for the following variations:
72-
# ```
73-
# ?page={match}>
74-
# ?page={match}&...
75-
# &page={match}>
76-
# &page={match}&...
77-
# ```
78-
page = Regex.run(~r{[&/?]page=([^>&]+)}, link) |> List.last |> String.to_integer
79-
60+
rel = get_rel(link)
61+
page = get_page(link)
8062
{rel, page}
8163
end)
8264
|> Enum.into(%{})
8365
|> Map.get("last")
8466
end
67+
68+
defp get_rel(link) do
69+
# Searches for `rel=`
70+
Regex.run(~r{rel="([a-z]+)"}, link) |> List.last()
71+
end
72+
73+
defp get_page(link) do
74+
# Searches for the following variations:
75+
# ```
76+
# ?page={match}>
77+
# ?page={match}&...
78+
# &page={match}>
79+
# &page={match}&...
80+
# ```
81+
Regex.run(~r{[&/?]page=([^>&]+)}, link) |> List.last |> String.to_integer
82+
end
8583
end

lib/code_corps/github/api/repository.ex

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ defmodule CodeCorps.GitHub.API.Repository do
1212

1313
@spec issues(GithubRepo.t) :: {:ok, list(map)} | {:error, GitHub.api_error_struct}
1414
def issues(%GithubRepo{github_app_installation: %GithubAppInstallation{} = installation} = github_repo) do
15-
with {:ok, access_token} <- installation |> API.Installation.get_access_token(),
16-
issues <- github_repo |> fetch_issues(access_token) do
15+
with {:ok, access_token} <- API.Installation.get_access_token(installation),
16+
issues <- fetch_issues(github_repo, access_token)
17+
do
1718
{:ok, issues}
1819
else
1920
{:error, error} -> {:error, error}
@@ -25,24 +26,6 @@ defmodule CodeCorps.GitHub.API.Repository do
2526
path = "repos/#{owner}/#{repo}/issues"
2627
params = [per_page: per_page, state: "all"]
2728
opts = [access_token: access_token, params: params]
28-
29-
# stream/lazy
30-
# before_operation = Timex.now
31-
# results = path |> fetch_lazy(opts)
32-
# after_operation = Timex.now
33-
# count = results |> Enum.count
34-
# pages = count / per_page |> Float.round
35-
# elapsed = Timex.diff(after_operation, before_operation)
36-
# IO.puts("Stream: #{count} records, #{per_page} records per page, #{pages} pages total, #{elapsed} microseconds")
37-
38-
# eager
39-
GitHub.eager_get_all(path, %{}, opts)
40-
end
41-
42-
defp fetch_lazy(path, opts) do
43-
path
44-
|> GitHub.lazy_get_all(%{}, opts)
45-
|> Enum.to_list
46-
|> List.flatten
29+
GitHub.get_all(path, %{}, opts)
4730
end
4831
end

lib/code_corps/github/api/stream_api.ex

Lines changed: 0 additions & 60 deletions
This file was deleted.

lib/code_corps/github/github.ex

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,8 @@ defmodule CodeCorps.GitHub do
124124
end
125125
end
126126

127-
def lazy_get_all(endpoint, headers, options) do
128-
api().lazy_get_all(
129-
api_url_for(endpoint),
130-
headers |> Headers.user_request(options),
131-
options |> add_default_options()
132-
)
133-
end
134-
135-
def eager_get_all(endpoint, headers, options) do
136-
api().eager_get_all(
127+
def get_all(endpoint, headers, options) do
128+
api().get_all(
137129
api_url_for(endpoint),
138130
headers |> Headers.user_request(options),
139131
options |> add_default_options()

0 commit comments

Comments
 (0)