# frozen_string_literal: true module Qismo class Client include Qismo::Api # @return [String, NilClass] DEFAULT_APP_ID = ENV["QISCUS_APP_ID"] # @return [String, NilClass] DEFAULT_SECRET_KEY = ENV["QISCUS_SECRET_KEY"] # @return [String] DEFAULT_URL = ENV["QISCUS_OMNICHANNEL_URL"] || "https://qismo.qiscus.com" # @return [Hash] DEFAULT_OPTIONS = { app_id: DEFAULT_APP_ID, secret_key: DEFAULT_SECRET_KEY, url: DEFAULT_URL, logger: nil, instrumentation: nil, timeout: nil, proxy: nil, } attr_accessor(*DEFAULT_OPTIONS.keys) # Initialize client # # @param options [Hash] def initialize(options = {}) DEFAULT_OPTIONS.merge(options).each do |key, value| instance_variable_set("@#{key}", value) end end # Send http request with post method # # @param path [String] # @param body [Hash] # @return [Qismo::SingleObject] def post(path, body = {}) request(:post, path, json: body) end # Send HTTP request with post method to upload file # # @param path [String] # @param body [Hash] # @return [Qismo::SingleObject] def post_upload(path, body = {}) request(:post, form: body) end # Send HTTP request with get method # # @param path [String] # @param params [Hash] # @return [Qismo::SingleObject] def get(path, params = {}) request(:get, path, params: params) end # Send HTTP request with delete method # # @param path [String] # @param params [Hash] # @return [Qismo::SingleObject] def delete(path, params = {}) request(:delete, path, params: params) end # Send HTTP request # # @param method [Symbol, String] # @param path [String] # @param headers [Hash] # @param params [Hash] # @param json [Hash] # @return [Qismo::SingleObject] def request(method, path, headers: {}, params: {}, json: {}) res = connection.request(method, @url + path, { headers: headers, params: params, json: json, }) if res.status.success? return SingleObject.new(JSON.parse(res.to_s)) end if res.status.server_error? raise InternalServerError.new("Qiscus Omnichannel server error", status_code: res.code, response_body: res.to_s) end if res.status.client_error? body = SingleObject.new(JSON.parse(res.to_s)) error = body.errors error = error.message if error.is_a?(SingleObject) error_klass_map = { 400 => BadRequestError, 401 => UnauthorizedError, 402 => PaymentRequiredError, 403 => ForbiddenError, 404 => NotFoundError, 429 => TooManyRequestError, } error_klass = error_klass_map[res.code] || HTTPRequestError raise error_klass.new(error, status_code: res.code, response_body: res.to_s) end end # Http connection config # # @return [HTTP::Chainable] def connection http = HTTP unless @logger.nil? http = http.use(logging: @logger) end unless @instrumentation.nil? http = http.use(instrumentation: @instrumentation) end unless @timeout.nil? http = if @timeout.is_a?(Hash) http.timeout(**@timeout) else http.timeout(@timeout) end end unless @proxy.nil? http = http.via(*@proxy) end http.headers({ "Qiscus-App-Id": @app_id, "Qiscus-Secret-Key": @secret_key, "User-Agent": user_agent.to_json, }) end # Http user agent config # # @return [Hash] def user_agent { lib_version: Qismo::VERSION, lang: "ruby", lang_version: RUBY_VERSION, platform: RUBY_PLATFORM, engine: defined?(RUBY_ENGINE) ? RUBY_ENGINE : "", } end end end