Class: HTTP_Client
- Inherits:
-
Object
- Object
- HTTP_Client
- Defined in:
- lib/external_api_service/http_client.rb
Instance Method Summary collapse
- #build_post_request(http_params) ⇒ Object
- #get(endpoint_uri, credentials) ⇒ Object
- #make_request(endpoint, formatted_request) ⇒ Object
- #parse_response(response) ⇒ Object
- #post(http_params) ⇒ Object
Instance Method Details
#build_post_request(http_params) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/external_api_service/http_client.rb', line 40 def build_post_request(http_params) http_params[:header_params]["Content-Type"] = "application/json" new_request = Net::HTTP::Post.new(http_params[:uri], http_params[:header_params]) new_request.basic_auth(http_params[:credentials][0], http_params[:credentials][1]) new_request.body = http_params[:data].to_json new_request end |
#get(endpoint_uri, credentials) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/external_api_service/http_client.rb', line 6 def get(endpoint_uri, credentials) new_request = Net::HTTP::Get.new(endpoint_uri) new_request.basic_auth(credentials[0], credentials[1]) if !credentials.empty? response = make_request(endpoint_uri, new_request) parse_response(response) end |
#make_request(endpoint, formatted_request) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/external_api_service/http_client.rb', line 19 def make_request(endpoint, formatted_request) check_ssl = (endpoint.scheme == "https") Net::HTTP.start(endpoint.host, endpoint.port, :use_ssl => check_ssl) do |client| retries = 5 begin client.request(formatted_request) rescue Exception => ex retries -= 1 retries > 0 ? retry : error_response(422, ex) end end end |
#parse_response(response) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/external_api_service/http_client.rb', line 32 def parse_response(response) if (response.code =~ /^2/ ) JSON.parse(response.body, symbolize_names: true) else { error: response } end end |
#post(http_params) ⇒ Object
13 14 15 16 17 |
# File 'lib/external_api_service/http_client.rb', line 13 def post(http_params) new_request = build_post_request(http_params) response = make_request(http_params[:uri], new_request) parse_response(response) end |