# 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/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["status"] == ERROR && response.dig("data", "uniqueName")) response = replace_existing_application_by_unique_name( 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}'" end response["status"] end # 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 replace_existing_application_by_unique_name(project_name, unique_name, full_path_to_file, content_type) application_info_response = faraday_get_application_info(unique_name) application_info_response.each do |app_response| next unless app_response["uniqueName"] == unique_name response = replace_existing_application(app_response["id"], project_name, unique_name, full_path_to_file, content_type) return response if response["status"] == SUCCESS end 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}'" 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:, 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}'" end response end end # rubocop:enable Metrics/ClassLength end