This repository has been archived by the owner on May 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploader.rb
81 lines (69 loc) · 2.49 KB
/
uploader.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
require "json"
require "net/http"
require "net/http/post/multipart"
if ARGV.empty?
puts "Usage: #{$PROGRAM_NAME} file1 file2 file3..."
Process.exit 1
end
def request(path:, method: :get, payload: nil, access_token: nil)
response = perform_request path: path, method: method, payload: payload,
access_token: access_token
puts response.inspect
puts response.body
JSON.parse response.body, symbolize_names: true
end
def perform_request(path:, method:, payload:, access_token:)
uri = URI("http://#{ENV["BRAIN_SERVICE_HOST"]}:#{ENV["BRAIN_SERVICE_PORT"]}/api/v1/#{path}")
http = Net::HTTP.new(uri.host, uri.port)
req = build_request uri: uri, method: method
req["Authorization"] = "Bearer #{access_token}" if access_token
req.body = payload.to_json if !%i[head get delete].include?(method) && payload
http.request req
end
def build_request(uri:, method:)
case method
when :get
Net::HTTP::Get.new(uri.path)
when :post
Net::HTTP::Post.new(uri.path, "Content-Type" => "application/json")
else
raise "unknown method"
end
end
def login
puts "Logging in..."
request path: "sessions", method: :post, payload: { username: "test", password: "test" }
end
def upload_file(access_token:)
request path: "objects", method: :post, access_token: access_token
end
def do_upload_file(uri:, access_token:, filename:)
File.open(filename) do |file|
req = Net::HTTP::Post::Multipart.new uri.request_uri,
"object[payload]" => UploadIO.new(file, "application/data",
filename)
req["Authorization"] = "Bearer #{access_token}"
Net::HTTP.start(uri.host, uri.port) do |http|
http.request req
end
end
end
def upload_file_to_cell(access_token:, upload_token:, cell_ip:, filename:)
uri = URI.parse("http://#{cell_ip}/api/v1/objects/?upload_token=#{upload_token}")
puts uri.to_s
response = do_upload_file uri: uri, access_token: access_token, filename: filename
puts response.inspect
puts response.body
end
access_token = login[:access_token]
uploads = []
ARGV.count.times do
uploads << upload_file(access_token: access_token)
end
uploads.each_with_index do |upload_info, i|
upload_token = upload_info[:upload_token]
cell_ip = upload_info[:cell][:ip_address]
puts "Uploading file #{ARGV[i]}..."
upload_file_to_cell access_token: access_token, upload_token: upload_token, cell_ip: cell_ip,
filename: ARGV[i]
end