Class: AppnexusApi::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/appnexusapi/connection.rb

Constant Summary collapse

RATE_EXCEEDED_DEFAULT_TIMEOUT =
15
RATE_EXCEEDED_ERROR =

Inexplicably, sandbox uses the correct code of 429, while production uses 405? so we just rely on the error message

"RATE_EXCEEDED".freeze

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Connection

Returns a new instance of Connection.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/appnexusapi/connection.rb', line 11

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



58
59
60
# File 'lib/appnexusapi/connection.rb', line 58

def delete(route, body=nil, headers={})
  run_request(:delete, route, body, headers)
end

#get(route, params = {}, headers = {}) ⇒ Object



45
46
47
48
# File 'lib/appnexusapi/connection.rb', line 45

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

Returns:

  • (Boolean)


24
25
26
# File 'lib/appnexusapi/connection.rb', line 24

def is_authorized?
  !@token.nil?
end

#logObject



28
29
30
# File 'lib/appnexusapi/connection.rb', line 28

def log
  @logger
end

#loginObject



32
33
34
35
36
37
38
39
# File 'lib/appnexusapi/connection.rb', line 32

def 
  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

#logoutObject



41
42
43
# File 'lib/appnexusapi/connection.rb', line 41

def logout
  @token = nil
end

#post(route, body = nil, headers = {}) ⇒ Object



50
51
52
# File 'lib/appnexusapi/connection.rb', line 50

def post(route, body=nil, headers={})
  run_request(:post, route, body, headers)
end

#put(route, body = nil, headers = {}) ⇒ Object



54
55
56
# File 'lib/appnexusapi/connection.rb', line 54

def put(route, body=nil, headers={})
  run_request(:put, route, body, headers)
end

#run_request(method, route, body, headers) ⇒ Object



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
92
93
# File 'lib/appnexusapi/connection.rb', line 62

def run_request(method, route, body, headers)
   if !is_authorized?
  response = {}
  begin
    loop do
      response = @connection.run_request(
        method,
        route,
        body,
        { 'Authorization' => @token }.merge(headers)
      )
      break unless response.body.fetch('response', {})['error_code'] == RATE_EXCEEDED_ERROR
      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