# frozen_string_literal: true require "faraday" require "faraday/net_http" require "faraday/multipart" module CiToolkit # A client for Seetest webservice. # See API doc for more info: https://docs.experitest.com/display/PM/How+To+Execute+Rest+API # rubocop:disable Metrics/ClassLength class SeeTestClient SUCCESS = "SUCCESS" OKAY = "OK" ERROR = "ERROR" API_VERSION = "v1" attr_reader :faraday_conn def initialize(bot = CiToolkit::SeeTestBot.new) @faraday_conn = bot.faraday_conn end # rubocop:disable Metrics/AbcSize # rubocop:disable Metrics/MethodLength def upload_file( project_name:, unique_name:, full_path_to_file:, content_type: "application/octet-stream" ) response = faraday_upload( project_name, unique_name, full_path_to_file, content_type ).body # Application already exists. Replace it. if response.status == SUCCESS && response.data.created == "false" response = replace_existing_application( response.data.id, project_name, unique_name, full_path_to_file, content_type ) # Application with unique name already exist. Replace it. elsif response.status == ERROR && !response.data.nil? && !response.data.uniqueName.nil? application_info_response = faraday_get_application_info(unique_name) application_id = application_info_response[0].id response = replace_existing_application( application_id, project_name, unique_name, full_path_to_file, content_type ) end unless response.status == SUCCESS raise StandardError, "Upload response from Seetest returned an error. Response body is: '#{response.marshal_dump}'" end response.status end # rubocop:enable Metrics/AbcSize Metrics/MethodLength # rubocop:enable Metrics/MethodLength private def replace_existing_application( application_id, project_name, unique_name, full_path_to_file, content_type ) response = faraday_delete_application(application_id) if response.status == SUCCESS && response.code == OKAY response = faraday_upload_file_with_error_handling(project_name, unique_name, full_path_to_file, content_type) end response end def faraday_upload_file_with_error_handling( project_name, unique_name, full_path_to_file, content_type ) response = faraday_upload(project_name, unique_name, full_path_to_file, content_type).body unless response.status == SUCCESS && response.data.created == "true" raise StandardError, "Upload response from Seetest returned an error. Response body is: '#{response.marshal_dump}'" end response end def faraday_upload( project_name, unique_name, full_path_to_file, content_type ) file = Faraday::Multipart::FilePart.new(full_path_to_file, content_type) params = { file: file, uniqueName: unique_name, project: project_name } @faraday_conn.post("/api/#{API_VERSION}/applications/new", params) end def faraday_get_application_info(unique_name) params = { uniqueName: unique_name } response = @faraday_conn.get("/api/#{API_VERSION}/applications", params).body if response.empty? raise StandardError, "Get application info for uniqueName: #{unique_name} from Seetest returned an error. "\ "Response body is: '#{response}'" end response end def faraday_delete_application(application_id) response = @faraday_conn.post("/api/#{API_VERSION}/applications/#{application_id}/delete").body unless response.status == SUCCESS && response.code == OKAY raise StandardError, "Delete application id: #{application_id} from Seetest returned an error. "\ "Response body is: '#{response.marshal_dump}'" end response end end # rubocop:enable Metrics/ClassLength end