Skip to content

Commit

Permalink
Merge pull request #77 from JuliaWeb/kf/api
Browse files Browse the repository at this point in the history
Thread through an `api` argument everywhere
  • Loading branch information
Keno committed Aug 11, 2017
2 parents a66f7b8 + 066dbd0 commit 84692d3
Show file tree
Hide file tree
Showing 18 changed files with 197 additions and 160 deletions.
2 changes: 1 addition & 1 deletion src/GitHub.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import HttpCommon,

# include -------

include("utils/requests.jl")
include("utils/GitHubType.jl")
include("utils/auth.jl")
include("utils/requests.jl")

# export -------

Expand Down
24 changes: 12 additions & 12 deletions src/activity/activity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@
# Starring #
############

function stargazers(repo; options...)
results, page_data = gh_get_paged_json("/repos/$(name(repo))/stargazers"; options...)
@api_default function stargazers(api::GitHubAPI, repo; options...)
results, page_data = gh_get_paged_json(api, "/repos/$(name(repo))/stargazers"; options...)
return map(Owner, results), page_data
end

function starred(user; options...)
results, page_data = gh_get_paged_json("/users/$(name(user))/starred"; options...)
@api_default function starred(api::GitHubAPI, user; options...)
results, page_data = gh_get_paged_json(api, "/users/$(name(user))/starred"; options...)
return map(Repo, results), page_data
end

star(repo; options...) = gh_put("/user/starred/$(name(repo))"; options...)
@api_default star(api::GitHubAPI, repo; options...) = gh_put(api, "/user/starred/$(name(repo))"; options...)

unstar(repo; options...) = gh_delete("/user/starred/$(name(repo))"; options...)
@api_default unstar(api::GitHubAPI, repo; options...) = gh_delete(api, "/user/starred/$(name(repo))"; options...)

############
# Watching #
############

function watchers(repo; options...)
results, page_data = gh_get_paged_json("/repos/$(name(repo))/subscribers"; options...)
@api_default function watchers(api::GitHubAPI, repo; options...)
results, page_data = gh_get_paged_json(api, "/repos/$(name(repo))/subscribers"; options...)
return map(Owner, results), page_data
end

function watched(owner; options...)
results, page_data = gh_get_paged_json("/users/$(name(owner))/subscriptions"; options...)
@api_default function watched(api::GitHubAPI, owner; options...)
results, page_data = gh_get_paged_json(api, "/users/$(name(owner))/subscriptions"; options...)
return map(Repo, results), page_data
end

watch(repo; options...) = gh_put("/repos/$(name(repo))/subscription"; options...)
@api_default watch(api::GitHubAPI, repo; options...) = gh_put(api, "/repos/$(name(repo))/subscription"; options...)

unwatch(repo; options...) = gh_delete("/repos/$(name(repo))/subscription"; options...)
@api_default unwatch(api::GitHubAPI, repo; options...) = gh_delete(api, "/repos/$(name(repo))/subscription"; options...)
12 changes: 6 additions & 6 deletions src/apps/apps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ end
namefield(a::App) = a.id
App(data::Dict) = json2github(App, data)

function app(; headers = Dict(), kwargs...)
@api_default function app(api::GitHubAPI; headers = Dict(), kwargs...)
headers["Accept"] = "application/vnd.github.machine-man-preview+json"
App(gh_get_json("/app"; headers=headers, kwargs...))
App(gh_get_json(api, "/app"; headers=headers, kwargs...))
end

function app(id::Int; headers = Dict(), kwargs...)
@api_default function app(api::GitHubAPI, id::Int; headers = Dict(), kwargs...)
headers["Accept"] = "application/vnd.github.machine-man-preview+json"
App(gh_get_json("/app/$id"; headers=headers, kwargs...))
App(gh_get_json(api, "/app/$id"; headers=headers, kwargs...))
end

function app(slug::String; headers = Dict(), kwargs...)
@api_default function app(api::GitHubAPI, slug::String; headers = Dict(), kwargs...)
headers["Accept"] = "application/vnd.github.machine-man-preview+json"
App(gh_get_json("/apps/$slug"; headers=headers, kwargs...))
App(gh_get_json(api, "/apps/$slug"; headers=headers, kwargs...))
end
4 changes: 2 additions & 2 deletions src/apps/installations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namefield(i::Installation) = i.id
Installation(data::Dict) = json2github(Installation, data)
Installation(id::Int) = Installation(Dict("id" => id))

function create_access_token(i::Installation, auth::JWTAuth; headers = Dict(), options...)
@api_default function create_access_token(api::GitHubAPI, i::Installation, auth::JWTAuth; headers = Dict(), options...)
headers["Accept"] = "application/vnd.github.machine-man-preview+json"
payload = gh_post_json("/installations/$(get(i.id))/access_tokens", auth = auth,
payload = gh_post_json(api, "/installations/$(get(i.id))/access_tokens", auth = auth,
headers=headers, options...)
OAuth2(payload["token"])
end
34 changes: 17 additions & 17 deletions src/gists/gist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,48 +32,48 @@ namefield(gist::Gist) = gist.id
# creating #
#----------#

gist(gist_obj::Gist; options...) = gist(name(gist_obj); options...)
@api_default gist(gist_obj::Gist; options...) = gist(api::GitHubAPI, name(gist_obj); options...)

function gist(gist_obj, sha = ""; options...)
@api_default function gist(api::GitHubAPI, gist_obj, sha = ""; options...)
!isempty(sha) && (sha = "/" * sha)
result = gh_get_json("/gists/$(name(gist_obj))$sha"; options...)
result = gh_get_json(api, "/gists/$(name(gist_obj))$sha"; options...)
g = Gist(result)
end

function gists(owner; options...)
results, page_data = gh_get_paged_json("/users/$(name(owner))/gists"; options...)
@api_default function gists(api::GitHubAPI, owner; options...)
results, page_data = gh_get_paged_json(api, "/users/$(name(owner))/gists"; options...)
map(Gist, results), page_data
end

function gists(; options...)
results, page_data = gh_get_paged_json("/gists/public"; options...)
@api_default function gists(api::GitHubAPI; options...)
results, page_data = gh_get_paged_json(api, "/gists/public"; options...)
return map(Gist, results), page_data
end

# modifying #
#-----------#

create_gist(; options...) = Gist(gh_post_json("/gists"; options...))
edit_gist(gist; options...) = Gist(gh_patch_json("/gists/$(name(gist))"; options...))
delete_gist(gist; options...) = gh_delete("/gists/$(name(gist))"; options...)
@api_default create_gist(; options...) = Gist(gh_post_json(api, "/gists"; options...))
@api_default edit_gist(api::GitHubAPI, gist; options...) = Gist(gh_patch_json(api, "/gists/$(name(gist))"; options...))
@api_default delete_gist(api::GitHubAPI, gist; options...) = gh_delete(api, "/gists/$(name(gist))"; options...)

# stars #
#------#

star_gist(gist; options...) = gh_put("/gists/$(name(gist))/star"; options...)
unstar_gist(gist; options...) = gh_delete("/gists/$(name(gist))/star"; options...)
@api_default star_gist(api::GitHubAPI, gist; options...) = gh_put(api, "/gists/$(name(gist))/star"; options...)
@api_default unstar_gist(api::GitHubAPI, gist; options...) = gh_delete(api, "/gists/$(name(gist))/star"; options...)

function starred_gists(; options...)
results, page_data = gh_get_paged_json("/gists/starred"; options...)
@api_default function starred_gists(api::GitHubAPI; options...)
results, page_data = gh_get_paged_json(api, "/gists/starred"; options...)
return map(Gist, results), page_data
end

# forks #
#-------#

create_gist_fork(gist::Gist; options...) = Gist(gh_post_json("/gists/$(name(gist))/forks"; options...))
@api_default create_gist_fork(api::GitHubAPI, gist::Gist; options...) = Gist(gh_post_json(api, "/gists/$(name(gist))/forks"; options...))

function gist_forks(gist; options...)
results, page_data = gh_get_paged_json("/gists/$(name(gist))/forks"; options...)
@api_default function gist_forks(api::GitHubAPI, gist; options...)
results, page_data = gh_get_paged_json(api, "/gists/$(name(gist))/forks"; options...)
return map(Gist, results), page_data
end
20 changes: 10 additions & 10 deletions src/issues/comments.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ kind_err_str(kind) = ("Error building comment request: :$kind is not a valid kin
# API Methods #
###############

function comment(repo, item, kind = :issue; options...)
@api_default function comment(api::GitHubAPI, repo, item, kind = :issue; options...)
if (kind == :issue) || (kind == :pr)
path = "/repos/$(name(repo))/issues/comments/$(name(item))"
elseif kind == :review
Expand All @@ -42,10 +42,10 @@ function comment(repo, item, kind = :issue; options...)
else
error(kind_err_str(kind))
end
return Comment(gh_get_json(path; options...))
return Comment(gh_get_json(api, path; options...))
end

function comments(repo, item, kind = :issue; options...)
@api_default function comments(api::GitHubAPI, repo, item, kind = :issue; options...)
if (kind == :issue) || (kind == :pr)
path = "/repos/$(name(repo))/issues/$(name(item))/comments"
elseif kind == :review
Expand All @@ -55,11 +55,11 @@ function comments(repo, item, kind = :issue; options...)
else
error(kind_err_str(kind))
end
results, page_data = gh_get_paged_json(path; options...)
results, page_data = gh_get_paged_json(api, path; options...)
return map(Comment, results), page_data
end

function create_comment(repo, item, kind = :issue; options...)
@api_default function create_comment(api::GitHubAPI, repo, item, kind = :issue; options...)
if (kind == :issue) || (kind == :pr)
path = "/repos/$(name(repo))/issues/$(name(item))/comments"
elseif kind == :review
Expand All @@ -69,10 +69,10 @@ function create_comment(repo, item, kind = :issue; options...)
else
error(kind_err_str(kind))
end
return Comment(gh_post_json(path; options...))
return Comment(gh_post_json(api, path; options...))
end

function edit_comment(repo, item, kind = :issue; options...)
@api_default function edit_comment(api::GitHubAPI, repo, item, kind = :issue; options...)
if (kind == :issue) || (kind == :pr)
path = "/repos/$(name(repo))/issues/comments/$(name(item))"
elseif kind == :review
Expand All @@ -82,10 +82,10 @@ function edit_comment(repo, item, kind = :issue; options...)
else
error(kind_err_str(kind))
end
return Comment(gh_patch_json(path; options...))
return Comment(gh_patch_json(api, path; options...))
end

function delete_comment(repo, item, kind = :issue; options...)
@api_default function delete_comment(api::GitHubAPI, repo, item, kind = :issue; options...)
if (kind == :issue) || (kind == :pr)
path = "/repos/$(name(repo))/issues/comments/$(name(item))"
elseif kind == :review
Expand All @@ -95,5 +95,5 @@ function delete_comment(repo, item, kind = :issue; options...)
else
error(kind_err_str(kind))
end
return gh_delete(path; options...)
return gh_delete(api, path; options...)
end
16 changes: 8 additions & 8 deletions src/issues/issues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ namefield(issue::Issue) = issue.number
# API Methods #
###############

function issue(repo, issue_obj; options...)
result = gh_get_json("/repos/$(name(repo))/issues/$(name(issue_obj))"; options...)
@api_default function issue(api::GitHubAPI, repo, issue_obj; options...)
result = gh_get_json(api, "/repos/$(name(repo))/issues/$(name(issue_obj))"; options...)
return Issue(result)
end

function issues(repo; options...)
results, page_data = gh_get_paged_json("/repos/$(name(repo))/issues"; options...)
@api_default function issues(api::GitHubAPI, repo; options...)
results, page_data = gh_get_paged_json(api, "/repos/$(name(repo))/issues"; options...)
return map(Issue, results), page_data
end

function create_issue(repo; options...)
result = gh_post_json("/repos/$(name(repo))/issues"; options...)
@api_default function create_issue(api::GitHubAPI, repo; options...)
result = gh_post_json(api, "/repos/$(name(repo))/issues"; options...)
return Issue(result)
end

function edit_issue(repo, issue; options...)
result = gh_patch_json("/repos/$(name(repo))/issues/$(name(issue))"; options...)
@api_default function edit_issue(api::GitHubAPI, repo, issue; options...)
result = gh_patch_json(api, "/repos/$(name(repo))/issues/$(name(issue))"; options...)
return Issue(result)
end
16 changes: 8 additions & 8 deletions src/issues/pull_requests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ namefield(pr::PullRequest) = pr.number
# API Methods #
###############

function pull_requests(repo; options...)
results, page_data = gh_get_paged_json("/repos/$(name(repo))/pulls"; options...)
@api_default function pull_requests(api::GitHubAPI, repo; options...)
results, page_data = gh_get_paged_json(api, "/repos/$(name(repo))/pulls"; options...)
return map(PullRequest, results), page_data
end

function pull_request(repo, pr; options...)
result = gh_get_json("/repos/$(name(repo))/pulls/$(name(pr))"; options...)
@api_default function pull_request(api::GitHubAPI, repo, pr; options...)
result = gh_get_json(api, "/repos/$(name(repo))/pulls/$(name(pr))"; options...)
return PullRequest(result)
end

function create_pull_request(repo; options...)
result = gh_post_json("/repos/$(name(repo))/pulls"; options...)
@api_default function create_pull_request(api::GitHubAPI, repo; options...)
result = gh_post_json(api, "/repos/$(name(repo))/pulls"; options...)
return PullRequest(result)
end

function create_comment(repo, pr::PullRequest, body::AbstractString; options...)
create_comment(repo, pr, :pr; params = Dict(
@api_default function create_comment(api::GitHubAPI, repo, pr::PullRequest, body::AbstractString; options...)
create_comment(api, repo, pr, :pr; params = Dict(
:body => body
), options...)
end
16 changes: 8 additions & 8 deletions src/issues/reviews.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ end

namefield(rev::Review) = rev.id

function reviews(repo, pr::PullRequest; options...)
@api_default function reviews(api::GitHubAPI, repo, pr::PullRequest; options...)
path = "/repos/$(name(repo))/pulls/$(name(pr))/reviews"
results, page_data = gh_get_paged_json(path; options...)
results, page_data = gh_get_paged_json(api, path; options...)
return map(x->Review(pr, x), results), page_data
end

function comments(repo, rev::Review; options...)
@api_default function comments(api::GitHubAPI, repo, rev::Review; options...)
path = "/repos/$(name(repo))/pulls/$(name(get(rev.pr)))/reviews/$(name(rev))/comments"
results, page_data = gh_get_paged_json(path; options...)
results, page_data = gh_get_paged_json(api, path; options...)
return map(Comment, results), page_data
end

function reply_to(repo, r::Review, c::Comment, body; options...)
create_comment(repo, get(r.pr), :review; params = Dict(
@api_default function reply_to(api::GitHubAPI, repo, r::Review, c::Comment, body; options...)
create_comment(api, repo, get(r.pr), :review; params = Dict(
:body => body,
:in_reply_to => get(c.id)
), options...)
end

function dismiss_review(repo::Repo, r::Review; options...)
gh_put("/repos/$(name(repo))/pulls/$(name(get(rev.pr)))/reviews/$(name(rev))/dismissals")
@api_default function dismiss_review(api::GitHubAPI, repo::Repo, r::Review; options...)
gh_put(api, "/repos/$(name(repo))/pulls/$(name(get(rev.pr)))/reviews/$(name(rev))/dismissals")
end
Loading

0 comments on commit 84692d3

Please sign in to comment.