Class: HandleSystem::HttpClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/handle_system/http_client.rb

Overview

HTTP utility class for Handle client

Provides some convenience methods so we don’t have to set headers and options with each request, checks for and throws errors reported by the handle server, and does the work of acquiring a session

Author:

  • David Walker

Instance Method Summary collapse

Constructor Details

#initialize(server, hs_admin, priv_key_path, pass_phrase = nil) ⇒ HttpClient

New Handle HTTP client

Parameters:

  • server (String)

    ip_address:port, e.g., 123.456.78.9:8000

  • hs_admin (String)

    handle administrator

  • priv_key_path (String)

    file path to private key

  • pass_phrase (String) (defaults to: nil)
    optional

    pass phrase for private key



28
29
30
31
32
33
34
# File 'lib/handle_system/http_client.rb', line 28

def initialize(server, hs_admin, priv_key_path, pass_phrase = nil)
  @hs_admin = hs_admin
  @private_key_path = priv_key_path
  @pass_phrase = pass_phrase
  @base_url = 'https://' + server + '/api'
  @session_id = initialize_session
end

Instance Method Details

#delete(path) ⇒ JSON

Send a delete request

Parameters:

  • path (String)

    relative path from /api end-point

Returns:

  • (JSON)

    parsed json response from handle server

Raises:

  • HandleSystem::Error if we got an error from the server



73
74
75
76
77
# File 'lib/handle_system/http_client.rb', line 73

def delete(path)
  url = @base_url + path
  response = self.class.delete(url, options)
  process_response(url, response)
end

#get(path) ⇒ JSON

Send a get request

Parameters:

  • path (String)

    relative path from /api end-point

Returns:

  • (JSON)

    parsed json response from handle server

Raises:

  • HandleSystem::Error if we got an error from the server



44
45
46
47
48
# File 'lib/handle_system/http_client.rb', line 44

def get(path)
  url = @base_url + path
  response = self.class.get(url, options)
  process_response(url, response)
end

#put(path, body) ⇒ JSON

Send a put request

Parameters:

  • path (String)

    relative path from /api end-point

  • body (String)

    json data object

Returns:

  • (JSON)

    parsed json response from handle server

Raises:

  • HandleSystem::Error if we got an error from the server



59
60
61
62
63
# File 'lib/handle_system/http_client.rb', line 59

def put(path, body)
  url = @base_url + path
  response = self.class.put(url, body: body, **options)
  process_response(url, response)
end