Class: Medlink::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/medlink/client.rb

Instance Method Summary collapse

Instance Method Details

#post(url, params = {}) ⇒ Object

Raises:



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/medlink/client.rb', line 4

def post(url, params = {})
  response = faraday_client.post do |req|
    req.url api_url(url)

    req.headers['Authorization']    = "Token token=\"#{token}\""
    req.headers['Content-Type']     = "application/json"
    req.headers['Accept']           = "application/json"

    req.body = JSON.generate(params)
  end

  raise UnauthorizedError, "Token '#{token}' is not valid" if response.status == 401

  json = JSON.parse response.body

  if json.is_a? Hash and json.has_key?("errors")
    error = json["errors"].first
    raise(Error, "Error #{error["code"]}: #{error["detail"]}")
  end

  json

end