Class: TaboolaApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/taboola_api/client.rb

Constant Summary collapse

HOST =
"https://backstage.taboola.com/backstage"
CURRENT_API_PATH =
"api/1.0"
DEFAULT_URL =
"#{HOST}/#{CURRENT_API_PATH}"

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, client_secret:, access_token:) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
# File 'lib/taboola_api/client.rb', line 13

def initialize(client_id:, client_secret:, access_token:)
  @client_id = client_id
  @client_secret = client_secret
  @access_token = access_token
end

Instance Method Details

#accountsObject



99
100
101
# File 'lib/taboola_api/client.rb', line 99

def accounts
  @accounts ||= TaboolaApi::Resources::Account.new(self)
end

#campaign_itemsObject



107
108
109
# File 'lib/taboola_api/client.rb', line 107

def campaign_items
  @campaign_items ||= TaboolaApi::Resources::CampaignItem.new(self)
end

#campaignsObject



103
104
105
# File 'lib/taboola_api/client.rb', line 103

def campaigns
  @campaigns ||= TaboolaApi::Resources::Campaign.new(self)
end

#connectionObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/taboola_api/client.rb', line 19

def connection
  @connection ||= Faraday.new(url: DEFAULT_URL) do |conn|
    conn.headers["Authorization"] = "Bearer #{@access_token}"
    conn.headers["Content-Type"] = "application/json"

    conn.options.timeout = 60
    conn.options.open_timeout = 30

    conn.request :multipart
    conn.use Faraday::FollowRedirects::Middleware
    conn.use Faraday::Retry::Middleware, max: 3

    conn.response :json

    conn.adapter Faraday.default_adapter
  end
end

#handle_response(response) ⇒ Object

Raises:

  • (klass)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/taboola_api/client.rb', line 60

def handle_response(response)
  return response if response.success?

  status = response.status
  body = response.body
  error_message = body.is_a?(Hash) ? body&.dig("message") : body

  klass = case status
  when 401
    TaboolaApi::AuthenticationError
  when 403
    TaboolaApi::AuthorizationError
  when 429
    TaboolaApi::RateLimitError
  when 400..499
    TaboolaApi::InvalidRequestError
  when 500..599
    TaboolaApi::ApiError
  else
    TaboolaApi::Error
  end

  raise klass.new(error_message || "HTTP #{status}", status, body)
end

#motion_adsObject



111
112
113
# File 'lib/taboola_api/client.rb', line 111

def motion_ads
  @motion_ads ||= TaboolaApi::Resources::MotionAds.new(self)
end

#operationsObject



115
116
117
# File 'lib/taboola_api/client.rb', line 115

def operations
  @operations ||= TaboolaApi::Resources::Operations.new(self)
end

#reportingsObject



119
120
121
# File 'lib/taboola_api/client.rb', line 119

def reportings
  @reportings ||= TaboolaApi::Resources::Reportings.new(self)
end

#request(method, path, params = {}, headers = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/taboola_api/client.rb', line 37

def request(method, path, params = {}, headers = {})
  url = File.join(DEFAULT_URL, path)

  response = connection.run_request(method, url, nil, headers) do |req|
    case method
    when :get, :delete
      req.params = params
    when :post, :put
      if headers["Content-Type"] == "multipart/form-data"
        req.options.timeout = 120
        req.body = {}
        params.each do |key, value|
          req.body[key.to_sym] = value
        end
      else
        req.body = JSON.generate(params) unless params.empty?
      end
    end
  end

  handle_response(response)
end

#update_access_tokenObject



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/taboola_api/client.rb', line 85

def update_access_token
  response = Faraday.post("#{HOST}/oauth/token") do |req|
    req.headers["Content-Type"] = "application/x-www-form-urlencoded"
    req.body = URI.encode_www_form(
      client_id: @client_id,
      client_secret: @client_secret,
      grant_type: "client_credentials"
    )
  end
  data = JSON.parse(response.body)

  @access_token = data["access_token"]
end