Class: AppnexusApi::Connection
- Inherits:
-
Object
- Object
- AppnexusApi::Connection
- Defined in:
- lib/appnexusapi/connection.rb
Constant Summary collapse
- RATE_EXCEEDED_DEFAULT_TIMEOUT =
15- RATE_EXCEEDED_HTTP_CODE =
429
Instance Method Summary collapse
- #delete(route, body = nil, headers = {}) ⇒ Object
- #get(route, params = {}, headers = {}) ⇒ Object
-
#initialize(config) ⇒ Connection
constructor
A new instance of Connection.
- #is_authorized? ⇒ Boolean
- #log ⇒ Object
- #login ⇒ Object
- #logout ⇒ Object
- #post(route, body = nil, headers = {}) ⇒ Object
- #put(route, body = nil, headers = {}) ⇒ Object
- #run_request(method, route, body, headers) ⇒ Object
Constructor Details
#initialize(config) ⇒ Connection
Returns a new instance of Connection.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/appnexusapi/connection.rb', line 9 def initialize(config) @config = config @config['uri'] ||= 'https://api.appnexus.com/' @logger = @config['logger'] || NullLogger.instance @connection = Faraday.new(@config['uri']) do |conn| conn.response :logger, @logger, bodies: true conn.request :json conn.response :json, :content_type => /\bjson$/ conn.use AppnexusApi::Faraday::Response::RaiseHttpError conn.adapter Faraday.default_adapter end end |
Instance Method Details
#delete(route, body = nil, headers = {}) ⇒ Object
56 57 58 |
# File 'lib/appnexusapi/connection.rb', line 56 def delete(route, body=nil, headers={}) run_request(:delete, route, body, headers) end |
#get(route, params = {}, headers = {}) ⇒ Object
43 44 45 46 |
# File 'lib/appnexusapi/connection.rb', line 43 def get(route, params={}, headers={}) params = params.delete_if {|key, value| value.nil? } run_request(:get, @connection.build_url(route, params), nil, headers) end |
#is_authorized? ⇒ Boolean
22 23 24 |
# File 'lib/appnexusapi/connection.rb', line 22 def !@token.nil? end |
#log ⇒ Object
26 27 28 |
# File 'lib/appnexusapi/connection.rb', line 26 def log @logger end |
#login ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/appnexusapi/connection.rb', line 30 def login response = @connection.run_request(:post, 'auth', { 'auth' => { 'username' => @config['username'], 'password' => @config['password'] } }, {}) log.debug(response.body) if response.body['response']['error_code'] fail "#{response.body['response']['error_code']}/#{response.body['response']['error_description']}" end @token = response.body['response']['token'] end |
#logout ⇒ Object
39 40 41 |
# File 'lib/appnexusapi/connection.rb', line 39 def logout @token = nil end |
#post(route, body = nil, headers = {}) ⇒ Object
48 49 50 |
# File 'lib/appnexusapi/connection.rb', line 48 def post(route, body=nil, headers={}) run_request(:post, route, body, headers) end |
#put(route, body = nil, headers = {}) ⇒ Object
52 53 54 |
# File 'lib/appnexusapi/connection.rb', line 52 def put(route, body=nil, headers={}) run_request(:put, route, body, headers) end |
#run_request(method, route, body, headers) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/appnexusapi/connection.rb', line 60 def run_request(method, route, body, headers) login if ! response = {} begin loop do response = @connection.run_request( method, route, body, { 'Authorization' => @token }.merge(headers) ) break unless response.status == RATE_EXCEEDED_HTTP_CODE wait_time = response.headers['retry-after'] || RATE_EXCEEDED_DEFAULT_TIMEOUT log.info("received rate exceeded. wait time: #{wait_time}s") sleep wait_time.to_i end rescue AppnexusApi::Unauthorized => e if @retry == true raise AppnexusApi::Unauthorized, e else @retry = true logout run_request(method, route, body, headers) end rescue Faraday::Error::TimeoutError => _e raise AppnexusApi::Timeout, 'Timeout' ensure @retry = false end log.debug(response.body) response end |