-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add post functionality to parliament-ruby #79
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 |
---|---|---|
@@ -1 +1 @@ | ||
2.3.1 | ||
2.4.1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,6 +106,52 @@ def get(params: nil) | |
build_response(net_response) | ||
end | ||
|
||
# Makes an HTTP POST request and process results into a response. | ||
# | ||
# @example HTTP POST request | ||
# request = Parliament::Request::BaseRequest.new(base_url: 'http://example.com/people/123', headers: {'Content': 'application/json', 'Accept': 'application/json'}) | ||
# | ||
# # url: http://example.com/people/123 | ||
# | ||
# response = request.post(body: {}.to_json) #=> #<Parliament::Response::BaseResponse ...> | ||
# | ||
# @example HTTP POST request with URI encoded form values | ||
# request = Parliament::Request::BaseRequest.new(base_url: 'http://example.com/people/current', headers: {'Content': 'application/json', 'Accept': 'application/json'}) | ||
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. Line is too long. [175/120] |
||
# | ||
# # url: http://example.com/people/current?limit=10&page=4&lang=en-gb | ||
# | ||
# response = request.post({ limit: 10, page: 4, lang: 'en-gb' }, body: {}.to_json) #=> #<Parliament::Response::BaseResponse ...> | ||
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. Line is too long. [136/120] |
||
# | ||
# @raise [Parliament::ServerError] when the server responds with a 5xx status code. | ||
# @raise [Parliament::ClientError] when the server responds with a 4xx status code. | ||
# @raise [Parliament::NoContentResponseError] when the response body is empty. | ||
# | ||
# @param [Hash] params (optional) additional URI encoded form values to be added to the URI. | ||
# @param [String] body (optional) body of the post request. | ||
# | ||
# @return [Parliament::Response::BaseResponse] a Parliament::Response::BaseResponse object containing all of the data returned from the URL. | ||
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. Line is too long. [146/120] |
||
def post(params: nil, body: nil) | ||
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. Assignment Branch Condition size for post is too high. [21.91/15] |
||
@query_params = @query_params.merge(params) unless params.nil? | ||
|
||
endpoint_uri = URI.parse(query_url) | ||
endpoint_uri.query = URI.encode_www_form(@query_params.to_a) unless @query_params.empty? | ||
|
||
http = Net::HTTP.new(endpoint_uri.host, endpoint_uri.port) | ||
http.use_ssl = true if endpoint_uri.scheme == 'https' | ||
|
||
net_response = http.start do |h| | ||
api_request = Net::HTTP::Post.new(endpoint_uri.request_uri) | ||
add_headers(api_request) | ||
api_request.body = body unless body.nil? | ||
|
||
h.request api_request | ||
end | ||
|
||
handle_errors(net_response) | ||
|
||
build_response(net_response) | ||
end | ||
|
||
private | ||
|
||
# @attr [String] base_url the base url of our api. (expected: http://example.com - without the trailing slash). | ||
|
@@ -133,17 +179,17 @@ def add_headers(request) | |
end | ||
end | ||
|
||
def handle_errors(response) | ||
case response | ||
def handle_errors(net_response) | ||
case net_response | ||
when Net::HTTPOK # 2xx Status | ||
exception_class = Parliament::NoContentResponseError if response['Content-Length'] == '0' | ||
exception_class = Parliament::NoContentResponseError if net_response['Content-Length'] == '0' | ||
when Net::HTTPClientError # 4xx Status | ||
exception_class = Parliament::ClientError | ||
when Net::HTTPServerError # 5xx Status | ||
exception_class = Parliament::ServerError | ||
end | ||
|
||
raise exception_class.new(query_url, response) if exception_class | ||
raise exception_class.new(query_url, net_response) if exception_class | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Parliament | ||
VERSION = '0.8.0'.freeze | ||
VERSION = '0.9.0'.freeze | ||
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.
Line is too long. [171/120]