class Fastlane::Actions::ExecuteApiAction

Public Class Methods

authors() click to toggle source
# File lib/fastlane/plugin/execute_api/actions/execute_api_action.rb, line 79
def self.authors
  ["Pasan Eramusugoda"]
end
available_options() click to toggle source
# File lib/fastlane/plugin/execute_api/actions/execute_api_action.rb, line 92
def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :uploadArtifacts,
                            env_name: "",
                            description: "uploading any file or not",
                            optional: true,
                            default_value: false),
    FastlaneCore::ConfigItem.new(key: :apk,
                            env_name: "",
                            description: ".apk file for the build",
                            optional: true,
                            default_value: Actions.lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH]),
    FastlaneCore::ConfigItem.new(key: :ipa,
                            env_name: "",
                            description: ".ipa file for the build ",
                            optional: true,
                            default_value: Actions.lane_context[SharedValues::IPA_OUTPUT_PATH]),
    FastlaneCore::ConfigItem.new(key: :file,
                            env_name: "",
                            description: "file to be uploaded to the server",
                            optional: true),
    FastlaneCore::ConfigItem.new(key: :multipartPayload,
                            env_name: "",
                            description: "payload for the multipart request ",
                            optional: true,
                            type: Hash),
    FastlaneCore::ConfigItem.new(key: :headers,
                              env_name: "",
                              description: "headers of the request ",
                              optional: true,
                              type: Hash),
    FastlaneCore::ConfigItem.new(key: :endPoint,
                            env_name: "",
                            description: "file upload request url",
                            optional: false,
                            default_value: "",
                            type: String),
    FastlaneCore::ConfigItem.new(key: :method,
                            env_name: "",
                            description: "request method",
                            optional: true,
                            default_value: :post,
                            type: Symbol)

  ]
end
description() click to toggle source
# File lib/fastlane/plugin/execute_api/actions/execute_api_action.rb, line 75
def self.description
  "This plugin will be used to execute an api"
end
details() click to toggle source
# File lib/fastlane/plugin/execute_api/actions/execute_api_action.rb, line 87
def self.details
  # Optional:
  "Plugin can be used to execute an api or file upload, which will be usefull when need to notify your self hosted backend"
end
is_supported?(platform) click to toggle source
# File lib/fastlane/plugin/execute_api/actions/execute_api_action.rb, line 139
def self.is_supported?(platform)
  # Adjust this if your plugin only works for a particular platform (iOS vs. Android, for example)
  # See: https://docs.fastlane.tools/advanced/#control-configuration-by-lane-and-by-platform
  #
  # [:ios, :mac, :android].include?(platform)
  platform == :ios || platform == :android
end
return_value() click to toggle source
# File lib/fastlane/plugin/execute_api/actions/execute_api_action.rb, line 83
def self.return_value
  # If your method provides a return value, you can describe here what it does
end
run(config) click to toggle source
# File lib/fastlane/plugin/execute_api/actions/execute_api_action.rb, line 8
def self.run(config)
  params = {}
  # extract parms from config received from fastlane
  params[:endPoint] = config[:endPoint]
  params[:apk] = config[:apk]
  params[:ipa] = config[:ipa]
  params[:file] = config[:file]
  params[:method] = config[:method]

  params[:multipartPayload] = config[:multipartPayload]
  params[:headers] = config[:headers]

  apk_file = params[:apk]
  ipa_file = params[:ipa]
  custom_file = params[:file]

  end_point = params[:endPoint]

  upload_artifacts = ipa_file.to_s.length > 0 || apk_file.to_s.length > 0 || custom_file.to_s.length > 0

  params[:uploadArtifacts] = upload_artifacts.to_s

  UI.user_error!("No endPoint given, pass using endPoint: 'endpoint'") if end_point.to_s.length == 0
  UI.user_error!("No IPA or APK or a file path given, pass using `ipa: 'ipa path'` or `apk: 'apk path' or file:`") if upload_artifacts && ipa_file.to_s.length == 0 && apk_file.to_s.length == 0 && custom_file.to_s.length == 0
  UI.user_error!("Please only give IPA path or APK path (not both)") if upload_artifacts && ipa_file.to_s.length > 0 && apk_file.to_s.length > 0

  if upload_artifacts
    upload_custom_file(params, apk_file) if apk_file.to_s.length > 0
    upload_custom_file(params, ipa_file) if ipa_file.to_s.length > 0
    upload_custom_file(params, custom_file) if custom_file.to_s.length > 0
  else
    multipart_payload = params[:multipartPayload]
    multipart_payload[:multipart] = false
    upload_request(params, multipart_payload)
  end
end
upload_custom_file(params, custom_file) click to toggle source
# File lib/fastlane/plugin/execute_api/actions/execute_api_action.rb, line 45
def self.upload_custom_file(params, custom_file)
  multipart_payload = params[:multipartPayload]
  multipart_payload[:multipart] = true
  if multipart_payload[:fileFormFieldName]
    key = multipart_payload[:fileFormFieldName]
    multipart_payload[key.to_s] = File.new(custom_file, 'rb')
  else
    multipart_payload[:file] = File.new(custom_file, 'rb')
  end

  UI.message(multipart_payload)
  upload_request(params, multipart_payload)
end
upload_request(params, multipart_payload) click to toggle source
# File lib/fastlane/plugin/execute_api/actions/execute_api_action.rb, line 59
def self.upload_request(params, multipart_payload)
  request = RestClient::Request.new(
    method: params[:method],
    url: params[:endPoint],
    payload: multipart_payload,
    headers: params[:headers],
    log: Logger.new($stdout)
  )

  response = request.execute
  UI.message(response)
  if response.code == 200 || response.code == 201
    params[:uploadArtifacts]? UI.success("Successfully finished uploading the fille") : UI.success("Successfully finished executing the request")
  end
end