Class: WCC::Contentful::SimpleClient
- Inherits:
-
Object
- Object
- WCC::Contentful::SimpleClient
- 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
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
- #api_url ⇒ Object readonly
- #environment ⇒ Object readonly
- #space ⇒ Object readonly
Attributes included from Instrumentation
Class Method Summary collapse
Instance Method Summary collapse
-
#get(path, query = {}) ⇒ Object
performs an HTTP GET request to the specified path within the configured space and environment.
-
#initialize(api_url:, space:, access_token:, **options) ⇒ SimpleClient
constructor
Creates a new SimpleClient with the given configuration.
Methods included from Instrumentation
Constructor Details
#initialize(api_url:, space:, access_token:, **options) ⇒ SimpleClient
Creates a new SimpleClient with the given configuration.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/wcc/contentful/simple_client.rb', line 43 def initialize(api_url:, space:, access_token:, **) @api_url = URI.join(api_url, '/spaces/', "#{space}/") @space = space @access_token = access_token @adapter = SimpleClient.load_adapter([:connection]) @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 @environment = [:environment] return unless @environment.present? @api_url = URI.join(@api_url, 'environments/', "#{@environment}/") end |
Instance Attribute Details
#api_url ⇒ Object (readonly)
29 30 31 |
# File 'lib/wcc/contentful/simple_client.rb', line 29 def api_url @api_url end |
#environment ⇒ Object (readonly)
29 30 31 |
# File 'lib/wcc/contentful/simple_client.rb', line 29 def environment @environment end |
#space ⇒ Object (readonly)
29 30 31 |
# File 'lib/wcc/contentful/simple_client.rb', line 29 def space @space end |
Class Method Details
.load_adapter(adapter) ⇒ Object
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 110 |
# File 'lib/wcc/contentful/simple_client.rb', line 83 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.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/wcc/contentful/simple_client.rb', line 66 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 |