class Contentful::Management::Client

Client for interacting with the Contentful Management API @see _ www.contentful.com/developers/docs/references/content-management-api/

Constants

DEFAULT_CONFIGURATION

Default configuration for Contentful::Management::Client

RATE_LIMIT_RESET_HEADER_KEY

Rate Limit Reset Header Key

Attributes

access_token[R]
configuration[R]
content_type_id[RW]
dynamic_entry_cache[RW]
logger[R]
organization_id[RW]
version[RW]

Public Class Methods

new(access_token = nil, configuration = {}) click to toggle source

@param [String] access_token @param [Hash] configuration @option configuration [String] :api_url @option configuration [String] :api_version @option configuration [String] :default_locale @option configuration [Boolean] :gzip_encoded @option configuration [false, ::Logger] :logger @option configuration [::Logger::DEBUG, ::Logger::INFO, ::Logger::WARN, ::Logger::ERROR] :log_level @option configuration [Boolean] :raise_errors @option configuration [::Array<String>] :dynamic_entries @option configuration [Boolean] :disable_content_type_caching @option configuration [String] :proxy_host @option configuration [Fixnum] :proxy_port @option configuration [String] :proxy_username @option configuration [String] :proxy_username @option configuration [String] :application_name @option configuration [String] :application_version @option configuration [String] :integration_name @option configuration [String] :integration_version

# File lib/contentful/management/client.rb, line 91
def initialize(access_token = nil, configuration = {})
  @configuration = default_configuration.merge(configuration)
  setup_logger
  @access_token = access_token
  @dynamic_entry_cache = {}
  Thread.current[:client] = self
  update_all_dynamic_entry_cache!
end
shared_instance() click to toggle source

@private

# File lib/contentful/management/client.rb, line 632
def self.shared_instance
  Thread.current[:client]
end

Public Instance Methods

accept_encoding_header(encoding) click to toggle source

@private

# File lib/contentful/management/client.rb, line 611
def accept_encoding_header(encoding)
  Hash['Accept-Encoding', encoding]
end
api_keys(space_id) click to toggle source

Allows manipulation of api keys in context of the current client Allows listing all api keys for client, creating new and finding one by ID. @see _ README for details.

@return [Contentful::Management::ClientApiKeyMethodsFactory]

# File lib/contentful/management/client.rb, line 166
def api_keys(space_id)
  ClientApiKeyMethodsFactory.new(self, space_id)
end
api_version() click to toggle source

@private

# File lib/contentful/management/client.rb, line 357
def api_version
  configuration[:api_version]
end
api_version_header() click to toggle source

@private

# File lib/contentful/management/client.rb, line 523
def api_version_header
  Hash['Content-Type', "application/vnd.contentful.management.v#{api_version}+json"]
end
app_info() click to toggle source

Returns the X-Contentful-User-Agent app data @private

# File lib/contentful/management/client.rb, line 543
def app_info
  { name: configuration[:application_name], version: configuration[:application_version] }
end
assets(space_id, environment_id) click to toggle source

Allows manipulation of assets in context of the current client Allows listing all assets for client, creating new and finding one by ID. @see _ README for details.

@return [Contentful::Management::ClientAssetMethodsFactory]

# File lib/contentful/management/client.rb, line 193
def assets(space_id, environment_id)
  ClientAssetMethodsFactory.new(self, space_id, environment_id)
end
authentication_header() click to toggle source

@private

# File lib/contentful/management/client.rb, line 518
def authentication_header
  Hash['Authorization', "Bearer #{access_token}"]
end
base_url() click to toggle source

@private

# File lib/contentful/management/client.rb, line 498
def base_url
  "#{protocol}://#{configuration[:api_url]}/"
end
clear_headers() click to toggle source

@private

# File lib/contentful/management/client.rb, line 424
def clear_headers
  self.content_type_id = nil
  self.version = nil
  self.organization_id = nil
end
content_type_header(content_type_id) click to toggle source

@private

# File lib/contentful/management/client.rb, line 606
def content_type_header(content_type_id)
  Hash['X-Contentful-Content-Type', content_type_id]
end
content_type_snapshots(space_id, environment_id) click to toggle source

Allows manipulation of content type snapshots in context of the current client Allows listing all content type snapshots for client and finding one by ID. @see _ README for details.

@return [Contentful::Management::ClientSnapshotMethodsFactory]

# File lib/contentful/management/client.rb, line 310
def content_type_snapshots(space_id, environment_id)
  ClientSnapshotMethodsFactory.new(self, space_id, environment_id, 'content_types')
end
content_types(space_id, environment_id) click to toggle source

Allows manipulation of content types in context of the current client Allows listing all content types for client, creating new and finding one by ID. @see _ README for details.

@return [Contentful::Management::ClientContentTypeMethodsFactory]

# File lib/contentful/management/client.rb, line 202
def content_types(space_id, environment_id)
  ClientContentTypeMethodsFactory.new(self, space_id, environment_id)
end
contentful_user_agent() click to toggle source

Returns the X-Contentful-User-Agent @private

# File lib/contentful/management/client.rb, line 572
def contentful_user_agent
  header = {
    'sdk' => sdk_info,
    'app' => app_info,
    'integration' => integration_info,
    'platform' => platform_info,
    'os' => os_info
  }

  result = []
  header.each do |key, values|
    next unless values[:name]
    result << format_user_agent_header(key, values)
  end

  result.join(' ')
end
default_configuration() click to toggle source

@private

# File lib/contentful/management/client.rb, line 367
def default_configuration
  DEFAULT_CONFIGURATION.dup
end
default_locale() click to toggle source

@private

# File lib/contentful/management/client.rb, line 508
def default_locale
  configuration[:default_locale]
end
delete(request) click to toggle source

@private

# File lib/contentful/management/client.rb, line 431
def delete(request)
  execute_request(request) do |url|
    http_send(:delete, url, { params: request.query }, request_headers(request), proxy_parameters)
  end
end
editor_interfaces(space_id, environment_id, content_type_id) click to toggle source

Allows manipulation of editor interfaces in context of the current client Allows listing all editor interfaces for client and finding one by content type. @see _ README for details.

@return [Contentful::Management::ClientEditorInterfaceMethodsFactory]

# File lib/contentful/management/client.rb, line 247
def editor_interfaces(space_id, environment_id, content_type_id)
  ClientEditorInterfaceMethodsFactory.new(self, space_id, environment_id, content_type_id)
end
entries(space_id, environment_id) click to toggle source

Allows manipulation of entries in context of the current client Allows listing all entries for client, creating new and finding one by ID. @see _ README for details.

@return [Contentful::Management::ClientEntryMethodsFactory]

# File lib/contentful/management/client.rb, line 211
def entries(space_id, environment_id)
  ClientEntryMethodsFactory.new(self, space_id, environment_id)
end
entry_snapshots(space_id, environment_id) click to toggle source

Allows manipulation of entry snapshots in context of the current client Allows listing all entry snapshots for client and finding one by ID. @see _ README for details.

@return [Contentful::Management::ClientSnapshotMethodsFactory]

# File lib/contentful/management/client.rb, line 301
def entry_snapshots(space_id, environment_id)
  ClientSnapshotMethodsFactory.new(self, space_id, environment_id, 'entries')
end
environments(space_id) click to toggle source

Allows manipulation of environments in context of the current client Allows listing all environments for client, creating new and finding one by ID. @see _ README for details.

@return [Contentful::Management::ClientEnvironmentMethodsFactory]

# File lib/contentful/management/client.rb, line 114
def environments(space_id)
  ClientEnvironmentMethodsFactory.new(self, space_id)
end
execute_request(request) { |url| ... } click to toggle source

@private

# File lib/contentful/management/client.rb, line 377
def execute_request(request)
  retries_left = configuration[:max_rate_limit_retries]
  host = host_url(request)
  url = request.absolute? ? request.url : host + request.url

  begin
    logger.info(request: { url: url, query: request.query, header: request_headers(request) }) if logger
    raw_response = yield(url)
    logger.debug(response: raw_response) if logger
    clear_headers
    result = Response.new(raw_response, request)
    fail result.object if result.object.is_a?(Error) && configuration[:raise_errors]
  rescue Contentful::Management::RateLimitExceeded => rate_limit_error
    reset_time = rate_limit_error.response.raw[RATE_LIMIT_RESET_HEADER_KEY].to_i
    if should_retry(retries_left, reset_time, configuration[:max_rate_limit_wait])
      retries_left -= 1
      logger.info(retry_message(retries_left, reset_time)) if logger
      sleep(reset_time * Random.new.rand(1.0..1.2))
      retry
    end

    raise
  end

  result
end
format_user_agent_header(key, values) click to toggle source

Returns the formatted part of the X-Contentful-User-Agent header @private

# File lib/contentful/management/client.rb, line 529
def format_user_agent_header(key, values)
  header = "#{key} #{values[:name]}"
  header = "#{header}/#{values[:version]}" if values[:version]
  "#{header};"
end
get(request) click to toggle source

@private

# File lib/contentful/management/client.rb, line 438
def get(request)
  execute_request(request) do |url|
    http_send(:get, url, { params: request.query }, request_headers(request), proxy_parameters)
  end
end
gzip_encoded() click to toggle source

@private

# File lib/contentful/management/client.rb, line 362
def gzip_encoded
  configuration[:gzip_encoded]
end
host_url(request) click to toggle source

@private

# File lib/contentful/management/client.rb, line 405
def host_url(request)
  %r{^spaces/[\w|-|_]+/uploads(?:/[\w|-|_]*)?$} =~ request.url ? uploads_url : base_url
end
http_send(type, url, params, headers, proxy) click to toggle source

HTTP Helper Abtracts the Proxy/No-Proxy logic

@param [Symbol] type @param [String] url @param [Hash] params @param [Hash] headers @param [Hash] proxy

@return [HTTP::Response]

# File lib/contentful/management/client.rb, line 492
def http_send(type, url, params, headers, proxy)
  return proxy_send(type, url, params, headers, proxy) unless proxy[:host].nil?
  HTTP[headers].public_send(type, url, params)
end
integration_info() click to toggle source

Returns the X-Contentful-User-Agent integration data @private

# File lib/contentful/management/client.rb, line 549
def integration_info
  { name: configuration[:integration_name], version: configuration[:integration_version] }
end
locales(space_id, environment_id) click to toggle source

Allows manipulation of locales in context of the current client Allows listing all locales for client, creating new and finding one by ID. @see _ README for details.

@return [Contentful::Management::ClientLocaleMethodsFactory]

# File lib/contentful/management/client.rb, line 220
def locales(space_id, environment_id)
  ClientLocaleMethodsFactory.new(self, space_id, environment_id)
end
organization_header(organization_id) click to toggle source

@private

# File lib/contentful/management/client.rb, line 596
def organization_header(organization_id)
  Hash['X-Contentful-Organization', organization_id]
end
organization_periodic_usages(organization_id) click to toggle source

Allows listing all usage periods for organization grouped by organization. @see _ README for details.

@return [Contentful::Management::ClientOrganizationPeriodicUsageMethodsFactory]

# File lib/contentful/management/client.rb, line 140
def organization_periodic_usages(organization_id)
  ClientOrganizationPeriodicUsageMethodsFactory.new(self, organization_id)
end
organizations() click to toggle source

Allows viewing of organizations in context of the current client Allows listing all organizations for client. @see _ README for details.

@return [Contentful::Management::ClientOrganizationMethodsFactory]

# File lib/contentful/management/client.rb, line 132
def organizations
  ClientOrganizationMethodsFactory.new(self)
end
os_info() click to toggle source

Returns the X-Contentful-User-Agent os data @private

# File lib/contentful/management/client.rb, line 561
def os_info
  os_name = case ::RbConfig::CONFIG['host_os']
            when /(cygwin|mingw|mswin|windows)/i then 'Windows'
            when /(darwin|macruby|mac os)/i      then 'macOS'
            when /(linux|bsd|aix|solarix)/i      then 'Linux'
            end
  { name: os_name, version: Gem::Platform.local.version }
end
personal_access_tokens() click to toggle source

Allows manipulation of personal access tokens in context of the current client Allows listing all personal access tokens for client, creating new and finding one by ID. @see _ README for details.

@return [Contentful::Management::ClientPersonalAccessTokenMethodsFactory]

# File lib/contentful/management/client.rb, line 184
def personal_access_tokens
  ClientPersonalAccessTokenMethodsFactory.new(self)
end
platform_info() click to toggle source

Returns the X-Contentful-User-Agent platform data @private

# File lib/contentful/management/client.rb, line 555
def platform_info
  { name: 'ruby', version: RUBY_VERSION }
end
post(request) click to toggle source

@private

# File lib/contentful/management/client.rb, line 445
def post(request)
  execute_request(request) do |url|
    data = if url.include?(Client::DEFAULT_CONFIGURATION[:uploads_url])
             { body: request.query }
           else
             { json: request.query }
           end

    http_send(:post, url, data, request_headers(request), proxy_parameters)
  end
end
preview_api_keys(space_id) click to toggle source

Allows manipulation of api keys in context of the current client Allows listing all preview api keys for client and finding one by ID. @see _ README for details.

@return [Contentful::Management::ClientPreviewApiKeyMethodsFactory]

# File lib/contentful/management/client.rb, line 175
def preview_api_keys(space_id)
  ClientPreviewApiKeyMethodsFactory.new(self, space_id)
end
protocol() click to toggle source

@private

# File lib/contentful/management/client.rb, line 513
def protocol
  configuration[:secure] ? 'https' : 'http'
end
proxy_parameters() click to toggle source

@private

# File lib/contentful/management/client.rb, line 637
def proxy_parameters
  {
    host: configuration[:proxy_host],
    port: configuration[:proxy_port],
    username: configuration[:proxy_username],
    password: configuration[:proxy_password]
  }
end
proxy_send(type, url, params, headers, proxy) click to toggle source

Proxy Helper

@param [Symbol] type @param [String] url @param [Hash] params @param [Hash] headers @param [Hash] proxy

@return [HTTP::Response]

# File lib/contentful/management/client.rb, line 473
def proxy_send(type, url, params, headers, proxy)
  HTTP[headers].via(
    proxy[:host],
    proxy[:port],
    proxy[:username],
    proxy[:password]
  ).public_send(type, url, params)
end
put(request) click to toggle source

@private

# File lib/contentful/management/client.rb, line 458
def put(request)
  execute_request(request) do |url|
    http_send(:put, url, { json: request.query }, request_headers(request), proxy_parameters)
  end
end
register_dynamic_entry(key, klass) click to toggle source

@private

# File lib/contentful/management/client.rb, line 372
def register_dynamic_entry(key, klass)
  @dynamic_entry_cache[key.to_sym] = klass
end
request_headers(request = nil) click to toggle source

@todo headers should be supplied differently, maybe through the request object. @private

# File lib/contentful/management/client.rb, line 617
def request_headers(request = nil)
  headers = {}
  headers.merge! user_agent
  headers.merge! authentication_header
  headers.merge! api_version_header
  headers.merge! organization_header(organization_id) if organization_id
  headers.merge! version_header(version) if version
  headers.merge! content_type_header(content_type_id) if content_type_id
  headers.merge! accept_encoding_header('gzip') if gzip_encoded

  headers.merge! request.headers unless request.nil?
  headers
end
retry_message(retries_left, reset_time) click to toggle source

@private

# File lib/contentful/management/client.rb, line 410
def retry_message(retries_left, reset_time)
  retry_message = 'Contentful Management API Rate Limit Hit! '
  retry_message += "Retrying - Retries left: #{retries_left}"
  retry_message += "- Time until reset (seconds): #{reset_time}"

  retry_message
end
roles(space_id) click to toggle source

Allows manipulation of roles in context of the current client Allows listing all roles for client, creating new and finding one by ID. @see _ README for details.

@return [Contentful::Management::ClientRoleMethodsFactory]

# File lib/contentful/management/client.rb, line 229
def roles(space_id)
  ClientRoleMethodsFactory.new(self, space_id)
end
sdk_info() click to toggle source

Returns the X-Contentful-User-Agent sdk data @private

# File lib/contentful/management/client.rb, line 537
def sdk_info
  { name: 'contentful-management.rb', version: ::Contentful::Management::VERSION }
end
setup_logger() click to toggle source

@private

# File lib/contentful/management/client.rb, line 315
def setup_logger
  @logger = configuration[:logger]
  logger.level = configuration[:log_level] if logger
end
should_retry(retries_left, reset_time, max_wait) click to toggle source

@private

# File lib/contentful/management/client.rb, line 419
def should_retry(retries_left, reset_time, max_wait)
  retries_left > 0 && max_wait > reset_time
end
snapshots(space_id, environment_id, resource_type = 'entries') click to toggle source

Allows manipulation of snapshots in context of the current client Allows listing all snapshots for client and finding one by ID. @see _ README for details.

@return [Contentful::Management::ClientSnapshotMethodsFactory]

# File lib/contentful/management/client.rb, line 292
def snapshots(space_id, environment_id, resource_type = 'entries')
  ClientSnapshotMethodsFactory.new(self, space_id, environment_id, resource_type)
end
space_memberships(space_id) click to toggle source

Allows manipulation of space memberships in context of the current client Allows listing all space memberships for client, creating new and finding one by ID. @see _ README for details.

@return [Contentful::Management::ClientSpaceMembershipMethodsFactory]

# File lib/contentful/management/client.rb, line 123
def space_memberships(space_id)
  ClientSpaceMembershipMethodsFactory.new(self, space_id)
end
space_periodic_usages(organization_id) click to toggle source

Allows listing all usage periods for organization grouped by space. @see _ README for details.

@return [Contentful::Management::ClientSpacePeriodicUsageMethodsFactory]

# File lib/contentful/management/client.rb, line 148
def space_periodic_usages(organization_id)
  ClientSpacePeriodicUsageMethodsFactory.new(self, organization_id)
end
spaces() click to toggle source

Allows manipulation of spaces in context of the current client Allows listing all spaces for client, creating new and finding one by ID. @see _ README for details.

@return [Contentful::Management::ClientSpaceMethodsFactory]

# File lib/contentful/management/client.rb, line 105
def spaces
  ClientSpaceMethodsFactory.new(self)
end
ui_extensions(space_id, environment_id) click to toggle source

Allows manipulation of UI extensions in context of the current client Allows listing all UI extensions for client, creating new and finding one by ID. @see _ README for details.

@return [Contentful::Management::ClientUIExtensionMethodsFactory]

# File lib/contentful/management/client.rb, line 238
def ui_extensions(space_id, environment_id)
  ClientUIExtensionMethodsFactory.new(self, space_id, environment_id)
end
update_all_dynamic_entry_cache!() click to toggle source

@private

# File lib/contentful/management/client.rb, line 321
def update_all_dynamic_entry_cache!
  return if configuration[:dynamic_entries].empty? || configuration[:disable_content_type_caching]

  environments = configuration[:dynamic_entries].map do |space_id, environment_id|
    ::Contentful::Management::Environment.find(self, space_id, environment_id)
  end

  update_dynamic_entry_cache_for_environments!(environments)
end
update_dynamic_entry_cache!(content_types) click to toggle source

@private

# File lib/contentful/management/client.rb, line 350
def update_dynamic_entry_cache!(content_types)
  content_types.each do |ct|
    @dynamic_entry_cache[ct.id.to_sym] = DynamicEntry.create(ct, self)
  end
end
update_dynamic_entry_cache_for_environment!(environment) click to toggle source

Use this method together with the client's :dynamic_entries configuration. See README for details. @private

# File lib/contentful/management/client.rb, line 343
def update_dynamic_entry_cache_for_environment!(environment)
  return if configuration[:disable_content_type_caching]

  update_dynamic_entry_cache!(environment.content_types.all)
end
update_dynamic_entry_cache_for_environments!(environments) click to toggle source

@private

# File lib/contentful/management/client.rb, line 332
def update_dynamic_entry_cache_for_environments!(environments)
  return if configuration[:disable_content_type_caching]

  environments.each do |environment|
    update_dynamic_entry_cache_for_environment!(environment)
  end
end
uploads(space_id) click to toggle source

Allows manipulation of uploads in context of the current client Allows creating new and finding uploads by ID. @see _ README for details.

@return [Contentful::Management::ClientUploadMethodsFactory]

# File lib/contentful/management/client.rb, line 283
def uploads(space_id)
  ClientUploadMethodsFactory.new(self, space_id)
end
uploads_url() click to toggle source

@private

# File lib/contentful/management/client.rb, line 503
def uploads_url
  "#{protocol}://#{configuration[:uploads_url]}/"
end
user_agent() click to toggle source

@private

# File lib/contentful/management/client.rb, line 591
def user_agent
  Hash['X-Contentful-User-Agent', contentful_user_agent]
end
users() click to toggle source

Allows viewing of users in context of the current client Allows listing all users for client. @see _ README for details.

@return [Contentful::Management::ClientUserMethodsFactory]

# File lib/contentful/management/client.rb, line 157
def users
  ClientUserMethodsFactory.new(self)
end
version_header(version) click to toggle source

@private

# File lib/contentful/management/client.rb, line 601
def version_header(version)
  Hash['X-Contentful-Version', version]
end
webhook_calls(space_id, webhook_id) click to toggle source

Allows manipulation of webhook calls in context of the current client Allows listing all webhook call details for client and finding one by ID. @see _ README for details.

@return [Contentful::Management::ClientWebhookCallMethodsFactory]

# File lib/contentful/management/client.rb, line 265
def webhook_calls(space_id, webhook_id)
  ClientWebhookCallMethodsFactory.new(self, space_id, webhook_id)
end
webhook_health(space_id) click to toggle source

Allows manipulation of webhook health in context of the current client Allows listing all webhook health details for client and finding one by ID. @see _ README for details.

@return [Contentful::Management::ClientWebhookHealthMethodsFactory]

# File lib/contentful/management/client.rb, line 274
def webhook_health(space_id)
  ClientWebhookHealthMethodsFactory.new(self, space_id)
end
webhooks(space_id) click to toggle source

Allows manipulation of webhooks in context of the current client Allows listing all webhooks for client, creating new and finding one by ID. @see _ README for details.

@return [Contentful::Management::ClientWebhookMethodsFactory]

# File lib/contentful/management/client.rb, line 256
def webhooks(space_id)
  ClientWebhookMethodsFactory.new(self, space_id)
end