class BridgeBankin::API::Client
Allows to request the Bridge API
using Ruby native net/http library
Constants
- HTTP_VERBS_MAP
Attributes
access_token[RW]
Public Instance Methods
delete(path, **params)
click to toggle source
get(path, **params)
click to toggle source
Handles a GET request
@param [String] path the API
endpoint PATH to query @param [Hash] params any params that might be required (or optional) to communicate with the API
@return [Hash] the parsed API
response
@raise [API::Error] expectation if API
responding with any error
# File lib/bridge_bankin/api/client.rb, line 34 def get(path, **params) request :get, path, params end
post(path, **params)
click to toggle source
Private Instance Methods
encode_path(path, params = nil)
click to toggle source
# File lib/bridge_bankin/api/client.rb, line 150 def encode_path(path, params = nil) URI::HTTP .build(path: path, query: URI.encode_www_form(params)) .request_uri end
follow_pages()
click to toggle source
# File lib/bridge_bankin/api/client.rb, line 117 def follow_pages BridgeBankin.configuration.follow_pages end
handle_error(api_response)
click to toggle source
# File lib/bridge_bankin/api/client.rb, line 156 def handle_error(api_response) response_body = parse_response_body(api_response.body) case api_response.code when "400" raise API::BadRequestError.new(api_response.code, response_body) when "401" raise API::UnauthorizedError.new(api_response.code, response_body) when "403" raise API::ForbiddenError.new(api_response.code, response_body) when "404" raise API::NotFoundError.new(api_response.code, response_body) when "409" raise API::ConflictError.new(api_response.code, response_body) when "415" raise API::UnsupportedMediaTypeError.new(api_response.code, response_body) when "422" raise API::UnprocessableEntityError.new(api_response.code, response_body) when "429" raise API::TooManyRequestsError.new(api_response.code, response_body) when "500" raise API::InternalServerError.new(api_response.code, response_body) else raise API::Error.new(api_response.code, response_body) end end
handle_paging(data)
click to toggle source
# File lib/bridge_bankin/api/client.rb, line 121 def handle_paging(data) if follow_pages && data[:pagination][:next_uri] page_uri = URI.parse(data[:pagination][:next_uri]) params = URI.decode_www_form(page_uri.query).to_h next_page_data = get(page_uri.path, **params) end next_page_data[:resources] << data[:resources] next_page_data[:resources] = next_page_data[:resources].flatten next_page_data end
headers()
click to toggle source
# File lib/bridge_bankin/api/client.rb, line 136 def headers headers = { "Bankin-Version" => BridgeBankin.configuration.api_version, "Client-Id" => BridgeBankin.configuration.api_client_id, "Client-Secret" => BridgeBankin.configuration.api_client_secret, "Content-Type" => "application/json" } return headers unless access_token headers.merge!("Authorization" => "Bearer #{access_token}") end
make_http_request() { || ... }
click to toggle source
# File lib/bridge_bankin/api/client.rb, line 88 def make_http_request Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| api_response = http.request(yield) case api_response.code when "200", "201" data = parse_response_body(api_response.body) if data.dig(:pagination, :next_uri) && follow_pages handle_paging(data) else data end when "204", "202" {} else handle_error(api_response) end end end
parse_response_body(json_response_body)
click to toggle source
# File lib/bridge_bankin/api/client.rb, line 109 def parse_response_body(json_response_body) JSON.parse(json_response_body, symbolize_names: true) end
request(method, path, params = {})
click to toggle source
# File lib/bridge_bankin/api/client.rb, line 80 def request(method, path, params = {}) make_http_request do HTTP_VERBS_MAP[method].new(encode_path(path, params), headers).tap do |request| request.set_form_data(params) if method != :get end end end
uri()
click to toggle source
# File lib/bridge_bankin/api/client.rb, line 113 def uri @uri ||= URI.parse(BridgeBankin.configuration.api_base_url) end