class NgrokAPI::HttpClient
Low-level api client for communicating with Ngrok’s HTTP API using HTTP. You should not have to use this class directly, but use the individual clients to make your API calls.
Attributes
Public Class Methods
# File lib/ngrokapi/http_client.rb, line 11 def initialize(api_key:, base_url: 'https://api.ngrok.com') @api_key = api_key @base_url = base_url end
Public Instance Methods
Make a DELETE request to a given URI
@param [string] path URL resource path. @param [boolean] danger determine if we should throw an exception on 404 or not @return [nil]
# File lib/ngrokapi/http_client.rb, line 22 def delete(path, danger: false) uri = get_uri(path) req = Net::HTTP::Delete.new(uri, headers) json_do(uri, req, danger: danger) end
Make a GET request to a given URI with optional data
@param [string] path URL resource path @param [boolean] danger determine if we should throw an exception on 404 or not @param [hash] data hash which will be converted to query parameters or form data @return [json] response body
# File lib/ngrokapi/http_client.rb, line 35 def get(path, danger: false, data: {}) uri = get_uri(path, data: data) req = Net::HTTP::Get.new(uri, headers) json_do(uri, req, danger: danger) end
Make a GET request to list resources
@param [boolean] danger determine if we should throw an exception on 404 or not @param [string] before_id URL resource path @param [integer] limit URL resource path @param [string] path resource path, mutually exclusive with url @param [string] url Full URL of the resource, mutually exclusive with path @param [string] url Full URL of the resource, mutually exclusive with path @return [json] response body
# File lib/ngrokapi/http_client.rb, line 51 def list(danger: false, before_id: nil, limit: nil, path: nil, url: nil) if url get(url) else data = {} data[:before_id] = before_id if before_id data[:limit] = limit if limit get(path, danger: danger, data: data) end end
Make a PATCH request to a given URI with optional data
@param [string] path URL resource path @param [boolean] danger determine if we should throw an exception on 404 or not @param [hash] data hash which will be converted to query parameters or form data @return [json] response body
# File lib/ngrokapi/http_client.rb, line 69 def patch(path, danger: false, data: {}) uri = get_uri(path) req = Net::HTTP::Patch.new(uri, headers_with_json) json_do(uri, req, danger: danger, data: deep_to_h(data).to_json) end
Make a POST request to a given URI with optional data
@param [string] path URL resource path @param [boolean] danger determine if we should throw an exception on 404 or not @param [hash] data hash which will be converted to query parameters or form data @return [json] response body
# File lib/ngrokapi/http_client.rb, line 82 def post(path, danger: false, data: {}) uri = get_uri(path) req = Net::HTTP::Post.new(uri, headers_with_json) json_do(uri, req, danger: danger, data: deep_to_h(data).to_json) end
Make a PUT request to a given URI with optional data
@param [string] path URL resource path @param [boolean] danger determine if we should throw an exception on 404 or not @param [hash] data hash which will be converted to query parameters or form data @return [json] response body
# File lib/ngrokapi/http_client.rb, line 95 def put(path, danger: false, data: {}) uri = get_uri(path) req = Net::HTTP::Put.new(uri, headers_with_json) json_do(uri, req, danger: danger, data: deep_to_h(data).to_json) end
Private Instance Methods
# File lib/ngrokapi/http_client.rb, line 144 def deep_to_h(data) data.to_h.transform_values do |v| (!v.is_a?(Array) && v.methods.include?(:to_h)) ? deep_to_h(v) : v end end
# File lib/ngrokapi/http_client.rb, line 134 def get_uri(path, data: nil) data = (!data.nil? && data != {}) ? "?#{URI.encode_www_form(data)}" : "" URI("#{url(path)}#{data}") end
# File lib/ngrokapi/http_client.rb, line 103 def headers { 'Authorization': "Bearer #{@api_key}", 'Ngrok-Version': '2', } end
# File lib/ngrokapi/http_client.rb, line 110 def headers_with_json headers.merge({ 'Content-Type': 'application/json' }) end
# File lib/ngrokapi/http_client.rb, line 114 def json_do(uri, req, danger: false, data: nil) resp = Net::HTTP.start(uri.hostname, uri.port, req_options(uri)) do |http| http.request(req, data) end if danger if resp.code.to_i == 404 raise NgrokAPI::Errors::NotFoundError.new(response: resp) elsif resp.code.to_i >= 400 raise NgrokAPI::Error.new(response: resp) end end if resp.body && resp.body != '' JSON.parse(resp.body) end end
# File lib/ngrokapi/http_client.rb, line 130 def req_options(uri) { use_ssl: uri.scheme == 'https' } end
# File lib/ngrokapi/http_client.rb, line 139 def url(path) path = path.delete_prefix(@base_url) "#{@base_url}#{path}" end