Module: Knock::Client

Includes:
Kernel
Included in:
BulkOperations, Messages, Objects, Preferences, Users, Workflows
Defined in:
lib/knock/client.rb

Overview

A Net::HTTP based API client for interacting with the Knock API

Instance Method Summary collapse

Instance Method Details

#clientObject



6
7
8
9
10
11
12
13
# File 'lib/knock/client.rb', line 6

def client
  return @client if defined?(@client)

  @client = Net::HTTP.new(Knock::API_HOSTNAME, 443)
  @client.use_ssl = true

  @client
end

#delete_request(path:, auth: false, params: {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/knock/client.rb', line 46

def delete_request(path:, auth: false, params: {})
  uri = URI(path)
  uri.query = URI.encode_www_form(params) if params

  request = Net::HTTP::Delete.new(
    uri.to_s,
    "Content-Type" => "application/json"
  )

  request["Authorization"] = "Bearer #{Knock.key!}" if auth
  request["User-Agent"] = user_agent
  request
end

#execute_request(request:) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/knock/client.rb', line 15

def execute_request(request:)
  response = client.request(request)

  http_status = response.code.to_i
  handle_error_response(response: response) if http_status >= 400

  JSON.parse(response.body)
end

#get_request(path:, auth: false, params: {}, access_token: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/knock/client.rb', line 24

def get_request(path:, auth: false, params: {}, access_token: nil)
  uri = URI(path)
  uri.query = URI.encode_www_form(params) if params

  request = Net::HTTP::Get.new(
    uri.to_s,
    "Content-Type" => "application/json"
  )

  request["Authorization"] = "Bearer #{access_token || Knock.key!}" if auth
  request["User-Agent"] = user_agent
  request
end

#handle_error_response(response:) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/knock/client.rb', line 72

def handle_error_response(response:)
  http_status = response.code.to_i
  json = JSON.parse(response.body)

  case http_status
  when 400
    raise InvalidRequestError.new(
      message: json["message"],
      http_status: http_status,
      request_id: response["x-request-id"]
    )
  when 401
    raise AuthenticationError.new(
      message: json["message"],
      http_status: http_status,
      request_id: response["x-request-id"]
    )
  when 404
    raise APIError.new(
      message: json["message"],
      http_status: http_status,
      request_id: response["x-request-id"]
    )
  when 422
    message = json["message"]
    errors = extract_error(json["errors"]) if json["errors"]
    message += " (#{errors})" if errors

    raise InvalidRequestError.new(
      message: message,
      http_status: http_status,
      request_id: response["x-request-id"]
    )
  end
end

#post_request(path:, auth: false, idempotency_key: nil, body: nil) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/knock/client.rb', line 38

def post_request(path:, auth: false, idempotency_key: nil, body: nil)
  request = Net::HTTP::Post.new(path, "Content-Type" => "application/json")
  request.body = body.to_json if body
  request["Authorization"] = "Bearer #{Knock.key!}" if auth
  request["User-Agent"] = user_agent
  request
end

#put_request(path:, auth: false, idempotency_key: nil, body: nil) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/knock/client.rb', line 60

def put_request(path:, auth: false, idempotency_key: nil, body: nil)
  request = Net::HTTP::Put.new(path, "Content-Type" => "application/json")
  request.body = body.to_json if body
  request["Authorization"] = "Bearer #{Knock.key!}" if auth
  request["User-Agent"] = user_agent
  request
end

#user_agentObject



68
69
70
# File 'lib/knock/client.rb', line 68

def user_agent
  "Knock Ruby - v#{Knock::VERSION}"
end