Class: Synapse::HTTPClient
- Inherits:
-
Object
- Object
- Synapse::HTTPClient
- Defined in:
- lib/synapse_api/http_request.rb
Overview
Wrapper for HTTP requests using RestClient.
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#config ⇒ Object
Returns the value of attribute config.
-
#raise_for_202 ⇒ Boolean
Relating to how to handle 202 exception.
Instance Method Summary collapse
-
#delete(path) ⇒ Hash
Sends a DELETE request to the given path.
-
#get(path) ⇒ Hash
API response.
- #headers ⇒ Hash (also: #get_headers)
-
#initialize(base_url:, client_id:, client_secret:, fingerprint:, ip_address:, raise_for_202: false, **options) ⇒ HTTPClient
constructor
A new instance of HTTPClient.
- #oauthenticate(user_id:) ⇒ Object
-
#patch(path, payload) ⇒ Hash
API response.
- #post(path, payload, **options) ⇒ Object
- #update_headers(oauth_key: nil, fingerprint: nil, client_id: nil, client_secret: nil, ip_address: nil, idemopotency_key: nil) ⇒ Object
Constructor Details
#initialize(base_url:, client_id:, client_secret:, fingerprint:, ip_address:, raise_for_202: false, **options) ⇒ HTTPClient
Returns a new instance of HTTPClient.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/synapse_api/http_request.rb', line 25 def initialize(base_url:, client_id:, client_secret:, fingerprint:, ip_address:, raise_for_202:false, **) @raise_for_202 = raise_for_202 log_to = [:log_to] || 'stdout' RestClient.log = log_to if [:logging] @logging = [:logging] @config = { client_id: client_id, client_secret: client_secret, fingerprint: fingerprint, ip_address: ip_address, oauth_key: '', } @base_url = base_url end |
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
|
# File 'lib/synapse_api/http_request.rb', line 9
|
#config ⇒ Object
Returns the value of attribute config.
|
# File 'lib/synapse_api/http_request.rb', line 9
|
#raise_for_202 ⇒ Boolean
Returns relating to how to handle 202 exception.
15 |
# File 'lib/synapse_api/http_request.rb', line 15 attr_accessor :base_url, :config, :raise_for_202 |
Instance Method Details
#delete(path) ⇒ Hash
Sends a DELETE request to the given path
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/synapse_api/http_request.rb', line 135 def delete(path) response = with_error_handling {RestClient.delete(full_url(path), headers)} puts 'RESPONSE:', JSON.parse(response) if @logging response = JSON.parse(response) if raise_for_202 && response["http_code"] == "202" raise Error.from_response(response) elsif response["error"] raise Error.from_response(response) else response end end |
#get(path) ⇒ Hash
Returns API response.
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/synapse_api/http_request.rb', line 117 def get(path) response = with_error_handling {RestClient.get(full_url(path), headers)} puts 'RESPONSE:', JSON.parse(response) if @logging response = JSON.parse(response) if raise_for_202 && response["http_code"] == "202" raise Error.from_response(response) elsif response["error"] raise Error.from_response(response) else response end end |
#headers ⇒ Hash Also known as: get_headers
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/synapse_api/http_request.rb', line 43 def headers user = "#{config[:oauth_key]}|#{config[:fingerprint]}" gateway = "#{config[:client_id]}|#{config[:client_secret]}" headers = { content_type: :json, accept: :json, 'X-SP-GATEWAY' => gateway, 'X-SP-USER' => user, 'X-SP-USER-IP' => config[:ip_address], } if config[:idemopotency_key] headers['X-SP-IDEMPOTENCY-KEY'] = config[:idemopotency_key] end headers end |
#oauthenticate(user_id:) ⇒ Object
173 174 175 |
# File 'lib/synapse_api/http_request.rb', line 173 def oauthenticate(user_id:) refresh_token = refresh_token(user_id: user_id) end |
#patch(path, payload) ⇒ Hash
Returns API response.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/synapse_api/http_request.rb', line 154 def patch(path, payload) response = with_error_handling {RestClient::Request.execute(:method => :patch, :url => full_url(path), :payload => payload.to_json, :headers => headers, :timeout => 300 )} p 'RESPONSE:', JSON.parse(response) if @logging response = JSON.parse(response) if raise_for_202 && response["http_code"] == "202" raise Error.from_response(response) elsif response["error"] raise Error.from_response(response) else response end end |
#post(path, payload, **options) ⇒ Object
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 |
# File 'lib/synapse_api/http_request.rb', line 86 def post(path, payload, **) #copy of current headers headers = get_headers # update the headers with idempotency_key if [:idempotency_key] headers = headers.merge({'X-SP-IDEMPOTENCY-KEY' => [:idempotency_key]}) end response = with_error_handling { RestClient::Request.execute(:method => :post, :url => full_url(path), :payload => payload.to_json, :headers => headers, :timeout => 300 ) } puts 'RESPONSE:', JSON.parse(response) if @logging response = JSON.parse(response) if raise_for_202 && response["http_code"] == "202" raise Error.from_response(response) elsif response["error"] raise Error.from_response(response) else response end end |
#update_headers(oauth_key: nil, fingerprint: nil, client_id: nil, client_secret: nil, ip_address: nil, idemopotency_key: nil) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/synapse_api/http_request.rb', line 70 def update_headers(oauth_key: nil, fingerprint: nil, client_id: nil, client_secret: nil, ip_address: nil, idemopotency_key: nil) config[:fingerprint] = fingerprint if fingerprint config[:oauth_key] = oauth_key if oauth_key config[:client_id] = client_id if client_id config[:client_secret] = client_secret if client_secret config[:ip_address] = ip_address if ip_address config[:idemopotency_key] = idemopotency_key if idemopotency_key nil end |