Skip to content
This repository

Koala uses Faraday to make requests to Facebook. Faraday is a modular framework, patterned on Rack, which makes it easy to control and customize how Ruby makes HTTP requests; Koala, in turn, allows you to take advantage of Faraday’s configuration options to tailor requests to your needs.

If you’d like further customize how Koala handles requests, see Extensions.

Note: for information on pre-1.2 HTTP configuration, please see this wiki page’s history.

Global HTTP Options

These options can be set on Koala.http_service (which outside of mock testing points to Koala::HTTPService), and will be applied to every request.

:faraday_middleware

With Faraday, you can build a stack of handlers that can massage, prepare, and process HTTP requests. You do this by passing a block to Faraday.new, building up your middleware stack. By default, Koala uses the following stack:

DEFAULT_MIDDLEWARE = Proc.new do |builder|
  builder.request :multipart
  builder.use Koala::MultipartRequest
  builder.request :url_encoded
  builder.adapter Faraday.default_adapter
end

This stack, by default, has three parts:

  • multipart: detects if you’re uploading files, and if so turns the request into a properly formatted multipart post
  • url_encoded: properly convert request params to “www-form-urlencoded”
  • default_adapter: uses whichever HTTP library you define as the default to make the request (:typheous, :net_http, :etc)

If you simply want to use a specific HTTP library, you can specify that in your application initialization, and Koala will automatically use it:

Faraday.default_adapter = :typhoeus
# for persistent connections using Faraday 0.8 and the net-http-persistent gem
Faraday.default_adapter = :net_http_persistent

If, on the other hand, you want to set up a custom middleware stack, you can provide a custom block:

Koala.http_service.faraday_middleware = Proc.new do |builder|
  builder.request :json
  builder.response :my_logging_middleware

  # remember: the default middleware and an adapter are necessary for Koala to work properly!
  builder.request :multipart
  builder.request :url_encoded
  builder.adapter Faraday.default_adapter
end

Koala will then use that when creating Faraday connections.

:http_options

You can specify http_options with every request, which let you control such things as SSL certificate configuration, proxies and timeouts, whether to use Facebook’s beta tier, etc. Most of the time, these are settings that you want used every time, and so you can set them at a global level. (This consolidates and replaces the various settings — :proxy, :timeout, etc. — previously available in Koala.)

The setting hash you provide with Koala.http_service.http_options will be used with every request. Any options passed to an individual request will take precedence over global settings.

Koala.http_service.http_options = {
  # Koala options
  :beta => true, # use FB beta layer
  # Faraday options
  :ssl => {
    # see below for info on ca_path and ca_file
    :ca_file => file,
    :ca_path => path,
    # SSL verify defaults to true unless you explicitly set it to false
    :verify => true
  },
  # see below for links to info on Faraday options
}

See the next section for available options.

HTTP Options for Koala

All options can be used either per-request or globally.

:beta

If set to true, the request is sent to the beta version of whichever API (Graph or REST) that you are using. Useful for ensuring your application won’t break as changes are released.

# Sends a request to http://beta.graph.facebook.com 
@graph.get_object('me', {}, {:beta => true})

:use_ssl

If set to true, Koala will make all requests or an individual request (depending on where it’s set) via SSL, regardless of the type of request. By default, requests without access tokens are done over http, and those with tokens are done over https (Facebook has hinted at eventually requiring SSL and tokens for all requests).

# Sends a request to https://graph.facebook.com 
@graph.get_object('me', {}, {:use_ssl => true})

SSL, proxy, timeout, and other Faraday options

Various HTTP For additional options will be available for your HTTP library of choice. Check out the implementation of the relevant adapter for more details.

:ca_file, :ca_path

You may find it necessary to specify the certificate file or path to avoid OpenSSL errors. For more information, see this blog entry (thanks to Nitin Pande for his help with this). These options can also be provided on a per-request basis.

# Ubuntu
(Koala.http_service.http_options[:ssl] ||= {})[:ca_path] = '/etc/ssl/certs'
# Mac OS X 
(Koala.http_service.http_options[:ssl] ||= {})[:ca_file] = '/opt/local/share/curl/curl-ca-bundle.crt'
Something went wrong with that request. Please try again.