-
Notifications
You must be signed in to change notification settings - Fork 0
/
oneflowclient.rb
177 lines (147 loc) · 6.11 KB
/
oneflowclient.rb
1
2
3
4
5
6
7
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
require './models/order'
require 'httparty'
ver_2_2 = Gem::Version.new('2.2')
ver_current = Gem::Version.new(RUBY_VERSION)
if (ver_2_2 > ver_current)
require 'digest/hmac' # Ruby version <= 2.1.10
else
require 'openssl' # Ruby version 2.2 =>
end
class OneflowClient
attr_accessor :endpoint, :token, :secret, :order, :retries, :retryCondition, :retryDelay
def initialize(endpoint, token, secret, options = nil)
@endpoint, @token, @secret = endpoint, token, secret
set_options(options)
end
def set_options(options)
@retries = (options && options.retries) ? options.retries : 3
@retryCondition = (options && options.respond_to?("retryCondition")) ? options.method("retryCondition") : method(:isRetryableError)
@retryDelay = (options && options.respond_to?("retryDelay")) ? options.method("retryDelay") : method(:exponentialDelay)
end
def create_order(destination)
@order = Order.new(destination)
end
def request(method, resourcePath, data="")
post_url = @endpoint + resourcePath
parse_url = URI.parse(post_url)
timestamp = Time.now.getutc
auth_header = make_token(method, parse_url.path, timestamp)
for attemp in 1..@retries
case method.downcase
when "post"
response = post_request(post_url, data, timestamp, auth_header)
when "put"
response = put_request(post_url, data, timestamp, auth_header)
when "get"
response = get_request(post_url, timestamp, auth_header)
when "delete"
response = delete_request(post_url, timestamp, auth_header)
end
if [email protected](response) then break end
@retryDelay.call(response, attemp)
end
return response
end
def get_request(url, timestamp, auth_header)
response = HTTParty.get(url, :headers => {
'x-oneflow-date' => timestamp.to_s,
'x-oneflow-authorization' => auth_header,
'x-oneflow-algorithm' => 'SHA256'
})
end
def post_request(url, data, timestamp, auth_header)
response = HTTParty.post(url, :body => data, :headers => {
'Content-Type' => 'application/json',
'x-oneflow-date' => timestamp.to_s,
'x-oneflow-authorization' => auth_header,
'x-oneflow-algorithm' => 'SHA256'
})
end
def put_request(url, data, timestamp, auth_header)
response = HTTParty.put(url, :body => data, :headers => {
'Content-Type' => 'application/json',
'x-oneflow-date' => timestamp.to_s,
'x-oneflow-authorization' => auth_header,
'x-oneflow-algorithm' => 'SHA256'
})
end
def delete_request(url, data, timestamp, auth_header)
response = HTTParty.delete(url, :headers => {
'x-oneflow-date' => timestamp.to_s,
'x-oneflow-authorization' => auth_header,
'x-oneflow-algorithm' => 'SHA256'
})
end
def upload_request(url, local_filename)
puts "-----------------"
puts local_filename
puts url
puts "-----------------"
# begin
# RestClient.put(url, file: File.new(local_filename), :headers => {'Content-Type' => 'application/pdf'})
# rescue => e
# e
# end
# uri = URI.parse(url)
# params = CGI::parse(uri.query)
#https://s3-eu-west-1.amazonaws.com/dev-oneflow-files/ultraprint-539090ca61e529002b0a6386/files-in/5465cc95ed54f4796313d57c.pdf
# puts params['Signature']
# AWS.config(access_key_id: params['AWSAccessKeyId'])
# s3 = AWS::S3.new
# bucket = s3.buckets['dev-oneflow-files']
# form = bucket.presigned_post(:key => "ultraprint-539090ca61e529002b0a6386/files-in/5465cc95ed54f4796313d57c.pdf")
# bucket.objects["ultraprint-539090ca61e529002b0a6386/files-in/5465cc95ed54f4796313d57c.pdf"].write(:file => local_filename)
# file = File.open(local_filename)
# response = HTTMultiParty.put(url, :body => file, :headers => {
# 'Content-Type' => 'application/pdf',
# 'Content-Length' => file.size.to_s
# })
end
def make_token(method, path, timestamp)
ver_2_2 = Gem::Version.new('2.2')
ver_current = Gem::Version.new(RUBY_VERSION)
if (ver_2_2 > ver_current)
# Ruby Version <= 2.1.10
value = method.upcase + " " + URI.decode(path) + " " + timestamp.to_s
hmac = Digest::HMAC.new(@secret, Digest::SHA256)
hmac.update(value)
signature = hmac.hexdigest
else
# Ruby Version > v2.5 =>
value = method.upcase + " " + CGI::unescape(path) + " " + timestamp.to_s
signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @secret, value)
end
local_authorization = @token + ":" + signature
end
def self.converter(object)
if (object.instance_variables.size > 0)
hash = {}
object.instance_variables.each {|var| hash[var.to_s.delete("@")] = OneflowClient.converter(object.instance_variable_get(var))}
return hash
elsif (object.class == Array)
array = []
object.each {|var| array.push(OneflowClient.converter(var))}
return array
else
return object
end
end
def validate_order
request("POST", "/order/validate", OneflowClient.converter(@order).to_json)
end
def submit_order
request("POST", "/order", OneflowClient.converter(@order).to_json)
end
def upload_file(file_record, local_filename)
upload_request(file_record["url"], local_filename)
end
def isRetryableError(response)
return response.code == 429
end
def exponentialDelay(response, retryCount)
coefficient = (response.code == 429) ? (0.3 * 60) : 0.1 # assume 429 limit by minute
delay = 2.pow(retryCount) * coefficient
randomSum = delay * 0.04 * rand(11) # 0-40% of the delay
sleep (delay + randomSum)
end
end