Module: BlueFactory::Net Private

Defined in:
lib/blue_factory/tasks/support.rb

Overview

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

HTTP request helpers.

Defined Under Namespace

Classes: ResponseError

Class Method Summary collapse

Class Method Details

.get_request(server, method = nil, params = nil, auth: 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.

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/blue_factory/tasks/support.rb', line 26

def self.get_request(server, method = nil, params = nil, auth: nil)
  headers = {}
  headers['Authorization'] = "Bearer #{auth}" if auth

  url = method ? URI("#{server}/xrpc/#{method}") : URI(server)

  if params && !params.empty?
    url.query = URI.encode_www_form(params)
  end

  response = ::Net::HTTP.get_response(url, headers)
  raise ResponseError.new(response, "Invalid response: #{response.code} #{response.body}") if response.code.to_i / 100 != 2

  JSON.parse(response.body)
end

.post_request(server, method, data, auth: nil, content_type: "application/json") ⇒ 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.

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/blue_factory/tasks/support.rb', line 42

def self.post_request(server, method, data, auth: nil, content_type: "application/json")
  headers = {}
  headers['Content-Type'] = content_type
  headers['Authorization'] = "Bearer #{auth}" if auth

  body = data.is_a?(String) ? data : data.to_json

  response = ::Net::HTTP.post(URI("#{server}/xrpc/#{method}"), body, headers)
  raise ResponseError.new(response, "Invalid response: #{response.code} #{response.body}") if response.code.to_i / 100 != 2

  JSON.parse(response.body)
end