Class: AvrijAnalyticsUtility::APIConnector

Inherits:
Object
  • Object
show all
Defined in:
lib/AvrijAnalyticsUtility/APIConnector.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_uri) ⇒ APIConnector

Constructor



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/AvrijAnalyticsUtility/APIConnector.rb', line 8

def initialize(base_uri)
  @base_uri         = base_uri
  @username         = nil
  @password         = nil
  @auth_token       = nil
  @auth_value       = nil
  @header_hash      = nil
  @query_hash       = nil
  @body_hash        = nil
  @request_options  = nil
end

Instance Method Details

#authenticate(authentication_endpoint) ⇒ Object

Request for API authentication

Attributes

  • authentication_endpoint - API authentication end point

Examples

Steps for Authentication

myConnector = APIConnector.new(base_uri) auth_Header =

"Authorization" => "Bearer undefined"

auth_body =

"username" => "myusername",
"password" => "mypassword"

myConnector.setHeader(auth_Header) myConnector.setBody(auth_body) if myConnector.authenticate(auth_endpoint)

<todo>

end



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/AvrijAnalyticsUtility/APIConnector.rb', line 126

def authenticate(authentication_endpoint)
    authentication_uri = @base_uri + "/" + authentication_endpoint
    authentication_options = {headers: @header_hash, body: @body_hash}
    authentication_result = HTTParty.post(authentication_uri, authentication_options)
    if authentication_result.code == 200 # success
        @auth_token = authentication_result.parsed_response["token"]
        @auth_value = "Bearer " + @auth_token
        @header_hash = {
           "authorization" => @auth_value
        }
        result = true
    else
        result = false          
    end
    return result
end

#getRequest(apiEndpoint, headerHash, queryHash) ⇒ Object

Perform API request

Attributes

  • apiEndpoint - API end point

  • headerHash - Hash definition of header options

  • queryHash - Hash definition of query options

Examples

apiEndpoint
headerHash
  • my_header:

    "Key1" => "value1",
    "key2" => "value2"
    

headerQuery
  • my_query

    "key1" => "value1",
    "key2" => "value2"
    



44
45
46
47
48
49
50
51
# File 'lib/AvrijAnalyticsUtility/APIConnector.rb', line 44

def getRequest(apiEndpoint,headerHash,queryHash)
    @header_hash = headerHash
    @query_hash  = queryHash
    request_uri = @base_uri +"/" + apiEndpoint
    @request_options = {headers: @header_hash, query: @query_hash }
    result = HTTParty.get(request_uri,@request_options)
    return result
end

#setBody(bodyHash) ⇒ Object

Set request body hash

Attributes

  • bodyHash - Hash definition of body options

Examples

bodyHash
  • my_body:

    "Key1" => "value1",
    "key2" => "value2"
    



94
95
96
# File 'lib/AvrijAnalyticsUtility/APIConnector.rb', line 94

def setBody(bodyHash)
    @body_hash = bodyHash
end

#setHeader(headerHash) ⇒ Object

Set request header hash

Attributes

  • headerHash - Hash definition of header options

Examples

headerHash
  • my_header:

    "Key1" => "value1",
    "key2" => "value2"
    



64
65
66
# File 'lib/AvrijAnalyticsUtility/APIConnector.rb', line 64

def setHeader(headerHash)
    @header_hash = headerHash
end

#setQuery(queryHash) ⇒ Object

Set request query hash

Attributes

  • queryHash - Hash definition of query options

Examples

queryHash
  • my_query:

    "Key1" => "value1",
    "key2" => "value2"
    



79
80
81
# File 'lib/AvrijAnalyticsUtility/APIConnector.rb', line 79

def setQuery(queryHash)
    @query_hash = queryHash
end