class AvrijAnalyticsUtility::APIConnector
Public Class Methods
Constructor
# 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
Public Instance Methods
Request for API authentication
Attributes¶ ↑
-
authentication_endpoint
- API authentication end point
Examples¶ ↑
- authentication_endpoint
-
given the complete uri : www.company.com/auth
-
the base uri is : www.company.com
-
the authentication endpoint is : auth
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
# File lib/AvrijAnalyticsUtility/APIConnector.rb, line 169 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
Returns the header authentication value including the token
# File lib/AvrijAnalyticsUtility/APIConnector.rb, line 23 def getAuthValue return @auth_value end
Perform API GET request
Attributes¶ ↑
-
apiEndpoint
- API end point -
headerHash
- Hash definition of header options -
queryHash
- Hash definition of query options
Examples¶ ↑
#[apiEndpoint] - Given the complete browser’s uri : www.company.com:8080/api/endpoint1/endpoint1_1 - given the base uri : www.company.com:8080 - then endpoint is : api/endpoint1/endpoint1_1
#[headerHash] - my_header: { # “Key1” => “value1”, # “key2” => “value2” # }
#[headerQuery] - my_query { # “key1” => “value1”, # “key2” => “value2” #}
# File lib/AvrijAnalyticsUtility/APIConnector.rb, line 52 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
Perform API POST request
Attributes¶ ↑
-
apiEndpoint
- API end point -
headerHash
- Hash definition of header options -
queryHash
- Hash definition of query options
Examples¶ ↑
#[apiEndpoint] - Given the complete browser’s uri : www.company.com:8080/api/endpoint1/endpoint1_1 - given the base uri : www.company.com:8080 - then endpoint is : api/endpoint1/endpoint1_1
#[headerHash] - my_header: { # “Key1” => “value1”, # “key2” => “value2” # }
#[headerQuery] - my_query { # “key1” => “value1”, # “key2” => “value2” #}
# File lib/AvrijAnalyticsUtility/APIConnector.rb, line 85 def postRequest(apiEndpoint,headerHash,queryHash) @header_hash = headerHash @query_hash = queryHash request_uri = @base_uri +"/" + apiEndpoint @request_options = {headers: @header_hash, query: @query_hash } result = HTTParty.post(request_uri,@request_options) return result end