-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose error responses in the spec output
closes: #2
- Loading branch information
1 parent
17f9194
commit 72c7d4d
Showing
18 changed files
with
628 additions
and
134 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,6 @@ gem "rspec", "~> 3.0" | |
gem "rubocop", "~> 1.21" | ||
|
||
group :test do | ||
gem "pry" | ||
gem "simplecov", require: false | ||
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
module CoreAPI | ||
module Authenticators | ||
class MainAuthenticator < Apia::Authenticator | ||
|
||
BEARER_TOKEN = "example" | ||
|
||
type :bearer | ||
|
||
potential_error "InvalidToken" do | ||
code :invalid_token | ||
description "The token provided is invalid. In this example, you should provide '#{BEARER_TOKEN}'." | ||
http_status 403 | ||
|
||
field :given_token, type: :string | ||
end | ||
|
||
potential_error "UnauthorizedNetworkForAPIToken" do | ||
code :unauthorized_network_for_api_token | ||
description "Network is not allowed to access the API with this API token" | ||
http_status 403 | ||
|
||
field :ip_address, :string do | ||
description "The IP address the request was received from" | ||
end | ||
end | ||
|
||
def call | ||
configure_cors_response | ||
return if request.options? | ||
|
||
given_token = request.headers["authorization"]&.sub(/\ABearer /, "") | ||
if given_token == BEARER_TOKEN | ||
request.identity = { name: "Example User", id: 1234 } | ||
else | ||
raise_error "CoreAPI/MainAuthenticator/InvalidToken", given_token: given_token.to_s | ||
end | ||
end | ||
|
||
private | ||
|
||
# These are not strictly required, but it allows the app to work with swagger-ui. | ||
def configure_cors_response | ||
# Define a list of cors methods that are permitted for the request. | ||
cors.methods = %w[GET POST PUT PATCH DELETE OPTIONS] | ||
|
||
# Define a list of cors headers that are permitted for the request. | ||
cors.headers = %w[Authorization Content-Type] # or allow all with '*' | ||
|
||
# Define a the hostname to allow for CORS requests. | ||
cors.origin = "*" # or 'example.com' | ||
end | ||
|
||
end | ||
end | ||
end |
13 changes: 13 additions & 0 deletions
13
examples/core_api/authenticators/time_controller_authenticator.rb
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,13 @@ | ||
# frozen_string_literal: true | ||
|
||
require "core_api/errors/rate_limit_reached" | ||
|
||
module CoreAPI | ||
module Authenticators | ||
class TimeControllerAuthenticator < Apia::Authenticator | ||
|
||
potential_error CoreAPI::Errors::RateLimitReached | ||
|
||
end | ||
end | ||
end |
17 changes: 17 additions & 0 deletions
17
examples/core_api/authenticators/time_now_authenticator.rb
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 @@ | ||
# frozen_string_literal: true | ||
|
||
require "core_api/errors/rate_limit_reached" | ||
|
||
module CoreAPI | ||
module Authenticators | ||
class TimeNowAuthenticator < Apia::Authenticator | ||
|
||
potential_error "WrongDayOfWeek" do | ||
code :wrong_day_of_week | ||
description "You called this API on the wrong day of the week, try again tomorrow" | ||
http_status 503 | ||
end | ||
|
||
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
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,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module CoreAPI | ||
module Errors | ||
class RateLimitReached < Apia::Error | ||
|
||
code :rate_limit_reached | ||
http_status 429 | ||
description "You have reached the rate limit for this type of request" | ||
|
||
field :total_permitted, type: :integer do | ||
description "The total number of requests per minute that are permitted" | ||
end | ||
|
||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.