Class: SynapsePayRest::HTTPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse_pay_rest/http_client.rb

Overview

Wrapper for HTTP requests using RestClient.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, client_id:, fingerprint:, ip_address:, client_secret:, **options) ⇒ HTTPClient

Returns a new instance of HTTPClient.

Parameters:

  • base_url (String)

    the base url of the API (production or sandbox)

  • client_id (String)
  • client_secret (String)
  • fingerprint (String)
  • ip_address (String)
  • logging (Boolean)

    (optional) logs to stdout when true

  • log_to (String)

    (optional) file path to log to file (logging must be true)

  • proxy_url (String)

    (optional) proxy url which is used to proxy outbound requests



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/synapse_pay_rest/http_client.rb', line 25

def initialize(base_url:, client_id:, fingerprint:, ip_address:,
               client_secret:, **options)
  log_to         = options[:log_to] || 'stdout'
  RestClient.log = log_to if options[:logging]
  @logging       = options[:logging]

  RestClient.proxy = options[:proxy_url] if options[:proxy_url]
  @proxy_url = options[:proxy_url]

  @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_urlString

Returns the base url of the API (production or sandbox).

Returns:

  • (String)

    the base url of the API (production or sandbox)



11
12
13
# File 'lib/synapse_pay_rest/http_client.rb', line 11

def base_url
  @base_url
end

#configHash

Returns various settings related to request headers.

Returns:

  • (Hash)

    various settings related to request headers



11
# File 'lib/synapse_pay_rest/http_client.rb', line 11

attr_accessor :base_url, :config

#proxy_urlObject

Returns the value of attribute proxy_url.



15
16
17
# File 'lib/synapse_pay_rest/http_client.rb', line 15

def proxy_url
  @proxy_url
end

Instance Method Details

#delete(path) ⇒ Hash

Sends a DELETE request to the given path with the given payload.

Parameters:

  • path (String)

Returns:

  • (Hash)

    API response

Raises:



134
135
136
137
138
# File 'lib/synapse_pay_rest/http_client.rb', line 134

def delete(path)
  response = with_error_handling { RestClient.delete(full_url(path), headers) }
  p 'RESPONSE:', JSON.parse(response) if @logging
  JSON.parse(response)
end

#get(path) ⇒ Hash

Sends a GET request to the given path with the given payload.

Parameters:

  • path (String)

Returns:

  • (Hash)

    API response

Raises:



121
122
123
124
125
# File 'lib/synapse_pay_rest/http_client.rb', line 121

def get(path)
  response = with_error_handling { RestClient.get(full_url(path), headers) }
  p 'RESPONSE:', JSON.parse(response) if @logging
  JSON.parse(response)
end

#headersHash Also known as: get_headers

Returns headers for HTTP requests.

Returns:

  • (Hash)


47
48
49
50
51
52
53
54
55
56
57
# File 'lib/synapse_pay_rest/http_client.rb', line 47

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]
  }
end

#patch(path, payload) ⇒ Hash

Sends a PATCH request to the given path with the given payload.

Parameters:

  • path (String)
  • payload (Hash)

Returns:

  • (Hash)

    API response

Raises:



108
109
110
111
112
# File 'lib/synapse_pay_rest/http_client.rb', line 108

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
  JSON.parse(response)
end

#post(path, payload, **options) ⇒ Hash

Sends a POST request to the given path with the given payload.

Parameters:

  • path (String)
  • payload (Hash)
  • idempotency_key (String)

    (optional) avoid accidentally performing the same operation twice

Returns:

  • (Hash)

    API response

Raises:



89
90
91
92
93
94
95
96
97
98
# File 'lib/synapse_pay_rest/http_client.rb', line 89

def post(path, payload, **options)
  headers = get_headers
  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) }
  p 'RESPONSE:', JSON.parse(response) if @logging
  JSON.parse(response)
end

#update_headers(oauth_key: nil, fingerprint: nil, client_id: nil, client_secret: nil, ip_address: nil, **options) ⇒ void

This method returns an undefined value.

Updates headers.

Parameters:

  • oauth_key (String, void) (defaults to: nil)
  • fingerprint (String, void) (defaults to: nil)
  • client_id (String, void) (defaults to: nil)
  • client_secret (String, void) (defaults to: nil)
  • ip_address (String, void) (defaults to: nil)


70
71
72
73
74
75
76
77
78
# File 'lib/synapse_pay_rest/http_client.rb', line 70

def update_headers(oauth_key: nil, fingerprint: nil, client_id: nil,
                   client_secret: nil, ip_address: nil, **options)
  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
  nil
end