Class: Invoance::Http Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Low-level HTTP transport using stdlib net/http. Zero external dependencies. Network-level failures are raised as Error subclasses (NetworkError / TimeoutError) so a single rescue is exhaustive.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Http

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Http.

Parameters:



21
22
23
24
25
26
27
28
29
# File 'lib/invoance/http.rb', line 21

def initialize(config)
  @config = config
  @base_headers = {
    "Authorization" => "Bearer #{config.api_key}",
    "Accept" => "application/json",
    "Content-Type" => "application/json",
    "User-Agent" => "invoance-ruby/#{Invoance::VERSION}"
  }.merge(config.extra_headers)
end

Instance Method Details

#delete(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

DELETE returning parsed JSON.



63
64
65
66
67
68
# File 'lib/invoance/http.rb', line 63

def delete(path)
  uri = build_uri(path)
  ctx = { method: "DELETE", path: path }
  resp = do_request(Net::HTTP::Delete.new(uri), uri, ctx, headers: @base_headers)
  handle(resp, ctx)
end

#get(path, params = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

GET returning parsed JSON (Hash with string keys).



32
33
34
35
36
37
# File 'lib/invoance/http.rb', line 32

def get(path, params = nil)
  uri = build_uri(path, params)
  ctx = { method: "GET", path: path }
  resp = do_request(Net::HTTP::Get.new(uri), uri, ctx, headers: @base_headers)
  handle(resp, ctx)
end

#get_bytes(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

GET returning raw bytes (binary String). Sets Accept: application/octet-stream and drops Content-Type.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/invoance/http.rb', line 72

def get_bytes(path)
  uri = build_uri(path)
  ctx = { method: "GET", path: path }
  headers = @base_headers.dup
  headers["Accept"] = "application/octet-stream"
  headers.delete("Content-Type")
  resp = do_request(Net::HTTP::Get.new(uri), uri, ctx, headers: headers)

  unless success?(resp)
    body = parse_json_body(resp)
    Invoance.throw_for_status(resp.code.to_i, body,
                              request_context: ctx,
                              retry_after_seconds: parse_retry_after(resp["retry-after"]))
  end

  body = resp.body || ""
  body.dup.force_encoding(Encoding::BINARY)
end

#get_raw(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

GET returning the decoded JSON value untyped (used by attestations raw).



92
93
94
# File 'lib/invoance/http.rb', line 92

def get_raw(path)
  get(path)
end

#post(path, body = nil, idempotency_key = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

POST returning parsed JSON. Sends Idempotency-Key when available.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/invoance/http.rb', line 40

def post(path, body = nil, idempotency_key = nil)
  uri = build_uri(path)
  ctx = { method: "POST", path: path }
  headers = @base_headers.dup
  idem = idempotency_key || @config.idempotency_key
  headers["Idempotency-Key"] = idem if idem
  req = Net::HTTP::Post.new(uri)
  req.body = JSON.generate(body) unless body.nil?
  resp = do_request(req, uri, ctx, headers: headers)
  handle(resp, ctx)
end

#put(path, body = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

PUT returning parsed JSON.



53
54
55
56
57
58
59
60
# File 'lib/invoance/http.rb', line 53

def put(path, body = nil)
  uri = build_uri(path)
  ctx = { method: "PUT", path: path }
  req = Net::HTTP::Put.new(uri)
  req.body = JSON.generate(body) unless body.nil?
  resp = do_request(req, uri, ctx, headers: @base_headers)
  handle(resp, ctx)
end