Class: Redd::APIClient
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
Instance Attribute Summary collapse
-
#access ⇒ APIClient
The access the client uses.
Instance Method Summary collapse
-
#authenticate(*args) ⇒ Object
Authenticate the client using the provided auth.
-
#initialize(auth, endpoint: API_ENDPOINT, user_agent: USER_AGENT, limit_time: 1, max_retries: 5, auto_refresh: true) ⇒ APIClient
constructor
Create a new API client with an auth strategy.
- #model(verb, path, options = {}) ⇒ Object
-
#refresh ⇒ Object
Refresh the access currently in use.
-
#request(verb, path, raw: false, params: {}, **options) ⇒ Object
Makes a request, ensuring not to break the rate limit by sleeping.
-
#revoke ⇒ Object
Revoke the current access and remove it from the client.
- #unmarshal(object) ⇒ Object
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.
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
#access ⇒ APIClient
Returns the access the client uses.
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, = {}) # XXX: make unmarshal explicit in methods? unmarshal(send(verb, path, ).body) end |
#refresh ⇒ Object
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.
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: {}, **) # 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, **) } # 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 |
#revoke ⇒ Object
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 |