Class: WCC::Contentful::SimpleClient

Inherits:
Object
  • Object
show all
Includes:
Instrumentation
Defined in:
lib/wcc/contentful/simple_client.rb,
lib/wcc/contentful/simple_client/response.rb

Overview

The SimpleClient accesses the Contentful CDN to get JSON responses, returning the raw JSON data as a parsed hash. This is the bottom layer of the WCC::Contentful gem.

Note: Do not create this directly, instead create one of WCC::Contentful::SimpleClient::Cdn, WCC::Contentful::SimpleClient::Preview, WCC::Contentful::SimpleClient::Management

It can be configured to access any API url and exposes only a single method, ‘get`. This method returns a WCC::Contentful::SimpleClient::Response that handles paging automatically.

The SimpleClient by default uses ‘faraday’ to perform the gets, but any HTTP client adapter be injected by passing the ‘connection:` option.

Direct Known Subclasses

Cdn, Management

Defined Under Namespace

Classes: ApiError, Cdn, Management, NotFoundError, PaginatingEnumerable, Preview, RateLimitError, Response, SyncResponse, TyphoeusAdapter, UnauthorizedError

Constant Summary collapse

ADAPTERS =
{
  faraday: ['faraday', '>= 0.9'],
  typhoeus: ['typhoeus', '~> 1.0']
}.freeze

Instance Attribute Summary collapse

Attributes included from Instrumentation

#_instrumentation

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Instrumentation

instrument

Constructor Details

#initialize(api_url:, space:, access_token:, **options) ⇒ SimpleClient

Creates a new SimpleClient with the given configuration.

Parameters:

  • api_url (String)

    the base URL of the Contentful API to connect to

  • space (String)

    The Space ID to access

  • access_token (String)

    A Contentful Access Token to be sent in the Authorization header

  • options (Hash)

    The remaining optional parameters, defined below

Options Hash (**options):

  • connection (Symbol, Object)

    The Faraday connection to use to make requests. Auto-discovered based on what gems are installed if this is not provided.

  • environment (String)

    The contentful environment to access. Defaults to ‘master’.

  • no_follow_redirects (Boolean)

    If true, do not follow 300 level redirects.

  • rate_limit_wait_timeout (Number)

    The maximum time to block the thread waiting on a rate limit response. By default will wait for one 429 and then fail on the second 429.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/wcc/contentful/simple_client.rb', line 43

def initialize(api_url:, space:, access_token:, **options)
  @api_url = URI.join(api_url, '/spaces/', "#{space}/")
  @space = space
  @access_token = access_token

  @adapter = SimpleClient.load_adapter(options[:connection])

  @options = options
  @_instrumentation = @options[:instrumentation]
  @query_defaults = {}
  # default 1.5 so that we retry one time then fail if still rate limited
  # https://www.contentful.com/developers/docs/references/content-preview-api/#/introduction/api-rate-limits
  @rate_limit_wait_timeout = @options[:rate_limit_wait_timeout] || 1.5

  return unless options[:environment].present?

  @api_url = URI.join(@api_url, 'environments/', "#{options[:environment]}/")
end

Instance Attribute Details

#api_urlObject (readonly)



29
30
31
# File 'lib/wcc/contentful/simple_client.rb', line 29

def api_url
  @api_url
end

#spaceObject (readonly)



29
30
31
# File 'lib/wcc/contentful/simple_client.rb', line 29

def space
  @space
end

Class Method Details

.load_adapter(adapter) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/wcc/contentful/simple_client.rb', line 82

def self.load_adapter(adapter)
  case adapter
  when nil
    ADAPTERS.each do |a, spec|
      gem(*spec)
      return load_adapter(a)
    rescue Gem::LoadError
      next
    end
    raise ArgumentError, 'Unable to load adapter!  Please install one of ' \
                         "#{ADAPTERS.values.map(&:join).join(',')}"
  when :faraday
    require 'faraday'
    ::Faraday.new do |faraday|
      faraday.response :logger, (Rails.logger if defined?(Rails)), { headers: false, bodies: false }
      faraday.adapter :net_http
    end
  when :typhoeus
    require_relative 'simple_client/typhoeus_adapter'
    TyphoeusAdapter.new
  else
    unless adapter.respond_to?(:get)
      raise ArgumentError, "Adapter #{adapter} is not invokeable!  Please " \
                           "pass use one of #{ADAPTERS.keys} or create a Faraday-compatible adapter"
    end
    adapter
  end
end

Instance Method Details

#get(path, query = {}) ⇒ Object

performs an HTTP GET request to the specified path within the configured space and environment. Query parameters are merged with the defaults and appended to the request.



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/wcc/contentful/simple_client.rb', line 65

def get(path, query = {})
  url = URI.join(@api_url, path)

  resp =
    _instrument 'get_http', url: url, query: query do
      get_http(url, query)
    end
  Response.new(self,
    { url: url, query: query },
    resp)
end