Skip to content

Commit

Permalink
[#48] Moving all the backend calls to Coophub.Backends and changing t…
Browse files Browse the repository at this point in the history
…he behaviour to just have the minimal needed specific implementation.
  • Loading branch information
diegomanuel committed Feb 26, 2020
1 parent e470995 commit f463721
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 136 deletions.
143 changes: 135 additions & 8 deletions lib/coophub/backends/backends.ex
Original file line number Diff line number Diff line change
@@ -1,20 +1,147 @@
defmodule Coophub.Backends do
alias Coophub.Backends
alias Coophub.Schemas.{Organization, Repository}

require Logger

@type url :: String.t()
@type headers :: [{String.t(), String.t()}]
@type languages :: [%{String.t() => integer()}]
@type request :: {String.t(), url, headers}

@type org :: Organization.t()
@type repo :: Repository.t()
@type langs :: %{String.t() => integer()}
@type topics :: [String.t()]

## Backends implementations
defp get_backend!("github"), do: Backends.Github
defp get_backend!(source), do: raise("Unknown backend source: #{source}")

########
## CALLS TO BACKENDS RESOURCES
########

@spec get_org(String.t(), String.t(), map) :: org | :error
def get_org(source, key, yml_data) do
backend = get_backend!(source)
bname = backend.name()
{name, url, headers} = backend.request_org(key, yml_data)
Logger.info("Fetching '#{name}' organization from #{bname}..", ansi_color: :yellow)

case call_api_get(url, headers) do
{:ok, data, ms} ->
Logger.info("Fetched '#{name}' organization! (#{ms}ms)", ansi_color: :green)
backend.parse_org(data)

{:error, reason} ->
Logger.error("Error getting '#{name}' organization from #{bname}: #{inspect(reason)}")
:error
end
end

@spec get_members(String.t(), org) :: [map]
def get_members(source, org) do
backend = get_backend!(source)
bname = backend.name()
{name, url, headers} = backend.request_members(org)
Logger.info("Fetching '#{name}' members from #{bname}..", ansi_color: :yellow)

case call_api_get(url, headers) do
{:ok, members, ms} ->
Logger.info("Fetched #{length(members)} '#{name}' members! (#{ms}ms)", ansi_color: :green)
backend.parse_members(members)

{:error, reason} ->
Logger.error("Error getting '#{name}' members from #{bname}: #{inspect(reason)}")
[]
end
end

@spec get_repos(String.t(), org) :: [repo]
def get_repos(source, org) do
backend = get_backend!(source)
bname = backend.name()
limit = Application.get_env(:coophub, :fetch_max_repos)
{name, url, headers} = backend.request_repos(org, limit)
Logger.info("Fetching '#{name}' repos from #{bname} (max=#{limit})..", ansi_color: :yellow)

case call_api_get(url, headers) do
{:ok, repos, ms} ->
Logger.info("Fetched #{length(repos)} '#{name}' repos! (#{ms}ms)", ansi_color: :green)

Enum.map(repos, fn repo_data ->
get_repo(backend, org, repo_data)
end)

## Backends definitions
defp get_backend("github"), do: Backends.Github
defp get_backend(source), do: raise("Unknown backend source: #{source}")
{:error, reason} ->
Logger.error("Error getting '#{name}' repos from #{bname}: #{inspect(reason)}")
[]
end
end

@spec get_repo(module, org, map) :: [repo]
def get_repo(backend, org, repo_data) do
bname = backend.name()
{name, url, headers} = backend.request_repo(org, repo_data)
Logger.info("Fetching '#{name}' repo data from #{bname}..", ansi_color: :cyan)

case call_api_get(url, headers) do
{:ok, data, ms} ->
Logger.info("Fetched '#{name}' repo data! (#{ms}ms)", ansi_color: :green)
backend.parse_repo(data)

{:error, reason} ->
Logger.error("Error getting '#{name}' repo data from #{bname}: #{inspect(reason)}")
## Fallback to repo_data we've fetched before
backend.parse_repo(repo_data)
end
end

@spec get_topics(String.t(), org, repo) :: topics
def get_topics(source, org, repo) do
backend = get_backend!(source)
bname = backend.name()
{name, url, headers} = backend.request_topics(org, repo)
Logger.info("Fetching '#{name}' topics from #{bname}..", ansi_color: :cyan)

case call_api_get(url, headers) do
{:ok, data, ms} ->
topics = backend.parse_topics(data)

@spec call_backend!(String.t(), atom, list) :: Backends.Behaviour.results()
def call_backend!(source, func, params) do
get_backend(source) |> apply(func, params)
Logger.info("Fetched #{length(topics)} '#{name}' topics! (#{ms}ms)",
ansi_color: :green
)

topics

{:error, reason} ->
Logger.error("Error getting '#{name}' topics from #{bname}: #{inspect(reason)}")
[]
end
end

@spec get_languages(String.t(), org, repo) :: langs
def get_languages(source, org, repo) do
backend = get_backend!(source)
bname = backend.name()
{name, url, headers} = backend.request_languages(org, repo)
Logger.info("Fetching '#{name}' languages from #{bname}..", ansi_color: :cyan)

case call_api_get(url, headers) do
{:ok, data, ms} ->
langs = backend.parse_languages(data)
count = langs |> Map.keys() |> length()
Logger.info("Fetched #{count} '#{name}' languages! (#{ms}ms)", ansi_color: :green)
langs

{:error, reason} ->
Logger.error("Error getting '#{name}' languages from #{bname}: #{inspect(reason)}")
[]
end
end

@spec call_api_get(String.t(), headers) :: {:ok, map | [map], integer} | {:error, any}
def call_api_get(url, headers) do
defp call_api_get(url, headers) do
start_ms = take_time()

case HTTPoison.get(url, headers) do
Expand Down
39 changes: 22 additions & 17 deletions lib/coophub/backends/behaviour.ex
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
defmodule Coophub.Backends.Behaviour do
alias Coophub.Backends
alias Coophub.Schemas.{Organization, Repository}

@type org :: Organization.t()
@type repo :: Repository.t()
@type langs :: Backends.languages()
@type results ::
:error
| org
| repo
| [repo]
| [map]
| [String.t()]
| langs
@type request :: Backends.request()
@type org :: Backends.org()
@type repo :: Backends.repo()
@type langs :: Backends.langs()
@type topics :: Backends.topics()

@callback get_org(String.t(), map) :: org | :error
@callback get_members(org) :: [map]
@callback get_repos(org) :: [repo]
@callback get_topics(org, repo) :: [String.t()]
@callback get_languages(org, repo) :: langs
@callback name() :: String.t()

@callback request_org(String.t(), map) :: request
@callback parse_org(map) :: org

@callback request_members(org) :: request
@callback parse_members([map]) :: [map]

@callback request_repos(org, integer) :: request
@callback request_repo(org, map) :: request
@callback parse_repo(map) :: repo

@callback request_topics(org, repo) :: request
@callback parse_topics(any) :: topics

@callback request_languages(org, repo) :: request
@callback parse_languages(any) :: langs
end
Loading

0 comments on commit f463721

Please sign in to comment.