Class: PritunlApiClient::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/pritunl_api_client/api.rb

Overview

Low-level communication to api server

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, api_token:, api_secret:, verify_ssl: true) ⇒ Api

Returns a new instance of Api.

Parameters:

  • base_url (String)

    Full URL to a running Pritunl server (include the “https://”)

  • api_token (String)

    pi_token [String

  • api_secret (String)
  • verify_ssl (Boolean) (defaults to: true)

    Whether or not to verify SSL certificate]



15
16
17
18
19
# File 'lib/pritunl_api_client/api.rb', line 15

def initialize( base_url:, api_token:, api_secret:, verify_ssl: true )
  @api_token = api_token
  @api_secret = api_secret
  @client = RestClient::Resource.new( base_url, verify_ssl: verify_ssl )
end

Instance Method Details

#delete(path, params = nil) ⇒ Hash, ...

Send delete request to api server

Parameters:

  • path (String)

    URL path of api

  • params (Hash, NilClass) (defaults to: nil)

    Api parameters

Returns:

  • (Hash, Array, String)

    Response from api server



60
61
62
63
64
65
# File 'lib/pritunl_api_client/api.rb', line 60

def delete( path, params = nil )
  parameters = { params: params }
  parameters.merge!( common_headers )
  parameters.merge!( generate_auth_headers( path: path, params: params, method: 'DELETE' ) )
  parse_data @client[path].delete( parameters )
end

#get(path, params = nil) ⇒ Hash, ...

Send get request to api server

Parameters:

  • path (String)

    URL path of api

  • params (Hash, NilClass) (defaults to: nil)

    Api parameters

Returns:

  • (Hash, Array, String)

    Response from api server



37
38
39
40
41
42
# File 'lib/pritunl_api_client/api.rb', line 37

def get( path, params = nil )
  parameters = { params: params }
  parameters.merge!( common_headers )
  parameters.merge!( generate_auth_headers( path: path, params: params, method: 'GET' ) )
  parse_data @client[path].get( parameters )
end

#head(path, params = nil) ⇒ Hash, ...

Send head request to api server

Parameters:

  • path (String)

    URL path of api

  • params (Hash, NilClass) (defaults to: nil)

    Api parameters

Returns:

  • (Hash, Array, String)

    Response from api server



72
73
74
75
76
# File 'lib/pritunl_api_client/api.rb', line 72

def head( path, params = nil )
  headers = common_headers.merge( generate_auth_headers( path: path, params: params, method: 'HEAD' ) )
  params = JSON.generate( params, space: ' ' ) if params
  parse_data @client[path].head( params, headers )
end

#post(path, params = nil) ⇒ Hash, ...

Send post request to api server

Parameters:

  • path (String)

    URL path of api

  • params (Hash, NilClass) (defaults to: nil)

    Api parameters

Returns:

  • (Hash, Array, String)

    Response from api server



26
27
28
29
30
# File 'lib/pritunl_api_client/api.rb', line 26

def post( path, params = nil )
  headers = common_headers.merge( generate_auth_headers( path: path, params: params, method: 'POST' ) )
  params = JSON.generate( params, space: ' ' ) if params
  parse_data @client[path].post( params, headers )
end

#put(path, params = nil) ⇒ Hash, ...

Send put request to api server

Parameters:

  • path (String)

    URL path of api

  • params (Hash, NilClass) (defaults to: nil)

    Api parameters

Returns:

  • (Hash, Array, String)

    Response from api server



49
50
51
52
53
# File 'lib/pritunl_api_client/api.rb', line 49

def put( path, params = nil )
  headers = common_headers.merge( generate_auth_headers( path: path, params: params, method: 'PUT' ) )
  params = JSON.generate( params, space: ' ' ) if params
  parse_data @client[path].put( params, headers )
end