-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add code for API request logging #226
- Loading branch information
Showing
7 changed files
with
230 additions
and
11 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
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule App.Reqlog do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
alias App.{Repo} | ||
alias __MODULE__ | ||
require Logger | ||
|
||
schema "reqlogs" do | ||
field :req, :string | ||
field :param, :string | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(reqlog, attrs) do | ||
reqlog | ||
|> cast(attrs, [:req, :param]) | ||
|> validate_required([:req, :param]) | ||
end | ||
|
||
@doc """ | ||
Creates a `reqlog` (request log) record. | ||
""" | ||
def create(attrs) do | ||
%Reqlog{} | ||
|> changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
# clean interface function that can be called from App.GitHub functions | ||
# e.g: log("repository", "#{owner}/#{reponame}") | ||
def log(req, param) do | ||
Logger.info "Fetching #{req} #{param}" | ||
create(%{req: req, param: param}) | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule App.Repo.Migrations.CreateReqlogs do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:reqlogs) do | ||
add :req, :string | ||
add :param, :string | ||
|
||
timestamps() | ||
end | ||
end | ||
end |
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,6 @@ | ||
defmodule App.GitHubTest do | ||
use ExUnit.Case | ||
use App.DataCase | ||
alias App.GitHub | ||
|
||
test "App.GitHub.repository/1" do | ||
|
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule App.ReqlogTest do | ||
use App.DataCase | ||
|
||
test "App.Reqlog.create/1" do | ||
owner = "dwyl" | ||
reponame = "mvp" | ||
record = %{ | ||
created_at: "2014-03-02T13:20:04Z", | ||
req: "repository", | ||
param: "#{owner}/#{reponame}" | ||
} | ||
assert {:ok, inserted} = App.Reqlog.create(record) | ||
assert inserted.req == record.req | ||
end | ||
|
||
test "App.Reqlog.log/2" do | ||
owner = "dwyl" | ||
reponame = "mvp" | ||
|
||
assert {:ok, inserted} = App.Reqlog.log("repo", "#{owner}/#{reponame}") | ||
assert inserted.req == "repo" | ||
assert inserted.param == "#{owner}/#{reponame}" | ||
end | ||
end |