class RestPki::RestPkiClient

Public Class Methods

new(endpoint_url, access_token) click to toggle source
# File lib/rest_pki/restpki_client.rb, line 8
def initialize(endpoint_url, access_token)
    @endpoint_url = endpoint_url
    @access_token = access_token
end

Public Instance Methods

get(url, object_model) click to toggle source
# File lib/rest_pki/restpki_client.rb, line 13
def get(url, object_model)
    verb = 'GET'
    params = get_rest_params(verb, url)

    begin
        response = RestClient::Request.execute params
    rescue RestClient::Exception => ex
        response = RestPkiObject.convert({
            :code => ex.http_code,
            :body => ex.response
        }, 'response_model')
    rescue Exception => ex
        raise RestUnreachableError.new(verb, url, ex.message)
    end
    check_response(verb, url, response)

    RestPkiObject.convert(MultiJson.decode(response.body), object_model)
end
get_authentication() click to toggle source
# File lib/rest_pki/restpki_client.rb, line 51
def get_authentication
    Authentication.new(self)
end
post(url, data, object_model) click to toggle source
# File lib/rest_pki/restpki_client.rb, line 32
def post(url, data, object_model)
    verb = 'POST'
    params = get_rest_params(verb, url, data)

    begin
        response = RestClient::Request.execute params
    rescue RestClient::Exception => ex
        response = RestPkiObject.convert({
            :code => ex.http_code,
            :body => ex.response
        }, 'response_model')
    rescue Exception => ex
        raise RestUnreachableError.new(verb, url, ex.message)
    end
    check_response(verb, url, response)

    RestPkiObject.convert(MultiJson.decode(response.body), object_model)
end

Private Instance Methods

check_response(verb, url, http_response) click to toggle source
# File lib/rest_pki/restpki_client.rb, line 72
def check_response(verb, url, http_response)
    status_code = http_response.code
    if status_code < 200 || status_code > 299
        ex = nil
        begin
            response = MultiJson.decode http_response.body
            if status_code == 422 && response['code'].to_s.blank?
                if response['code'] == 'ValidationError'
                    vr = ValidationResults.new(response['validationResults'])
                    ex = ValidationError.new(verb, url, vr)
                else
                    ex = RestPkiError.new(verb, url, response['code'], response['detail'])
                end
            else
                ex = RestError.new(verb, url, status_code, response['message'])
            end
        rescue => e
            ex = RestError.new(verb, url, status_code)
        end
        raise ex
    end
end
get_rest_params(method, url, params=nil) click to toggle source
# File lib/rest_pki/restpki_client.rb, line 56
def get_rest_params(method, url, params=nil)
    {
        method: method,
        url: @endpoint_url + url,
        payload: params ? MultiJson.encode(params) : nil,
        open_timeout: 30,
        timeout: 90,
        headers: {
            'Content-Type': 'application/json; charset=utf8',
            Accept: 'application/json',
            Authorization: "Bearer #{@access_token}",
            'X-RestPki-Client': "Ruby #{RestPki::VERSION}"
        }
    }
end