lib/SematextCloud/api_client.rb in SematextCloud-0.3.1 vs lib/SematextCloud/api_client.rb in SematextCloud-0.4.0

- old
+ new

@@ -4,12 +4,11 @@ #API Explorer provides access and documentation for Sematext REST API. The REST API requires the API Key to be sent as part of `Authorization` header. E.g.: `Authorization : apiKey e5f18450-205a-48eb-8589-7d49edaea813`. OpenAPI spec version: v3 Generated by: https://github.com/swagger-api/swagger-codegen.git -Swagger Codegen version: 2.4.12 - +Swagger Codegen version: 3.0.30 =end require 'date' require 'json' require 'logger' @@ -42,11 +41,11 @@ @@default ||= ApiClient.new end # Call an API with given options. # - # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements: + # @return [Array<(Object, Integer, Hash)>] an array of 3 elements: # the data deserialized from response body (could be nil), response status code and response headers. def call_api(http_method, path, opts = {}) request = build_request(http_method, path, opts) response = request.run @@ -126,10 +125,38 @@ request = Typhoeus::Request.new(url, req_opts) download_file(request) if opts[:return_type] == 'File' request end + # Builds the HTTP request body + # + # @param [Hash] header_params Header parameters + # @param [Hash] form_params Query parameters + # @param [Object] body HTTP body (JSON/XML) + # @return [String] HTTP body data in the form of string + def build_request_body(header_params, form_params, body) + # http form + if header_params['Content-Type'] == 'application/x-www-form-urlencoded' || + header_params['Content-Type'] == 'multipart/form-data' + data = {} + form_params.each do |key, value| + case value + when ::File, ::Array, nil + # let typhoeus handle File, Array and nil parameters + data[key] = value + else + data[key] = value.to_s + end + end + elsif body + data = body.is_a?(String) ? body : body.to_json + else + data = nil + end + data + end + # Check if the given MIME is a JSON MIME. # JSON MIME examples: # application/json # application/json; charset=UTF8 # APPLICATION/JSON @@ -141,11 +168,11 @@ end # Deserialize the response to the given return type. # # @param [Response] response HTTP response - # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]" + # @param [String] return_type some examples: "User", "Array<User>", "Hash<String, Integer>" def deserialize(response, return_type) body = response.body # handle file downloading - return the File instance processed in request callbacks # note that response body is empty when the file is written in chunks in request on_body callback @@ -185,11 +212,11 @@ data.to_s when 'Integer' data.to_i when 'Float' data.to_f - when 'BOOLEAN' + when 'Boolean' data == true when 'DateTime' # parse date time (expecting ISO 8601 format) DateTime.parse data when 'Date' @@ -208,13 +235,11 @@ {}.tap do |hash| data.each { |k, v| hash[k] = convert_to_type(v, sub_type) } end else # models, e.g. Pet - SematextCloud.const_get(return_type).new.tap do |model| - model.build_from_hash data - end + SematextCloud.const_get(return_type).build_from_hash(data) end end # Save response body into a file in (the defined) temporary folder, using the filename # from the "Content-Disposition" header if provided, otherwise a random filename. @@ -242,15 +267,17 @@ request.on_body do |chunk| chunk.force_encoding(encoding) tempfile.write(chunk) end request.on_complete do |response| - tempfile.close - @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\ - "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\ - "will be deleted automatically with GC. It's also recommended to delete the temp file "\ - "explicitly with `tempfile.delete`" + if tempfile + tempfile.close + @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\ + "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\ + "will be deleted automatically with GC. It's also recommended to delete the temp file "\ + "explicitly with `tempfile.delete`" + end end end # Sanitize filename by removing path. # e.g. ../../sun.gif becomes sun.gif @@ -262,38 +289,10 @@ end def build_request_url(path) # Add leading and trailing slashes to path path = "/#{path}".gsub(/\/+/, '/') - URI.encode(@config.base_url + path) - end - - # Builds the HTTP request body - # - # @param [Hash] header_params Header parameters - # @param [Hash] form_params Query parameters - # @param [Object] body HTTP body (JSON/XML) - # @return [String] HTTP body data in the form of string - def build_request_body(header_params, form_params, body) - # http form - if header_params['Content-Type'] == 'application/x-www-form-urlencoded' || - header_params['Content-Type'] == 'multipart/form-data' - data = {} - form_params.each do |key, value| - case value - when ::File, ::Array, nil - # let typhoeus handle File, Array and nil parameters - data[key] = value - else - data[key] = value.to_s - end - end - elsif body - data = body.is_a?(String) ? body : body.to_json - else - data = nil - end - data + @config.base_url + path end # Update hearder and query params based on authentication settings. # # @param [Hash] header_params Header parameters