Class: Synapse::HTTPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse_api/http_request.rb

Overview

Wrapper for HTTP requests using RestClient.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, client_id:, client_secret:, fingerprint:, ip_address:, raise_for_202: false, **options) ⇒ 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, **options)
          @raise_for_202 = raise_for_202
  log_to         = options[:log_to] || 'stdout'
  RestClient.log = log_to if options[:logging]
  @logging       = options[: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_urlObject

Returns the value of attribute base_url.



# File 'lib/synapse_api/http_request.rb', line 9


#configObject

Returns the value of attribute config.



# File 'lib/synapse_api/http_request.rb', line 9


#raise_for_202Boolean



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

Raises:



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.

Raises:



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

#headersHash 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.

Raises:



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

Raises:



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, **options)
   #copy of current headers
   headers = get_headers

   # update the headers with idempotency_key
   if options[:idempotency_key]
       headers = headers.merge({'X-SP-IDEMPOTENCY-KEY' => options[: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