lib/qismo/client.rb in qismo-0.1.8 vs lib/qismo/client.rb in qismo-0.5.0

- old
+ new

@@ -1,131 +1,103 @@ # frozen_string_literal: true -require "http" -require "qismo/operations" - module Qismo - # - # Qismo client object - # - # @!attribute app_id [String] - # @!attribute secret_key [String] - # @!attribute base_url [String] - # @!attribute admin_email [String] class Client - # @!parse - # include Qismo::Helpers::BaseHelper - include Qismo::Helpers::BaseHelper + include Qismo::Model + include Qismo::Api - # @!parse - # include Qismo::Operations - include Qismo::Operations + VALID_OPTIONS = [:app_id, :secret_key, :url, :logger, :instrumentation, :timeout, :proxy] - # @return [String] default base url constant - DEFAULT_BASE_URL = "https://multichannel.qiscus.com" + attr_accessor(*VALID_OPTIONS) - attr_accessor :app_id, :secret_key, :base_url, :admin_email, :proxy + def initialize(**opt) + @app_id = opt[:app_id] || ENV["QISCUS_APP_ID"] + @secret_key = opt[:secret_key] || ENV["QISCUS_SECRET_KEY"] + @url = opt[:url] || ENV["QISCUS_OMNICHANNEL_URL"] || "https://qismo.qiscus.com" + @logger = opt[:logger] + @instrumentation = opt[:instrumentation] + @timeout = opt[:timeout] + @proxy = opt[:proxy] + end - # - # Initialize Qismo client - # - # @param options [Hash] - # @option options [String] :app_id - # @option options [String] :secret_key - # @option options [String] :base_url - # - def initialize(options = {}) - options = options.compact - options[:base_url] = DEFAULT_BASE_URL if options[:base_url].nil? + def post(path, body = {}) + request(:post, path, json: body) + end - options.each do |key, value| - instance_variable_set("@#{key}", value) - end + def post_upload(path, body = {}) + request(:post, form: body) + end - if @admin_email.nil? && !@app_id.nil? - @admin_email = "#{app_id}_admin@qismo.com" - end + def get(path, **params) + request(:get, path, params) end - # - # Call HTTP request raw - # - # @param method [String] - # @param url [String] - # @param options [Hash] - # @option options [Hash] :headers - # @option options [Hash] :params - # @option options [Hash] :json - # - # @return [Response] - # - def raw_request(method, url, options = {}) - resp = if Qismo.client.proxy.nil? - HTTP.request(method, url, options) - else - proxy = Qismo.client.proxy - proxy_args = [proxy[:host], proxy[:port], proxy[:username], proxy[:password]] + def request(method, path, **opt) + res = connection.request(method, @url + path, opt.compact) + if res.status.success? + return JSON.parse(res.to_s, object_class: Data) + end - HTTP.via(*proxy_args).request(method, url, options) + if res.status.server_error? + raise InternalServerError.new("Qiscus Omnichannel server error", status_code: res.code, response_body: res.to_s) end - resp = Response.new(resp) - resp.raise_for_error + if res.status.client_error? + body = JSON.parse(res.to_s, object_class: Data) + error = body.errors + error = error.message if error.is_a?(Hash) - resp + 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 - # - # Do HTTP request - # - # @param method [String] - # @param path [String] - # @param options [Hash] - # @option options [Hash] :params - # @option options [Hash] :json - # @options options [Hash] :headers - # - # @return [Response] - # - def request(method, path, options = {}) - url = Qismo.client.base_url + path + def connection + http = HTTP - if !options[:headers].nil? && options[:headers].is_a?(Hash) && !options[:headers][:authorization].nil? - options[:header].merge({ - qiscus_app_id: Qismo.client.app_id, - qiscus_secret_key: Qismo.client.secret_key, - }) - else - options[:headers] = { - qiscus_app_id: Qismo.client.app_id, - qiscus_secret_key: Qismo.client.secret_key, - } + unless @logger.nil? + http = http.use(logging: @logger) end - raw_request(method, url, options) - end + unless @instrumentation.nil? + http = http.use(instrumentation: @instrumentation) + end - # - # Do HTTP request using GET method - # - # @param path [String] - # @param params [Hash] - # - # @return [Response] - # - def get(path, params = {}) - request(:get, path, params: params) + 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 - # - # Do HTTP request using POST method - # - # @param path [String] - # @param body [Hash] - # - # @return [Response] - # - def post(path, body = {}) - request(:post, path, json: body) + 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