-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestheart.rb
More file actions
158 lines (122 loc) · 3.7 KB
/
restheart.rb
File metadata and controls
158 lines (122 loc) · 3.7 KB
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
module Restheart
class ResponseCode
attr_reader :code
def initialize(code)
@code = code
@json = nil
end
def data
nil
end
end
class Response
attr_reader :code, :json
def initialize(http_response)
@code = http_response.code
@json = http_response.body # assumes JSON!
end
def data
JSON.parse(@json)
end
end
class Connection
def initialize cfg
@base_path = "#{cfg.protocol}://#{cfg.host}:#{cfg.port}"
@user = cfg.user
@password = cfg.password
#@content_type = "application/hal+json"
@content_type = "application/json"
end
def get path, params={}
resource = client_resource(path)
begin
http_response = resource.get(params: params)
response = Restheart::Response.new(http_response)
rescue RestClient::NotFound
$stderr.puts "Object not found: #{path}"
response = nil
rescue RestClient::BadRequest
$stderr.puts "(get) BadRequest: #{path}"
response = nil
end
response
end
def delete path, headers
resource = client_resource(path)
begin
response = resource.delete(headers)
rescue Exception => e
$stderr.puts "EXCEPTION: #{e.inspect}"
end
response
end
def post path, attributes, etag=nil
_write(:post, path, attributes, etag)
end
def put path, attributes, etag=nil
_write(:put, path, attributes, etag)
end
def patch path, attributes, etag=nil
_write(:patch, path, attributes, etag)
end
def noop path, attributes, etag=nil
200
end
private
def _cleanse attributes
# RH will 406 on attempts at writing _etag or _id
# RH will silently ignore anything else with a leading _
attributes.reject{|k,v| k =~ /^_/ || k.to_sym=='id'}
end
def _write method, path, attributes, etag
resource = client_resource(path)
atr = _cleanse(attributes).to_json
headers = { :content_type => @content_type,
:accept => "application/json, */*" }
method_name = method.to_s
response_code = nil
loop do
begin
http_response = resource.send(method, atr, headers)
response = Restheart::Response.new(http_response)
response_code = response.code
done = true
rescue RestClient::NotFound => e
$stderr.puts "(#{method_name}) Not Found: #{path}"
response_code = e.http_code
done = true
rescue RestClient::BadRequest => e
$stderr.puts "(#{method_name}) Bad Request: #{path}"
response_code = e.http_code
done = true
rescue RestClient::NotAcceptable => e
$stderr.puts "(#{method_name}) Not Acceptable: #{path}"
response_code = e.http_code
done = true
rescue RestClient::PreconditionFailed => e
$stderr.puts "(#{method_name}) Not Acceptable: #{path}"
$stderr.puts " #{response.inspect}"
response_code = e.http_code
done = true
rescue RestClient::Exceptions::OpenTimeout => e
$stderr.puts "(#{method_name}) Timeout: #{path}"
$stderr.puts " #{response.inspect}"
# TODO: limit the number of retries
$stderr.puts "(#{method_name}) retrying.."
done = false
end
break if done
end
response_code
end
def client_resource path
resource_path = _parse(path)
RestClient::Resource.new(resource_path, @user, @password)
end
def _parse path
parts = path.split('/').reject{|i| i.nil? || i==''}
resource = [@base_path]
resource.concat(parts).join('/')
end
end
end