Class: Fastlane::Actions::ExecuteApiAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/execute_api/actions/execute_api_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



79
80
81
# File 'lib/fastlane/plugin/execute_api/actions/execute_api_action.rb', line 79

def self.authors
  ["Pasan Eramusugoda"]
end

.available_optionsObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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

.descriptionObject



75
76
77
# 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

.detailsObject



87
88
89
90
# 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) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
142
143
144
145
# 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_valueObject



83
84
85
# 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) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# 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) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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