-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add plain text responses #25
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,18 +152,41 @@ def triplet_for_exception(exception) | |
|
||
class << self | ||
|
||
# Return a plain text triplet for the given body. | ||
# | ||
# @param body [String] | ||
# @param status [Integer] | ||
# @param headers [Hash] | ||
# @return [Array] | ||
def plain_triplet(body, status: 200, headers: {}) | ||
response_triplet(body, content_type: 'text/plain', status: status, headers: headers) | ||
end | ||
|
||
# Return a JSON-ready triplet for the given body. | ||
# | ||
# @param body [Hash, Array] | ||
# @param status [Integer] | ||
# @param headers [Hash] | ||
# @return [Array] | ||
def json_triplet(body, status: 200, headers: {}) | ||
body_as_json = body.to_json | ||
response_triplet(body.to_json, content_type: 'application/json', status: status, headers: headers) | ||
end | ||
|
||
# Return a triplet for the given body. | ||
# | ||
# @param body [Hash, Array] | ||
# @param content_type [String] | ||
# @param status [Integer] | ||
# @param headers [Hash] | ||
# @return [Array] | ||
def response_triplet(body, content_type:, status: 200, headers: {}) | ||
content_length = body.bytesize.to_s | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that However, we don't directly expose a means of sending a custom response. While the Hence, I'd say this is not a blocker for merging this PR, but something that needs to be dealt with in the future should we want to expose fully custom responses. |
||
body = [body] unless body.respond_to?(:each) | ||
|
||
[ | ||
status, | ||
headers.merge('content-type' => 'application/json', 'content-length' => body_as_json.bytesize.to_s), | ||
[body_as_json] | ||
headers.merge('content-type' => content_type, 'content-length' => content_length), | ||
body | ||
] | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,11 @@ | |
module Apia | ||
class Response | ||
|
||
TYPES = [ | ||
JSON = :json, | ||
PLAIN = :plain | ||
].freeze | ||
|
||
attr_accessor :status | ||
attr_reader :fields | ||
attr_reader :headers | ||
|
@@ -20,6 +25,11 @@ def initialize(request, endpoint) | |
@headers = {} | ||
end | ||
|
||
def plain_text_body(body) | ||
@type = PLAIN | ||
@body = body | ||
end | ||
|
||
# Add a field value for this endpoint | ||
# | ||
# @param name [Symbol] | ||
|
@@ -53,11 +63,20 @@ def body | |
@body || hash | ||
end | ||
|
||
def type | ||
@type || JSON | ||
end | ||
|
||
# Return the rack triplet for this response | ||
# | ||
# @return [Array] | ||
def rack_triplet | ||
Rack.json_triplet(body, headers: @headers, status: @status) | ||
case type | ||
when JSON | ||
Rack.json_triplet(body, headers: headers, status: status) | ||
when PLAIN | ||
Rack.plain_triplet(body, headers: headers, status: status) | ||
end | ||
Comment on lines
+74
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: Since we're in the area, and we have the This might also have even further implications with the OpenAPI schema though. Basically, if this wouldn't be a quick win, let's ignore it until we need it :) |
||
end | ||
|
||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good @mattbearman !
Would you be able to have a go at adding a plain text endpoint to the example api used in the tests for our apia-openapi gem.
https://github.com/apiaframework/apia-openapi/blob/main/examples/core_api/base.rb
It would be good to confirm we can successfully add this change into the openapi spec that is generated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea 😄