Class: Redd::APIClient

Inherits:
Client
  • Object
show all
Defined in:
lib/redd/api_client.rb

Overview

The class for API clients.

Constant Summary collapse

API_ENDPOINT =

The endpoint to make API requests to.

'https://oauth.reddit.com'

Constants inherited from Client

Client::USER_AGENT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Client

#delete, #get, #patch, #post, #put

Constructor Details

#initialize(auth, endpoint: API_ENDPOINT, user_agent: USER_AGENT, limit_time: 1, max_retries: 5, auto_refresh: true) ⇒ APIClient

Create a new API client with an auth strategy.

Parameters:

  • auth (AuthStrategies::AuthStrategy)

    the auth strategy to use

  • endpoint (String) (defaults to: API_ENDPOINT)

    the API endpoint

  • user_agent (String) (defaults to: USER_AGENT)

    the user agent to send

  • limit_time (Integer) (defaults to: 1)

    the minimum number of seconds between each request

  • max_retries (Integer) (defaults to: 5)

    number of times to retry requests that may succeed if retried

  • auto_refresh (Boolean) (defaults to: true)

    automatically refresh access token if nearing expiration


25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/redd/api_client.rb', line 25

def initialize(auth, endpoint: API_ENDPOINT, user_agent: USER_AGENT, limit_time: 1,
               max_retries: 5, auto_refresh: true)
  super(endpoint: endpoint, user_agent: user_agent)

  @auth          = auth
  @access        = nil
  @max_retries   = max_retries
  @failures      = 0
  @error_handler = Utilities::ErrorHandler.new
  @rate_limiter  = Utilities::RateLimiter.new(limit_time)
  @unmarshaller  = Utilities::Unmarshaller.new(self)
  @auto_refresh  = auto_refresh
end

Instance Attribute Details

#accessAPIClient

Returns the access the client uses.

Returns:


16
17
18
# File 'lib/redd/api_client.rb', line 16

def access
  @access
end

Instance Method Details

#authenticate(*args) ⇒ Object

Authenticate the client using the provided auth.


40
41
42
# File 'lib/redd/api_client.rb', line 40

def authenticate(*args)
  @access = @auth.authenticate(*args)
end

#model(verb, path, options = {}) ⇒ Object


59
60
61
62
# File 'lib/redd/api_client.rb', line 59

def model(verb, path, options = {})
  # XXX: make unmarshal explicit in methods?
  unmarshal(send(verb, path, options).body)
end

#refreshObject

Refresh the access currently in use.


45
46
47
# File 'lib/redd/api_client.rb', line 45

def refresh
  @access = @auth.refresh(@access)
end

#request(verb, path, raw: false, params: {}, **options) ⇒ Object

Makes a request, ensuring not to break the rate limit by sleeping.

See Also:


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/redd/api_client.rb', line 66

def request(verb, path, raw: false, params: {}, **options)
  # Make sure @access is populated by a valid access
  ensure_access_is_valid
  # Setup base API params and make request
  api_params = { api_type: 'json', raw_json: 1 }.merge(params)

  # This loop is retried @max_retries number of times until it succeeds
  handle_retryable_errors do
    response = @rate_limiter.after_limit { super(verb, path, params: api_params, **options) }
    # Raise errors if encountered at the API level.
    response_error = @error_handler.check_error(response, raw: raw)
    raise response_error unless response_error.nil?
    # All done, return the response
    response
  end
end

#revokeObject

Revoke the current access and remove it from the client.


50
51
52
53
# File 'lib/redd/api_client.rb', line 50

def revoke
  @auth.revoke(@access)
  @access = nil
end

#unmarshal(object) ⇒ Object


55
56
57
# File 'lib/redd/api_client.rb', line 55

def unmarshal(object)
  @unmarshaller.unmarshal(object)
end