vsphere-automation-runtime 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +77 -0
- data/README.md +44 -0
- data/Rakefile +18 -0
- data/git_push.sh +55 -0
- data/lib/vsphere-automation-runtime.rb +35 -0
- data/lib/vsphere-automation-runtime/api_client.rb +390 -0
- data/lib/vsphere-automation-runtime/api_error.rb +34 -0
- data/lib/vsphere-automation-runtime/configuration.rb +212 -0
- data/lib/vsphere-automation-runtime/version.rb +13 -0
- data/pkg/vsphere-automation-runtime-0.2.0.gem +0 -0
- data/spec/api_client_spec.rb +222 -0
- data/spec/configuration_spec.rb +38 -0
- data/spec/spec_helper.rb +107 -0
- data/vsphere-automation-runtime.gemspec +40 -0
- metadata +173 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b05cd67136136e867b59fef1c0228a644f7707b85d7fa873ef6c1624585c7154
|
4
|
+
data.tar.gz: 7f7d2d1010fa3371537582cdf7dfb4b3c1d80cce86dcf00566a5e28294074640
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d49859e7910b63d87e7cc6eb0bb9ef196043cb548cfbe1879a5ff8a2cab5ddf89fb986c9d9b166d97c90fbc0bc84eb877274b3a00d497b3176e8b360d44171e3
|
7
|
+
data.tar.gz: 0c3617d1f136d611b2949377a8f80b5129e8e0a7f27e760e083f99309088f22b885c007e447d3e6cf729a8686a08738d162dbd21e0253ed278a18ebad15e7f47
|
data/Gemfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# Copyright (c) 2018-2019 VMware, Inc. All Rights Reserved.
|
2
|
+
# SPDX-License-Identifier: MIT
|
3
|
+
|
4
|
+
# DO NOT MODIFY. THIS CODE IS GENERATED. CHANGES WILL BE OVERWRITTEN.
|
5
|
+
|
6
|
+
# runtime - No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
7
|
+
|
8
|
+
|
9
|
+
source 'https://rubygems.org'
|
10
|
+
|
11
|
+
|
12
|
+
gemspec
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
vsphere-automation-runtime (0.2.0)
|
5
|
+
typhoeus (~> 1.3)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
addressable (2.6.0)
|
11
|
+
public_suffix (>= 2.0.2, < 4.0)
|
12
|
+
ast (2.4.0)
|
13
|
+
coderay (1.1.2)
|
14
|
+
crack (0.4.3)
|
15
|
+
safe_yaml (~> 1.0.0)
|
16
|
+
diff-lcs (1.3)
|
17
|
+
ethon (0.12.0)
|
18
|
+
ffi (>= 1.3.0)
|
19
|
+
ffi (1.11.1)
|
20
|
+
hashdiff (1.0.0)
|
21
|
+
jaro_winkler (1.5.3)
|
22
|
+
method_source (0.9.2)
|
23
|
+
parallel (1.17.0)
|
24
|
+
parser (2.6.3.0)
|
25
|
+
ast (~> 2.4.0)
|
26
|
+
pry (0.12.2)
|
27
|
+
coderay (~> 1.1.0)
|
28
|
+
method_source (~> 0.9.0)
|
29
|
+
public_suffix (3.1.1)
|
30
|
+
rainbow (3.0.0)
|
31
|
+
rake (12.3.3)
|
32
|
+
rspec (3.8.0)
|
33
|
+
rspec-core (~> 3.8.0)
|
34
|
+
rspec-expectations (~> 3.8.0)
|
35
|
+
rspec-mocks (~> 3.8.0)
|
36
|
+
rspec-core (3.8.2)
|
37
|
+
rspec-support (~> 3.8.0)
|
38
|
+
rspec-expectations (3.8.4)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.8.0)
|
41
|
+
rspec-mocks (3.8.1)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.8.0)
|
44
|
+
rspec-support (3.8.2)
|
45
|
+
rubocop (0.73.0)
|
46
|
+
jaro_winkler (~> 1.5.1)
|
47
|
+
parallel (~> 1.10)
|
48
|
+
parser (>= 2.6)
|
49
|
+
rainbow (>= 2.2.2, < 4.0)
|
50
|
+
ruby-progressbar (~> 1.7)
|
51
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
52
|
+
ruby-progressbar (1.10.1)
|
53
|
+
safe_yaml (1.0.5)
|
54
|
+
typhoeus (1.3.1)
|
55
|
+
ethon (>= 0.9.0)
|
56
|
+
unicode-display_width (1.6.0)
|
57
|
+
vcr (5.0.0)
|
58
|
+
webmock (3.6.2)
|
59
|
+
addressable (>= 2.3.6)
|
60
|
+
crack (>= 0.3.2)
|
61
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
62
|
+
|
63
|
+
PLATFORMS
|
64
|
+
ruby
|
65
|
+
|
66
|
+
DEPENDENCIES
|
67
|
+
bundler (~> 2.0)
|
68
|
+
pry (~> 0.12.2)
|
69
|
+
rake (~> 12.3)
|
70
|
+
rspec (~> 3.7)
|
71
|
+
rubocop (~> 0.73.0)
|
72
|
+
vcr (~> 5.0)
|
73
|
+
vsphere-automation-runtime!
|
74
|
+
webmock (~> 3.6)
|
75
|
+
|
76
|
+
BUNDLED WITH
|
77
|
+
2.0.2
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# vsphere-automation-runtime
|
2
|
+
|
3
|
+
The Ruby gem for the vSphere Runtime API
|
4
|
+
|
5
|
+
- API version: 2.0.0
|
6
|
+
- Package version: 0.2.0
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'vsphere-automation-runtime'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install vsphere-automation-runtime
|
23
|
+
|
24
|
+
## Getting Started
|
25
|
+
|
26
|
+
Please follow the [installation](#installation) procedure and then run the following code:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
# Load the gem
|
30
|
+
require 'vsphere-automation-runtime'
|
31
|
+
|
32
|
+
```
|
33
|
+
|
34
|
+
## Documentation for API Endpoints
|
35
|
+
|
36
|
+
All URIs are relative to *https://<vcenter>/rest*
|
37
|
+
|
38
|
+
Class | Method | HTTP request | Description
|
39
|
+
------------ | ------------- | ------------- | -------------
|
40
|
+
|
41
|
+
|
42
|
+
## Documentation for Models
|
43
|
+
|
44
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# Copyright (c) 2018-2019 VMware, Inc. All Rights Reserved.
|
2
|
+
# SPDX-License-Identifier: MIT
|
3
|
+
|
4
|
+
# DO NOT MODIFY. THIS CODE IS GENERATED. CHANGES WILL BE OVERWRITTEN.
|
5
|
+
|
6
|
+
# runtime - No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
7
|
+
|
8
|
+
|
9
|
+
require "bundler/gem_tasks"
|
10
|
+
|
11
|
+
begin
|
12
|
+
require 'rspec/core/rake_task'
|
13
|
+
|
14
|
+
RSpec::Core::RakeTask.new(:spec)
|
15
|
+
task default: :spec
|
16
|
+
rescue LoadError
|
17
|
+
# no rspec available
|
18
|
+
end
|
data/git_push.sh
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
#!/bin/sh
|
2
|
+
#
|
3
|
+
# Generated by: https://openapi-generator.tech
|
4
|
+
#
|
5
|
+
# ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/
|
6
|
+
#
|
7
|
+
# Usage example: /bin/sh ./git_push.sh wing328 openapi-pestore-perl "minor update"
|
8
|
+
|
9
|
+
git_user_id=$1
|
10
|
+
git_repo_id=$2
|
11
|
+
release_note=$3
|
12
|
+
|
13
|
+
if [ "$git_user_id" = "" ]; then
|
14
|
+
git_user_id="GIT_USER_ID"
|
15
|
+
echo "[INFO] No command line input provided. Set \$git_user_id to $git_user_id"
|
16
|
+
fi
|
17
|
+
|
18
|
+
if [ "$git_repo_id" = "" ]; then
|
19
|
+
git_repo_id="GIT_REPO_ID"
|
20
|
+
echo "[INFO] No command line input provided. Set \$git_repo_id to $git_repo_id"
|
21
|
+
fi
|
22
|
+
|
23
|
+
if [ "$release_note" = "" ]; then
|
24
|
+
release_note="Minor update"
|
25
|
+
echo "[INFO] No command line input provided. Set \$release_note to $release_note"
|
26
|
+
fi
|
27
|
+
|
28
|
+
# Initialize the local directory as a Git repository
|
29
|
+
git init
|
30
|
+
|
31
|
+
# Adds the files in the local repository and stages them for commit.
|
32
|
+
git add .
|
33
|
+
|
34
|
+
# Commits the tracked changes and prepares them to be pushed to a remote repository.
|
35
|
+
git commit -m "$release_note"
|
36
|
+
|
37
|
+
# Sets the new remote
|
38
|
+
git_remote=`git remote`
|
39
|
+
if [ "$git_remote" = "" ]; then # git remote not defined
|
40
|
+
|
41
|
+
if [ "$GIT_TOKEN" = "" ]; then
|
42
|
+
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment."
|
43
|
+
git remote add origin https://github.com/${git_user_id}/${git_repo_id}.git
|
44
|
+
else
|
45
|
+
git remote add origin https://${git_user_id}:${GIT_TOKEN}@github.com/${git_user_id}/${git_repo_id}.git
|
46
|
+
fi
|
47
|
+
|
48
|
+
fi
|
49
|
+
|
50
|
+
git pull origin master
|
51
|
+
|
52
|
+
# Pushes (Forces) the changes in the local repository up to the remote repository
|
53
|
+
echo "Git pushing to https://github.com/${git_user_id}/${git_repo_id}.git"
|
54
|
+
git push origin master 2>&1 | grep -v 'To https'
|
55
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# Copyright (c) 2018-2019 VMware, Inc. All Rights Reserved.
|
2
|
+
# SPDX-License-Identifier: MIT
|
3
|
+
|
4
|
+
# DO NOT MODIFY. THIS CODE IS GENERATED. CHANGES WILL BE OVERWRITTEN.
|
5
|
+
|
6
|
+
# runtime - No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
7
|
+
|
8
|
+
|
9
|
+
# Common files
|
10
|
+
require 'vsphere-automation-runtime/api_client'
|
11
|
+
require 'vsphere-automation-runtime/api_error'
|
12
|
+
require 'vsphere-automation-runtime/configuration'
|
13
|
+
require 'vsphere-automation-runtime/version'
|
14
|
+
|
15
|
+
# Models
|
16
|
+
|
17
|
+
# APIs
|
18
|
+
|
19
|
+
module VSphereAutomation
|
20
|
+
class << self
|
21
|
+
# Customize default settings for the SDK using block.
|
22
|
+
# VSphereAutomation.configure do |config|
|
23
|
+
# config.username = "xxx"
|
24
|
+
# config.password = "xxx"
|
25
|
+
# end
|
26
|
+
# If no block given, return the default Configuration object.
|
27
|
+
def configure
|
28
|
+
if block_given?
|
29
|
+
yield(Configuration.default)
|
30
|
+
else
|
31
|
+
Configuration.default
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,390 @@
|
|
1
|
+
=begin
|
2
|
+
# Copyright (c) 2018-2019 VMware, Inc. All Rights Reserved.
|
3
|
+
# SPDX-License-Identifier: MIT
|
4
|
+
|
5
|
+
# DO NOT MODIFY. THIS CODE IS GENERATED. CHANGES WILL BE OVERWRITTEN.
|
6
|
+
|
7
|
+
# runtime - No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
8
|
+
|
9
|
+
=end
|
10
|
+
|
11
|
+
require 'date'
|
12
|
+
require 'json'
|
13
|
+
require 'logger'
|
14
|
+
require 'tempfile'
|
15
|
+
require 'typhoeus'
|
16
|
+
require 'uri'
|
17
|
+
|
18
|
+
module VSphereAutomation
|
19
|
+
class ApiClient
|
20
|
+
# The Configuration object holding settings to be used in the API client.
|
21
|
+
attr_accessor :config
|
22
|
+
|
23
|
+
# Defines the headers to be used in HTTP requests of all API calls by default.
|
24
|
+
#
|
25
|
+
# @return [Hash]
|
26
|
+
attr_accessor :default_headers
|
27
|
+
|
28
|
+
# Initializes the ApiClient
|
29
|
+
# @option config [Configuration] Configuration for initializing the object, default to Configuration.default
|
30
|
+
def initialize(config = Configuration.default)
|
31
|
+
@config = config
|
32
|
+
@user_agent = "SDK/0.2.0 Ruby/#{RUBY_VERSION} (#{Gem::Platform.local.os}; #{Gem::Platform.local.version}; #{Gem::Platform.local.cpu})"
|
33
|
+
@default_headers = {
|
34
|
+
'Content-Type' => 'application/json',
|
35
|
+
'User-Agent' => @user_agent
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.default
|
40
|
+
@@default ||= ApiClient.new
|
41
|
+
end
|
42
|
+
|
43
|
+
# Call an API with given options.
|
44
|
+
#
|
45
|
+
# @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
|
46
|
+
# the data deserialized from response body (could be nil), response status code and response headers.
|
47
|
+
def call_api(http_method, path, opts = {})
|
48
|
+
request = build_request(http_method, path, opts)
|
49
|
+
response = request.run
|
50
|
+
|
51
|
+
if @config.debugging
|
52
|
+
@config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
|
53
|
+
end
|
54
|
+
|
55
|
+
unless opts.fetch(:return_type, {}).keys.include?(response.code.to_s) || response.success?
|
56
|
+
if response.timed_out?
|
57
|
+
fail ApiError.new('Connection timed out')
|
58
|
+
elsif response.code == 0
|
59
|
+
# Errors from libcurl will be made visible here
|
60
|
+
fail ApiError.new(:code => 0,
|
61
|
+
:message => response.return_message)
|
62
|
+
else
|
63
|
+
fail ApiError.new(:code => response.code,
|
64
|
+
:response_headers => response.headers,
|
65
|
+
:response_body => response.body),
|
66
|
+
response.status_message
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
if opts[:return_type]
|
71
|
+
data = deserialize(response, opts[:return_type][response.code.to_s])
|
72
|
+
if path == '/com/vmware/cis/session'
|
73
|
+
@config.api_key['vmware-api-session-id'] = data.value
|
74
|
+
end
|
75
|
+
else
|
76
|
+
data = nil
|
77
|
+
end
|
78
|
+
return data, response.code, response.headers
|
79
|
+
end
|
80
|
+
|
81
|
+
# Builds the HTTP request
|
82
|
+
#
|
83
|
+
# @param [String] http_method HTTP method/verb (e.g. POST)
|
84
|
+
# @param [String] path URL path (e.g. /account/new)
|
85
|
+
# @option opts [Hash] :header_params Header parameters
|
86
|
+
# @option opts [Hash] :query_params Query parameters
|
87
|
+
# @option opts [Hash] :form_params Query parameters
|
88
|
+
# @option opts [Object] :body HTTP body (JSON/XML)
|
89
|
+
# @return [Typhoeus::Request] A Typhoeus Request
|
90
|
+
def build_request(http_method, path, opts = {})
|
91
|
+
url = build_request_url(path)
|
92
|
+
http_method = http_method.to_sym.downcase
|
93
|
+
|
94
|
+
header_params = @default_headers.merge(opts[:header_params] || {})
|
95
|
+
query_params = opts[:query_params] || {}
|
96
|
+
form_params = opts[:form_params] || {}
|
97
|
+
|
98
|
+
update_params_for_auth! header_params, query_params, opts[:auth_names]
|
99
|
+
|
100
|
+
# set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
|
101
|
+
_verify_ssl_host = @config.verify_ssl_host ? 2 : 0
|
102
|
+
|
103
|
+
req_opts = {
|
104
|
+
:method => http_method,
|
105
|
+
:headers => header_params,
|
106
|
+
:params => query_params,
|
107
|
+
:params_encoding => @config.params_encoding,
|
108
|
+
:timeout => @config.timeout,
|
109
|
+
:ssl_verifypeer => @config.verify_ssl,
|
110
|
+
:ssl_verifyhost => _verify_ssl_host,
|
111
|
+
:sslcert => @config.cert_file,
|
112
|
+
:sslkey => @config.key_file,
|
113
|
+
:verbose => @config.debugging
|
114
|
+
}
|
115
|
+
|
116
|
+
# set custom cert, if provided
|
117
|
+
req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
|
118
|
+
|
119
|
+
if [:post, :patch, :put, :delete].include?(http_method)
|
120
|
+
req_body = build_request_body(header_params, form_params, opts[:body])
|
121
|
+
req_opts.update :body => req_body
|
122
|
+
if @config.debugging
|
123
|
+
@config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
request = Typhoeus::Request.new(url, req_opts)
|
128
|
+
download_file(request) if opts[:return_type] == 'File'
|
129
|
+
request
|
130
|
+
end
|
131
|
+
|
132
|
+
# Check if the given MIME is a JSON MIME.
|
133
|
+
# JSON MIME examples:
|
134
|
+
# application/json
|
135
|
+
# application/json; charset=UTF8
|
136
|
+
# APPLICATION/JSON
|
137
|
+
# */*
|
138
|
+
# @param [String] mime MIME
|
139
|
+
# @return [Boolean] True if the MIME is application/json
|
140
|
+
def json_mime?(mime)
|
141
|
+
(mime == '*/*') || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
|
142
|
+
end
|
143
|
+
|
144
|
+
# Deserialize the response to the given return type.
|
145
|
+
#
|
146
|
+
# @param [Response] response HTTP response
|
147
|
+
# @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
|
148
|
+
def deserialize(response, return_type)
|
149
|
+
body = response.body
|
150
|
+
|
151
|
+
# handle file downloading - return the File instance processed in request callbacks
|
152
|
+
# note that response body is empty when the file is written in chunks in request on_body callback
|
153
|
+
return @tempfile if return_type == 'File'
|
154
|
+
|
155
|
+
return nil if body.nil? || body.empty?
|
156
|
+
|
157
|
+
# return response body directly for String return type
|
158
|
+
return body if return_type == 'String'
|
159
|
+
|
160
|
+
# ensuring a default content type
|
161
|
+
content_type = response.headers['Content-Type'] || 'application/json'
|
162
|
+
|
163
|
+
fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
|
164
|
+
|
165
|
+
begin
|
166
|
+
data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
|
167
|
+
rescue JSON::ParserError => e
|
168
|
+
if %w(String Date DateTime).include?(return_type)
|
169
|
+
data = body
|
170
|
+
else
|
171
|
+
raise e
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
convert_to_type data, return_type
|
176
|
+
end
|
177
|
+
|
178
|
+
# Convert data to the given return type.
|
179
|
+
# @param [Object] data Data to be converted
|
180
|
+
# @param [String] return_type Return type
|
181
|
+
# @return [Mixed] Data in a particular type
|
182
|
+
def convert_to_type(data, return_type)
|
183
|
+
return nil if data.nil?
|
184
|
+
case return_type
|
185
|
+
when 'String'
|
186
|
+
data.to_s
|
187
|
+
when 'Integer'
|
188
|
+
data.to_i
|
189
|
+
when 'Float'
|
190
|
+
data.to_f
|
191
|
+
when 'BOOLEAN'
|
192
|
+
data == true
|
193
|
+
when 'DateTime'
|
194
|
+
# parse date time (expecting ISO 8601 format)
|
195
|
+
DateTime.parse data
|
196
|
+
when 'Date'
|
197
|
+
# parse date time (expecting ISO 8601 format)
|
198
|
+
Date.parse data
|
199
|
+
when 'Object'
|
200
|
+
# generic object (usually a Hash), return directly
|
201
|
+
data
|
202
|
+
when /\AArray<(.+)>\z/
|
203
|
+
# e.g. Array<Pet>
|
204
|
+
sub_type = $1
|
205
|
+
data.map { |item| convert_to_type(item, sub_type) }
|
206
|
+
when /\AHash\<String, (.+)\>\z/
|
207
|
+
# e.g. Hash<String, Integer>
|
208
|
+
sub_type = $1
|
209
|
+
{}.tap do |hash|
|
210
|
+
data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
|
211
|
+
end
|
212
|
+
else
|
213
|
+
# models, e.g. Pet
|
214
|
+
VSphereAutomation.const_get(return_type).new.tap do |model|
|
215
|
+
model.build_from_hash data
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
# Save response body into a file in (the defined) temporary folder, using the filename
|
221
|
+
# from the "Content-Disposition" header if provided, otherwise a random filename.
|
222
|
+
# The response body is written to the file in chunks in order to handle files which
|
223
|
+
# size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
|
224
|
+
# process can use.
|
225
|
+
#
|
226
|
+
# @see Configuration#temp_folder_path
|
227
|
+
def download_file(request)
|
228
|
+
tempfile = nil
|
229
|
+
encoding = nil
|
230
|
+
request.on_headers do |response|
|
231
|
+
content_disposition = response.headers['Content-Disposition']
|
232
|
+
if content_disposition && content_disposition =~ /filename=/i
|
233
|
+
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
234
|
+
prefix = sanitize_filename(filename)
|
235
|
+
else
|
236
|
+
prefix = 'download-'
|
237
|
+
end
|
238
|
+
prefix = prefix + '-' unless prefix.end_with?('-')
|
239
|
+
encoding = response.body.encoding
|
240
|
+
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
|
241
|
+
@tempfile = tempfile
|
242
|
+
end
|
243
|
+
request.on_body do |chunk|
|
244
|
+
chunk.force_encoding(encoding)
|
245
|
+
tempfile.write(chunk)
|
246
|
+
end
|
247
|
+
request.on_complete do |response|
|
248
|
+
tempfile.close
|
249
|
+
@config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
|
250
|
+
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
|
251
|
+
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
252
|
+
"explicitly with `tempfile.delete`"
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
# Sanitize filename by removing path.
|
257
|
+
# e.g. ../../sun.gif becomes sun.gif
|
258
|
+
#
|
259
|
+
# @param [String] filename the filename to be sanitized
|
260
|
+
# @return [String] the sanitized filename
|
261
|
+
def sanitize_filename(filename)
|
262
|
+
filename.gsub(/.*[\/\\]/, '')
|
263
|
+
end
|
264
|
+
|
265
|
+
def build_request_url(path)
|
266
|
+
# Add leading and trailing slashes to path
|
267
|
+
path = "/#{path}".gsub(/\/+/, '/')
|
268
|
+
URI.encode(@config.base_url + path)
|
269
|
+
end
|
270
|
+
|
271
|
+
# Builds the HTTP request body
|
272
|
+
#
|
273
|
+
# @param [Hash] header_params Header parameters
|
274
|
+
# @param [Hash] form_params Query parameters
|
275
|
+
# @param [Object] body HTTP body (JSON/XML)
|
276
|
+
# @return [String] HTTP body data in the form of string
|
277
|
+
def build_request_body(header_params, form_params, body)
|
278
|
+
# http form
|
279
|
+
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
|
280
|
+
header_params['Content-Type'] == 'multipart/form-data'
|
281
|
+
data = {}
|
282
|
+
form_params.each do |key, value|
|
283
|
+
case value
|
284
|
+
when ::File, ::Array, nil
|
285
|
+
# let typhoeus handle File, Array and nil parameters
|
286
|
+
data[key] = value
|
287
|
+
else
|
288
|
+
data[key] = value.to_s
|
289
|
+
end
|
290
|
+
end
|
291
|
+
elsif body
|
292
|
+
data = body.is_a?(String) ? body : body.to_json
|
293
|
+
else
|
294
|
+
data = nil
|
295
|
+
end
|
296
|
+
data
|
297
|
+
end
|
298
|
+
|
299
|
+
# Update hearder and query params based on authentication settings.
|
300
|
+
#
|
301
|
+
# @param [Hash] header_params Header parameters
|
302
|
+
# @param [Hash] query_params Query parameters
|
303
|
+
# @param [String] auth_names Authentication scheme name
|
304
|
+
def update_params_for_auth!(header_params, query_params, auth_names)
|
305
|
+
Array(auth_names).each do |auth_name|
|
306
|
+
auth_setting = @config.auth_settings[auth_name]
|
307
|
+
next unless auth_setting
|
308
|
+
case auth_setting[:in]
|
309
|
+
when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
|
310
|
+
when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
|
311
|
+
else fail ArgumentError, 'Authentication token must be in `query` of `header`'
|
312
|
+
end
|
313
|
+
end
|
314
|
+
end
|
315
|
+
|
316
|
+
# Sets user agent in HTTP header
|
317
|
+
#
|
318
|
+
# @param [String] user_agent User agent (e.g. openapi-generator/ruby/1.0.0)
|
319
|
+
def user_agent=(user_agent)
|
320
|
+
@user_agent = user_agent
|
321
|
+
@default_headers['User-Agent'] = @user_agent
|
322
|
+
end
|
323
|
+
|
324
|
+
# Return Accept header based on an array of accepts provided.
|
325
|
+
# @param [Array] accepts array for Accept
|
326
|
+
# @return [String] the Accept header (e.g. application/json)
|
327
|
+
def select_header_accept(accepts)
|
328
|
+
return nil if accepts.nil? || accepts.empty?
|
329
|
+
# use JSON when present, otherwise use all of the provided
|
330
|
+
json_accept = accepts.find { |s| json_mime?(s) }
|
331
|
+
json_accept || accepts.join(',')
|
332
|
+
end
|
333
|
+
|
334
|
+
# Return Content-Type header based on an array of content types provided.
|
335
|
+
# @param [Array] content_types array for Content-Type
|
336
|
+
# @return [String] the Content-Type header (e.g. application/json)
|
337
|
+
def select_header_content_type(content_types)
|
338
|
+
# use application/json by default
|
339
|
+
return 'application/json' if content_types.nil? || content_types.empty?
|
340
|
+
# use JSON when present, otherwise use the first one
|
341
|
+
json_content_type = content_types.find { |s| json_mime?(s) }
|
342
|
+
json_content_type || content_types.first
|
343
|
+
end
|
344
|
+
|
345
|
+
# Convert object (array, hash, object, etc) to JSON string.
|
346
|
+
# @param [Object] model object to be converted into JSON string
|
347
|
+
# @return [String] JSON string representation of the object
|
348
|
+
def object_to_http_body(model)
|
349
|
+
return model if model.nil? || model.is_a?(String)
|
350
|
+
local_body = nil
|
351
|
+
if model.is_a?(Array)
|
352
|
+
local_body = model.map { |m| object_to_hash(m) }
|
353
|
+
else
|
354
|
+
local_body = object_to_hash(model)
|
355
|
+
end
|
356
|
+
local_body.to_json
|
357
|
+
end
|
358
|
+
|
359
|
+
# Convert object(non-array) to hash.
|
360
|
+
# @param [Object] obj object to be converted into JSON string
|
361
|
+
# @return [String] JSON string representation of the object
|
362
|
+
def object_to_hash(obj)
|
363
|
+
if obj.respond_to?(:to_hash)
|
364
|
+
obj.to_hash
|
365
|
+
else
|
366
|
+
obj
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
370
|
+
# Build parameter value according to the given collection format.
|
371
|
+
# @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
|
372
|
+
def build_collection_param(param, collection_format)
|
373
|
+
case collection_format
|
374
|
+
when :csv
|
375
|
+
param.join(',')
|
376
|
+
when :ssv
|
377
|
+
param.join(' ')
|
378
|
+
when :tsv
|
379
|
+
param.join("\t")
|
380
|
+
when :pipes
|
381
|
+
param.join('|')
|
382
|
+
when :multi
|
383
|
+
# return the array directly as typhoeus will handle it as expected
|
384
|
+
param
|
385
|
+
else
|
386
|
+
fail "unknown collection format: #{collection_format.inspect}"
|
387
|
+
end
|
388
|
+
end
|
389
|
+
end
|
390
|
+
end
|