Skip to content

Commit

Permalink
Add support of v1 to Hoper class
Browse files Browse the repository at this point in the history
  • Loading branch information
dolfinus committed May 5, 2019
1 parent f974d24 commit e5493e9
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 deletions src/org/camunda/latera/bss/connectors/Hoper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,65 @@ package org.camunda.latera.bss.connectors

import org.camunda.latera.bss.http.HTTPRestProcessor
import org.camunda.latera.bss.logging.SimpleLogger
import org.camunda.latera.bss.utils.Base64Converter
import org.camunda.bpm.engine.delegate.DelegateExecution

class Hoper {
HTTPRestProcessor processor
LinkedHashMap headers
DelegateExecution execution
SimpleLogger logger
String url
private String user
private String password
String version
LinkedHashMap auth
HTTPRestProcessor http
DelegateExecution execution
SimpleLogger logger

Hoper(DelegateExecution execution) {
this.execution = execution
this.logger = new SimpleLogger(execution)

this.url = execution.getVariable("hoperUrl")
this.version = execution.getVariable("hoperVersion") ?: '2'
this.auth = [
login: execution.getVariable("hydraUser"),
password: execution.getVariable("hydraPassword")
this.user = execution.getVariable("hydraUser")
this.password = execution.getVariable("hydraPassword")
this.http = new HTTPRestProcessor(baseUrl : this.url,
execution : execution)
}

private def authToken() {
def auth = [
session: [
login : user,
password : password
]
]
this.processor = new HTTPRestProcessor(baseUrl : this.url,
execution : execution)
return http.sendRequest('get',
path : "/rest/v${this.version}/login",
body : auth,
supressRequestBodyLog : true,
supressResponseBodyLog : true)?.session?.token
}

private def authBasic() {
def auth = "${user}:${password}"
return Base64Converter.to(auth)
}

private def authHeader() {
if (version.toString() == '1') {
return ['Authorization': "Basic ${authBasic}"]
}
if (version.toString() == '2') {
return ['Authorization': "Token token=\"${authToken}\""]
}
return []
}

def sendRequest(LinkedHashMap input, String method = 'get') {
def token = this.processor.sendRequest(path: "/rest/v${this.version}/login", body: [session: this.auth], supressRequestBodyLog: true, supressResponseBodyLog: true, 'get')?.session?.token
def headers = [
'Authorization': "Token token=\"${token}\""
]
if (!input.headers) {
input.headers = [:]
}
input.headers += headers
input.path = "/rest/v${this.version}/${input.path}".toString()
return this.processor.sendRequest(input, method)
input.headers += authHeader
input.path = "/rest/v${version}/${input.path}".toString()
return http.sendRequest(input, method)
}
}

0 comments on commit e5493e9

Please sign in to comment.