Class: Sidemash::SDK::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/sidemash/sdk/http.rb

Class Method Summary collapse

Class Method Details

._call(path, method, body, auth, query_string, headers) ⇒ 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
# File 'lib/sidemash/sdk/http.rb', line 86

def self._call(path, method, body, auth,  query_string, headers)
  body_str = body.nil? ? nil : body.to_json
  url = Http._host + path + query_string
  signed_headers = Http._compute_signed_headers(body, headers.nil? ? {} : headers, auth)
  sdm_request = Sidemash::Sdk::HttpRequest.new(signed_headers,
                                               method,
                                               path,
                                               query_string,
                                               body_str.nil? ? nil : Http._sha256(body_str))
  h = signed_headers
  h['X-Sdm-SignedHeaders'] = signed_headers.keys.select { |key| key }.join(', ')
  h['X-Sdm-Nonce'] = sdm_request.nonce.to_s
  h['X-Sdm-Signature'] = 'SHA512 ' + Http._sign(sdm_request.to_message, auth.private_key)
  response = Http._make_request(method, url, body_str, h)
  if response.code == 204
    nil
  else
    response_json = JSON.parse(response.body)
    if response.code < 300
      response_json
    else
      raise Sidemash::Sdk::HttpCallError.new(response.code, response_json)
    end
  end
end

._compute_signed_headers(body, headers, auth) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/sidemash/sdk/http.rb', line 56

def self._compute_signed_headers(body, headers, auth)
  signed_headers =
      {'Accept' =>  'application/json', 'User-Agent' => 'Sdk Ruby v1.0', 'Authorization': 'Bearer ' + auth.token},
      merge(headers).
          merge(body.nil? ? {} : {'Content-Type' => 'application/json'})
  signed_headers
end

._hostObject



39
40
41
# File 'lib/sidemash/sdk/http.rb', line 39

def self._host
  'http://dev-api.sidemash.io'
end

._make_request(method, url, body, headers) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sidemash/sdk/http.rb', line 65

def self._make_request(method, url, body, headers)
  response = nil
  case method
  when 'POST'
    response = HTTParty.post(url, body: body, headers: headers)
  when 'GET'
    response = HTTParty.get(url, body: body, headers: headers)
  when 'PATCH'
    response = HTTParty.patch(url, body: body, headers: headers)
  when 'PUT'
    response = HTTParty.put(url, body: body, headers: headers)
  when 'DELETE'
    response = HTTParty.delete(url, headers: headers)
  else
    raise 'Unsupported HTTP Method: ' + method
  end
  return response
end

._sha256(message) ⇒ Object



50
51
52
53
# File 'lib/sidemash/sdk/http.rb', line 50

def self._sha256(message)
  hash = OpenSSL::Digest.digest('sha256', message)
  Base64.strict_encode64(hash)
end

._sign(message, private_key) ⇒ Object



44
45
46
47
# File 'lib/sidemash/sdk/http.rb', line 44

def self._sign(message, private_key)
  signature = OpenSSL::HMAC.digest('sha512', private_key, message)
  Base64.strict_encode64(signature)
end

.delete(path:, body:, query_string:, headers:, auth:) ⇒ Object



28
29
30
# File 'lib/sidemash/sdk/http.rb', line 28

def self.delete(path:, body:, query_string:, headers:, auth:)
  Http._call(path, 'DELETE', body, auth, query_string, headers)
end

.get(path:, query_string:, headers:, auth:) ⇒ Object



16
17
18
# File 'lib/sidemash/sdk/http.rb', line 16

def self.get(path:, query_string:, headers:, auth:)
  Http._call(path,  'GET', nil, auth, query_string, headers)
end

.list(path:, query_string:, headers:, auth:) ⇒ Object



12
13
14
# File 'lib/sidemash/sdk/http.rb', line 12

def self.list(path:, query_string:, headers:, auth:)
  Http._call(path,  'GET', nil, auth, query_string, headers)
end

.patch(path:, body:, query_string:, headers:, auth:) ⇒ Object



24
25
26
# File 'lib/sidemash/sdk/http.rb', line 24

def self.patch(path:, body:, query_string:, headers:, auth:)
  Http._call(path, 'PATCH', body, auth, query_string, headers)
end

.post(path:, body:, query_string:, headers:, auth:) ⇒ Object



8
9
10
# File 'lib/sidemash/sdk/http.rb', line 8

def self.post(path:, body:, query_string:, headers:, auth:)
  Http._call(path, 'POST', body, auth, query_string, headers)
end

.put(path:, body:, query_string:, headers:, auth:) ⇒ Object



20
21
22
# File 'lib/sidemash/sdk/http.rb', line 20

def self.put(path: , body:, query_string:, headers:, auth:)
  Http._call(path, 'PUT', body, auth, query_string, headers)
end

.versionObject



34
35
36
# File 'lib/sidemash/sdk/http.rb', line 34

def self.version
  "V1.0"
end