Skip to content

The CartoDB ruby http client

Rafa de la Torre edited this page Jul 7, 2015 · 7 revisions

Motivation

  • Be able to log requests, for debugging and stats purposes
  • Have a central point to set up common stuff, such as default timeouts and exception handling.

Design

Most http requests are done through Typhoeus. The class Carto::HttpClient wraps over the creation of Typhoeus::Request objects so that no big changes are required to make existing code work.

The tag is a string used to identify and group by the source of the requests. E.g: search_api, geocoder, etc.

Usage

require_relative 'lib/carto/http/client'

# Get a client with logger facilities
http_client = Carto::Http::Client.get('my_tag', log_requests: true)

# Create a wrapped request object
request = http_client.request("www.example.com",
                              method: :post,
                              body: "this is a request body",
                              params: { field1: "a field" },
                              headers: { Accept: "text/html" })

# Perform the request and get a Typhoeus::Response object
response = request.run

# Inspect the response code
response.code
=> 200

alternatively you can use get, post, put, etc. methods much like when using Typhoeus directly:

response = http_client.post("www.example.com",
                            body: "this is a request body",
                            params: { field1: "a field" },
                            headers: { Accept: "text/html" })

Log traces

The logging facility will spit traces to a log file "#{Rails.root}/log/http_client.log". They look like this:

{"tag":"downloader","hostname":"cartodbv3","method":"head","request_url":"https://gist.githubusercontent.com/rafatower/86760ea861ef4586e681/raw/10a35180ada27c7ea1eedc9a05f0d52438f5776f/five_countries.csv","total_time":0.037876,"response_code":200,"response_body_size":0}

Configuration

In order to enable logging, the following entry must be added to the configuration:

  http_client_logs: true
Clone this wiki locally