-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from JamesS237/more_api_organisation
Further API movement/organisation
- Loading branch information
Showing
6 changed files
with
85 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule Hex.API.Package.Owner do | ||
alias Hex.API | ||
|
||
def add(package, owner, auth) do | ||
owner = URI.encode_www_form(owner) | ||
API.request(:put, API.api_url("packages/#{package}/owners/#{owner}"), API.auth(auth)) | ||
end | ||
|
||
def delete(package, owner, auth) do | ||
owner = URI.encode_www_form(owner) | ||
API.request(:delete, API.api_url("packages/#{package}/owners/#{owner}"), API.auth(auth)) | ||
end | ||
|
||
def get(package, auth) do | ||
API.request(:get, API.api_url("packages/#{package}/owners"), API.auth(auth)) | ||
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,54 @@ | ||
defmodule Hex.API.Util do | ||
@doc false | ||
def handle_hex_message(nil), do: :ok | ||
|
||
def handle_hex_message(header) do | ||
{message, level} = :binary.list_to_bin(header) |> parse_hex_message | ||
case level do | ||
"warn" -> Mix.shell.info("API warning: " <> message) | ||
"fatal" -> Mix.shell.error("API error: " <> message) | ||
_ -> :ok | ||
end | ||
end | ||
|
||
@space [?\s, ?\t] | ||
|
||
def parse_hex_message(message) do | ||
{message, rest} = skip_ws(message) |> quoted | ||
level = skip_ws(rest) |> opt_level | ||
{message, level} | ||
end | ||
|
||
def skip_ws(<< char, rest :: binary >>) when char in @space, | ||
do: skip_ws(rest) | ||
def skip_ws(rest), | ||
do: rest | ||
|
||
def skip_trail_ws(input, str \\ "", ws \\ "") | ||
|
||
def skip_trail_ws(<< char, rest :: binary >>, str, ws) when char in @space, | ||
do: skip_trail_ws(rest, str, << ws :: binary, char >>) | ||
def skip_trail_ws(<< char, rest :: binary >>, str, ws), | ||
do: skip_trail_ws(rest, << str :: binary, ws :: binary, char >>, "") | ||
def skip_trail_ws("", str, _ws), | ||
do: str | ||
|
||
def quoted("\"" <> rest), | ||
do: do_quoted(rest, "") | ||
|
||
def do_quoted("\"" <> rest, acc), | ||
do: {acc, rest} | ||
def do_quoted(<< char, rest :: binary >>, acc), | ||
do: do_quoted(rest, << acc :: binary, char >>) | ||
|
||
def opt_level(";" <> rest), | ||
do: do_level(rest) | ||
def opt_level(_), | ||
do: nil | ||
|
||
def do_level(rest) do | ||
"level" <> rest = skip_ws(rest) | ||
"=" <> rest = skip_ws(rest) | ||
skip_ws(rest) |> skip_trail_ws | ||
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
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 |
---|---|---|
|
@@ -70,26 +70,26 @@ defmodule Hex.APITest do | |
Hex.API.Package.new("orange", %{}, auth) | ||
Hex.API.User.new("orange_user", "[email protected]", "hunter42") | ||
|
||
assert {200, [%{"username" => "user"}]} = Hex.API.Package.get_owners("orange", auth) | ||
assert {200, [%{"username" => "user"}]} = Hex.API.Package.Owner.get("orange", auth) | ||
|
||
assert {204, _} = Hex.API.Package.add_owner("orange", "[email protected]", auth) | ||
assert {204, _} = Hex.API.Package.Owner.add("orange", "[email protected]", auth) | ||
|
||
assert {200, [%{"username" => "user"}, %{"username" => "orange_user"}]} = | ||
Hex.API.Package.get_owners("orange", auth) | ||
Hex.API.Package.Owner.get("orange", auth) | ||
|
||
assert {204, _} = Hex.API.Package.delete_owner("orange", "[email protected]", auth) | ||
assert {204, _} = Hex.API.Package.Owner.delete("orange", "[email protected]", auth) | ||
|
||
assert {200, [%{"username" => "user"}]} = Hex.API.Package.get_owners("orange", auth) | ||
assert {200, [%{"username" => "user"}]} = Hex.API.Package.Owner.get("orange", auth) | ||
end | ||
|
||
test "x-hex-message" do | ||
Hex.API.handle_hex_message('"oops, you done goofed"') | ||
Hex.API.Util.handle_hex_message('"oops, you done goofed"') | ||
refute_received {:mix_shell, _, _} | ||
|
||
Hex.API.handle_hex_message(' "oops, you done goofed" ; level = warn') | ||
Hex.API.Util.handle_hex_message(' "oops, you done goofed" ; level = warn') | ||
assert_received {:mix_shell, :info, ["API warning: oops, you done goofed"]} | ||
|
||
Hex.API.handle_hex_message('"oops, you done goofed";level=fatal ') | ||
Hex.API.Util.handle_hex_message('"oops, you done goofed";level=fatal ') | ||
assert_received {:mix_shell, :error, ["API error: oops, you done goofed"]} | ||
end | ||
end |