This repository was archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastools.rb
113 lines (91 loc) · 2.75 KB
/
astools.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
require 'net/http'
require 'json'
require 'uri'
require 'io/console'
module ASTools
def self.backend_url
# the config.rb default; set this to whatever your backend URL is
"http://localhost:8089"
end
def self.store_user_session(session)
Thread.current[:backend_session] = session
end
module User
def self.get_session(opts = {})
puts "Logging into ArchivesSpace at #{ASTools.backend_url}... "
unless opts[:login]
print "login: "
opts[:login] = gets.chomp
end
unless opts[:password]
print "password: "
opts[:password] = STDIN.noecho(&:gets).chomp
end
print "\n"
uri = URI("#{ASTools.backend_url}/users/#{opts[:login]}/login")
response = Net::HTTP.post_form(uri, 'password' => opts[:password])
if response.is_a?(Net::HTTPSuccess) || response.code == "200"
ASTools.store_user_session(JSON.parse(response.body)['session'])
else
puts JSON.parse(response.body)['error']
get_session
end
end
end
module HTTP
def self.backend_url
ASTools.backend_url
end
def self.current_backend_session
Thread.current[:backend_session]
end
def self.do_http_request(url, req)
req['X-ArchivesSpace-Session'] = current_backend_session
begin
response = Net::HTTP.start(url.host, url.port) {|http| http.request(req)}
rescue StandardError
puts "Connection lost. Retrying in ten seconds... "
sleep(10)
retry
end
response
end
def self.get_response(url)
req = Net::HTTP::Get.new(url)
do_http_request(url, req)
end
def self.get_data(uri, params = {})
uri = URI.parse(URI.encode("#{backend_url}#{uri}"))
uri.query = URI.encode_www_form(params)
response = get_response(uri)
if response.is_a?(Net::HTTPSuccess) || response.code == "200"
response.body
elsif response.code == "412"
puts "Session expired or not found. Refreshing..."
ASTools::User.get_session
else
nil
end
end
def self.get_json(uri, params = {})
uri = URI.parse(URI.encode("#{backend_url}#{uri}"))
uri.query = URI.encode_www_form(params)
response = get_response(uri)
if response.is_a?(Net::HTTPSuccess) || response.code == "200"
JSON.parse(response.body)
elsif response.code == "412"
puts "Session expired or not found. Refreshing..."
ASTools::User.get_session
else
nil
end
end
def self.post_json(uri, json)
uri = URI.parse(URI.encode("#{backend_url}#{uri}"))
req = Net::HTTP::Post.new(uri)
req['Content-Type'] = "application/json"
req.body = json.to_json
do_http_request(uri, req)
end
end
end