class Airbnb::Service::Hongbao::Api::ApiClient

Constants

CLIENT_CONFIG
HOST_KEY
IDL_VERSION
PAYLOAD_TYPE
PORT_KEY
THRAWN_KEY

Public Class Methods

check_unsuccessful_response_error(error) click to toggle source
# 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 = {}) click to toggle source
# File lib/airbnb/service/hongbao/api/hongbao.rb, line 49
def self.instance(params = {})
  @@instance ||= new(params)
end
new(params = {}) click to toggle source
# 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

Public Instance Methods

can_claim_in_app(request) click to toggle source

canClaimInApp

# 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 = {}) click to toggle source
# 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) click to toggle source

fetchActiveCampaign

# 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) click to toggle source

fetchCampaignData

# 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) click to toggle source

fetchCouponData

# 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) click to toggle source

fetchCouponState

# 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) click to toggle source

fetchReferralStatus

# 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) click to toggle source

getCampaignTextData

# 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) click to toggle source

getCouponMessages

# 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
get_user(request) click to toggle source

getUser

# 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) click to toggle source

getUserChinaCoupons

# 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) click to toggle source

hasChinaCoupon

# 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) click to toggle source

hello

# 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 = {}) click to toggle source
# 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