Class: Airbnb::Service::Hongbao::Api::ApiClient

Inherits:
Api::ClientBase
  • Object
show all
Defined in:
lib/airbnb/service/hongbao/api/hongbao.rb

Constant Summary collapse

IDL_VERSION =
'no version'
HOST_KEY =
:host
PORT_KEY =
:port
THRAWN_KEY =
:thrawn
PAYLOAD_TYPE =
:thrift
CLIENT_CONFIG =
{
  name: 'Hongbao',
  caller: 'rails',
  thrawn: {
    host: 'hongbao.synapse',
    port: 6756,
    request_format: :thrift,
    response_format: :thrift,
    raise_errors: true,
    timeout: 10,
    connect_timeout: 10,
    retries: 1,
    down_failures: 3,
    down_retry_delay: 100,
    max_requests: 500,
  },
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ ApiClient

Returns a new instance of ApiClient.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/airbnb/service/hongbao/api/hongbao.rb', line 53

def initialize(params = {})
  @client_config = client_config(params)
  cert_configs = {}
  if params[:cert_common_name]
    cert_configs[:cert_common_name] = params[:cert_common_name]
  end
  @thrawn_client = ::Airbnb::Service::Api::ClientBase.setup_thrawn(@client_config.merge(cert_configs))
  @options = (params || {})
  @logger = begin
    if defined?(Rails.logger)
      @logger = Rails.logger
    else
      @logger = Logger.new(STDERR)
      @logger.progname = 'Hongbao::ApiClient'
    end
    @logger
  end
end

Class Method Details

.check_unsuccessful_response_error(error) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/airbnb/service/hongbao/api/hongbao.rb', line 321

def self.check_unsuccessful_response_error(error)
  status = error.response.status
  # Thrawn thrift format does not parse body
  body = JSON.parse(error.response.parsed_body)
  if status == 851
    case body["exception_cls"]
      when "HasChinaCouponException"
        raise ::Airbnb::Service::Hongbao::Api::HasChinaCouponException.build(body["exception_msg"])
      when "HelloException"
        raise ::Airbnb::Service::Hongbao::Api::HelloException.build(body["exception_msg"])
      when "HongbaoActiveCampaignException"
        raise ::Airbnb::Service::Hongbao::Api::HongbaoActiveCampaignException.build(body["exception_msg"])
      when "HongbaoCampaignDataException"
        raise ::Airbnb::Service::Hongbao::Api::HongbaoCampaignDataException.build(body["exception_msg"])
      when "HongbaoCampaignTextDataException"
        raise ::Airbnb::Service::Hongbao::Api::HongbaoCampaignTextDataException.build(body["exception_msg"])
      when "HongbaoCanClaimInAppException"
        raise ::Airbnb::Service::Hongbao::Api::HongbaoCanClaimInAppException.build(body["exception_msg"])
      when "HongbaoCouponDataException"
        raise ::Airbnb::Service::Hongbao::Api::HongbaoCouponDataException.build(body["exception_msg"])
      when "HongbaoCouponStateException"
        raise ::Airbnb::Service::Hongbao::Api::HongbaoCouponStateException.build(body["exception_msg"])
      when "HongbaoGetCouponMessagesException"
        raise ::Airbnb::Service::Hongbao::Api::HongbaoGetCouponMessagesException.build(body["exception_msg"])
      when "HongbaoGetCtaLinkException"
        raise ::Airbnb::Service::Hongbao::Api::HongbaoGetCtaLinkException.build(body["exception_msg"])
      when "HongbaoGetUserChinaCouponsException"
        raise ::Airbnb::Service::Hongbao::Api::HongbaoGetUserChinaCouponsException.build(body["exception_msg"])
      when "HongbaoReferralStatusException"
        raise ::Airbnb::Service::Hongbao::Api::HongbaoReferralStatusException.build(body["exception_msg"])
      when "UserException"
        raise ::Airbnb::Service::Hongbao::Api::UserException.build(body["exception_msg"])
    end
  else # Hadling framework standard erros, which is not defined by IDL
    rethrow_standard_error(error, body)
  end
  raise error
end

.instance(params = {}) ⇒ Object



49
50
51
# File 'lib/airbnb/service/hongbao/api/hongbao.rb', line 49

def self.instance(params = {})
  @@instance ||= new(params)
end

Instance Method Details

#can_claim_in_app(request) ⇒ Object

canClaimInApp



214
215
216
217
218
219
220
221
222
223
224
# File 'lib/airbnb/service/hongbao/api/hongbao.rb', line 214

def can_claim_in_app(request)
  serialized_req = request.serialize(Sparsam::CompactProtocol)
  response = send_thrawn_request("/Hongbao/canClaimInApp", "canClaimInApp", serialized_req)
  if response
    Sparsam::Deserializer.deserialize(::Airbnb::Service::Hongbao::Api::HongbaoCanClaimInAppResponse, response, Sparsam::CompactProtocol)
  else
    nil
  end
rescue Thrawn::UnsuccessfulResponseError => e
  self.class.check_unsuccessful_response_error(e)
end

#client_config(params = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/airbnb/service/hongbao/api/hongbao.rb', line 72

def client_config(params = {})
  if params.key?(HOST_KEY) or params.key?(PORT_KEY)
    client_config = Marshal.load(Marshal.dump(CLIENT_CONFIG))
    client_config[THRAWN_KEY][HOST_KEY] = params[HOST_KEY] if params[HOST_KEY]
    client_config[THRAWN_KEY][PORT_KEY] = params[PORT_KEY] if params[PORT_KEY]
    client_config
  else
    CLIENT_CONFIG
  end
end

#fetch_active_campaign(request) ⇒ Object

fetchActiveCampaign



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/airbnb/service/hongbao/api/hongbao.rb', line 149

def fetch_active_campaign(request)
  serialized_req = request.serialize(Sparsam::CompactProtocol)
  response = send_thrawn_request("/Hongbao/fetchActiveCampaign", "fetchActiveCampaign", serialized_req)
  if response
    Sparsam::Deserializer.deserialize(::Airbnb::Service::Hongbao::Api::HongbaoActiveCampaignResponse, response, Sparsam::CompactProtocol)
  else
    nil
  end
rescue Thrawn::UnsuccessfulResponseError => e
  self.class.check_unsuccessful_response_error(e)
end

#fetch_campaign_data(request) ⇒ Object

fetchCampaignData



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/airbnb/service/hongbao/api/hongbao.rb', line 162

def fetch_campaign_data(request)
  serialized_req = request.serialize(Sparsam::CompactProtocol)
  response = send_thrawn_request("/Hongbao/fetchCampaignData", "fetchCampaignData", serialized_req)
  if response
    Sparsam::Deserializer.deserialize(::Airbnb::Service::Hongbao::Api::HongbaoCampaignDataResponse, response, Sparsam::CompactProtocol)
  else
    nil
  end
rescue Thrawn::UnsuccessfulResponseError => e
  self.class.check_unsuccessful_response_error(e)
end

#fetch_coupon_data(request) ⇒ Object

fetchCouponData



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/airbnb/service/hongbao/api/hongbao.rb', line 136

def fetch_coupon_data(request)
  serialized_req = request.serialize(Sparsam::CompactProtocol)
  response = send_thrawn_request("/Hongbao/fetchCouponData", "fetchCouponData", serialized_req)
  if response
    Sparsam::Deserializer.deserialize(::Airbnb::Service::Hongbao::Api::HongbaoCouponDataResponse, response, Sparsam::CompactProtocol)
  else
    nil
  end
rescue Thrawn::UnsuccessfulResponseError => e
  self.class.check_unsuccessful_response_error(e)
end

#fetch_coupon_state(request) ⇒ Object

fetchCouponState



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/airbnb/service/hongbao/api/hongbao.rb', line 123

def fetch_coupon_state(request)
  serialized_req = request.serialize(Sparsam::CompactProtocol)
  response = send_thrawn_request("/Hongbao/fetchCouponState", "fetchCouponState", serialized_req)
  if response
    Sparsam::Deserializer.deserialize(::Airbnb::Service::Hongbao::Api::HongbaoCouponStateResponse, response, Sparsam::CompactProtocol)
  else
    nil
  end
rescue Thrawn::UnsuccessfulResponseError => e
  self.class.check_unsuccessful_response_error(e)
end

#fetch_referral_status(request) ⇒ Object

fetchReferralStatus



227
228
229
230
231
232
233
234
235
236
237
# File 'lib/airbnb/service/hongbao/api/hongbao.rb', line 227

def fetch_referral_status(request)
  serialized_req = request.serialize(Sparsam::CompactProtocol)
  response = send_thrawn_request("/Hongbao/fetchReferralStatus", "fetchReferralStatus", serialized_req)
  if response
    Sparsam::Deserializer.deserialize(::Airbnb::Service::Hongbao::Api::HongbaoReferralStatusResponse, response, Sparsam::CompactProtocol)
  else
    nil
  end
rescue Thrawn::UnsuccessfulResponseError => e
  self.class.check_unsuccessful_response_error(e)
end

#get_campaign_text_data(request) ⇒ Object

getCampaignTextData



240
241
242
243
244
245
246
247
248
249
250
# File 'lib/airbnb/service/hongbao/api/hongbao.rb', line 240

def get_campaign_text_data(request)
  serialized_req = request.serialize(Sparsam::CompactProtocol)
  response = send_thrawn_request("/Hongbao/getCampaignTextData", "getCampaignTextData", serialized_req)
  if response
    Sparsam::Deserializer.deserialize(::Airbnb::Service::Hongbao::Api::HongbaoCampaignTextDataResponse, response, Sparsam::CompactProtocol)
  else
    nil
  end
rescue Thrawn::UnsuccessfulResponseError => e
  self.class.check_unsuccessful_response_error(e)
end

#get_coupon_messages(request) ⇒ Object

getCouponMessages



201
202
203
204
205
206
207
208
209
210
211
# File 'lib/airbnb/service/hongbao/api/hongbao.rb', line 201

def get_coupon_messages(request)
  serialized_req = request.serialize(Sparsam::CompactProtocol)
  response = send_thrawn_request("/Hongbao/getCouponMessages", "getCouponMessages", serialized_req)
  if response
    Sparsam::Deserializer.deserialize(::Airbnb::Service::Hongbao::Api::HongbaoGetCouponMessagesResponse, response, Sparsam::CompactProtocol)
  else
    nil
  end
rescue Thrawn::UnsuccessfulResponseError => e
  self.class.check_unsuccessful_response_error(e)
end

getCtaLink



188
189
190
191
192
193
194
195
196
197
198
# File 'lib/airbnb/service/hongbao/api/hongbao.rb', line 188

def get_cta_link(request)
  serialized_req = request.serialize(Sparsam::CompactProtocol)
  response = send_thrawn_request("/Hongbao/getCtaLink", "getCtaLink", serialized_req)
  if response
    Sparsam::Deserializer.deserialize(::Airbnb::Service::Hongbao::Api::HongbaoGetCtaLinkResponse, response, Sparsam::CompactProtocol)
  else
    nil
  end
rescue Thrawn::UnsuccessfulResponseError => e
  self.class.check_unsuccessful_response_error(e)
end

#get_user(request) ⇒ Object

getUser



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/airbnb/service/hongbao/api/hongbao.rb', line 97

def get_user(request)
  serialized_req = request.serialize(Sparsam::CompactProtocol)
  response = send_thrawn_request("/Hongbao/getUser", "getUser", serialized_req)
  if response
    Sparsam::Deserializer.deserialize(::Airbnb::Service::Hongbao::Api::UserResponse, response, Sparsam::CompactProtocol)
  else
    nil
  end
rescue Thrawn::UnsuccessfulResponseError => e
  self.class.check_unsuccessful_response_error(e)
end

#get_user_china_coupons(request) ⇒ Object

getUserChinaCoupons



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/airbnb/service/hongbao/api/hongbao.rb', line 175

def get_user_china_coupons(request)
  serialized_req = request.serialize(Sparsam::CompactProtocol)
  response = send_thrawn_request("/Hongbao/getUserChinaCoupons", "getUserChinaCoupons", serialized_req)
  if response
    Sparsam::Deserializer.deserialize(::Airbnb::Service::Hongbao::Api::HongbaoGetUserChinaCouponsResponse, response, Sparsam::CompactProtocol)
  else
    nil
  end
rescue Thrawn::UnsuccessfulResponseError => e
  self.class.check_unsuccessful_response_error(e)
end

#has_china_coupon(request) ⇒ Object

hasChinaCoupon



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/airbnb/service/hongbao/api/hongbao.rb', line 110

def has_china_coupon(request)
  serialized_req = request.serialize(Sparsam::CompactProtocol)
  response = send_thrawn_request("/Hongbao/hasChinaCoupon", "hasChinaCoupon", serialized_req)
  if response
    Sparsam::Deserializer.deserialize(::Airbnb::Service::Hongbao::Api::HasChinaCouponResponse, response, Sparsam::CompactProtocol)
  else
    nil
  end
rescue Thrawn::UnsuccessfulResponseError => e
  self.class.check_unsuccessful_response_error(e)
end

#hello(request) ⇒ Object

hello



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/airbnb/service/hongbao/api/hongbao.rb', line 84

def hello(request)
  serialized_req = request.serialize(Sparsam::CompactProtocol)
  response = send_thrawn_request("/Hongbao/hello", "hello", serialized_req)
  if response
    Sparsam::Deserializer.deserialize(::Airbnb::Service::Hongbao::Api::HelloResponse, response, Sparsam::CompactProtocol)
  else
    nil
  end
rescue Thrawn::UnsuccessfulResponseError => e
  self.class.check_unsuccessful_response_error(e)
end

#send_thrawn_request(resource_url, resource_endpoint, request = nil, options = {}) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/airbnb/service/hongbao/api/hongbao.rb', line 252

def send_thrawn_request(resource_url, resource_endpoint, request = nil, options = {})
  if request
    options = options.merge({
      :body => request,
    })
  end

  DOGSTATSD.count('services_platform.client.request.count',
    1,
    :tags => [
      "caller:#{@client_config[:caller]}",
      'service:Hongbao',
      'client_for:Hongbao',
      "resource_endpoint:#{resource_url}",
      "method:#{resource_endpoint}",
      "content_type:#{PAYLOAD_TYPE}",
      'language:ruby',
    ],
  )
  start_time = Time.now
  response = ::Airbnb::Service::Api::ClientBase.send_request(@thrawn_client, :post, resource_url, @client_config, options)
  status = 'success'
  response
rescue Thrawn::UnsuccessfulResponseError => e
  status = 'error'
  parsed_body = e.response.parsed_body
  exception_class = e.response.parsed_body['exception_cls'] if (parsed_body && parsed_body.is_a?(Hash))
  exception_type = 'service'
  @logger.error "hongbao Ruby client Timeout:#{e} Path:#{resource_url}"
  raise e
rescue Exception => e
  status = 'error'
  exception_class = "#{e.class}"
  exception_type = 'unchecked'
  @logger.error "hongbao Ruby client error:#{e} Path:#{resource_url}"
  raise e
ensure
  elapsed_time_milliseconds = (Time.now - start_time) * 1000.0
  DOGSTATSD.histogram('services_platform.client.response',
    elapsed_time_milliseconds,
    :tags => [
      "caller:#{@client_config[:caller]}",
      'service:Hongbao',
      'client_for:Hongbao',
      "resource_endpoint:#{resource_url}",
      "method:#{resource_endpoint}",
      "content_type:#{PAYLOAD_TYPE}",
      'language:ruby',
      "status:#{status}",
    ],
  )
  if status == 'error'
    DOGSTATSD.count('services_platform.client.exception.count',
      1,
      :tags => [
        "caller:#{@client_config[:caller]}",
        'service:Hongbao',
        'client_for:Hongbao',
        "resource_endpoint:#{resource_url}",
        "method:#{resource_endpoint}",
        "content_type:#{PAYLOAD_TYPE}",
        'language:ruby',
        "exception_class:#{exception_class}",
        "exception_type:#{exception_type}",
      ],
    )
  end
end