Module: Procore::Requestable
- Included in:
- Client
- Defined in:
- lib/procore/requestable.rb
Overview
Module which defines HTTP verbs GET, POST, PUT, PATCH and DELETE. Is included in Client. Has support for Idempotency Tokens on POST and PATCH.
Constant Summary collapse
- HTTP_EXCEPTIONS =
[ Errno::ECONNREFUSED, Errno::ECONNRESET, Procore::OAuthError, RestClient::Exceptions::Timeout, RestClient::ServerBrokeConnection, ].freeze
Instance Method Summary collapse
- #delete(path, version: nil, query: {}, options: {}) ⇒ Response
- #get(path, version: nil, query: {}, options: {}) ⇒ Response
- #patch(path, version: nil, body: {}, options: {}) ⇒ Response
- #post(path, version: nil, body: {}, options: {}) ⇒ Response
- #put(path, version: nil, body: {}, options: {}) ⇒ Response
- #sync(path, version: nil, body: {}, options: {}) ⇒ Response
Instance Method Details
#delete(path, version: nil, query: {}, options: {}) ⇒ Response
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/procore/requestable.rb', line 226 def delete(path, version: nil, query: {}, options: {}) full_path = full_path(path, version) Util.log_info( "API Request Initiated", path: full_path, method: "DELETE", headers: headers(), query: query.to_s, ) with_response_handling do RestClient::Request.execute( method: :delete, url: full_path, headers: headers.merge(params: query), timeout: Procore.configuration.timeout, ) end end |
#get(path, version: nil, query: {}, options: {}) ⇒ Response
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/procore/requestable.rb', line 30 def get(path, version: nil, query: {}, options: {}) full_path = full_path(path, version) Util.log_info( "API Request Initiated", path: full_path, method: "GET", query: query.to_s, ) with_response_handling do RestClient::Request.execute( method: :get, url: full_path, headers: headers().merge(params: query), timeout: Procore.configuration.timeout, ) end end |
#patch(path, version: nil, body: {}, options: {}) ⇒ Response
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/procore/requestable.rb', line 130 def patch(path, version: nil, body: {}, options: {}) full_path = full_path(path, version) Util.log_info( "API Request Initiated", path: full_path, method: "PATCH", body: body.to_s, ) with_response_handling(request_body: body) do RestClient::Request.execute( method: :patch, url: full_path, payload: payload(body), headers: headers(), timeout: Procore.configuration.timeout, ) end end |
#post(path, version: nil, body: {}, options: {}) ⇒ Response
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/procore/requestable.rb', line 64 def post(path, version: nil, body: {}, options: {}) full_path = full_path(path, version) Util.log_info( "API Request Initiated", path: full_path, method: "POST", body: body.to_s, ) with_response_handling(request_body: body) do RestClient::Request.execute( method: :post, url: full_path, payload: payload(body), headers: headers(), timeout: Procore.configuration.timeout, ) end end |
#put(path, version: nil, body: {}, options: {}) ⇒ Response
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/procore/requestable.rb', line 95 def put(path, version: nil, body: {}, options: {}) full_path = full_path(path, version) Util.log_info( "API Request Initiated", path: full_path, method: "PUT", body: body.to_s, ) with_response_handling(request_body: body) do RestClient::Request.execute( method: :put, url: full_path, payload: payload(body), headers: headers(), timeout: Procore.configuration.timeout, ) end end |
#sync(path, version: nil, body: {}, options: {}) ⇒ Response
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/procore/requestable.rb', line 174 def sync(path, version: nil, body: {}, options: {}) full_path = full_path(path, version) batch_size = [:batch_size] || Procore.configuration.default_batch_size if batch_size > 1000 batch_size = 1000 end Util.log_info( "API Request Initiated", path: full_path, method: "SYNC", batch_size: batch_size, ) groups = body[:updates].each_slice(batch_size).to_a responses = groups.map do |group| batched_body = body.merge(updates: group) with_response_handling(request_body: batched_body) do RestClient::Request.execute( method: :patch, url: full_path, payload: payload(batched_body), headers: headers(), timeout: Procore.configuration.timeout, ) end end Procore::Response.new( body: responses.reduce({}) do |combined, response| combined.deep_merge(response.body) { |_, v1, v2| v1 + v2 } end.to_json, headers: responses.map(&:headers).inject({}, &:deep_merge), code: 200, request: responses.last&.request, request_body: body, ) end |