Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions src/main/scala/us/jubat/jubaql_client/JubaQLClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,14 @@ object JubaQLClient {
}
})

// login if session id is not given
val sessionId = sessionIdParameter match {
case Some(session) =>
session
case _ =>
val serverResponse = getSessionId(hostname, port)
if (serverResponse.isEmpty) {
System.exit(1)
}
serverResponse.get
val serverResponse = sessionIdParameter match {
case Some(session) => getSessionId(hostname, port, session)
case _ => getSessionId(hostname, port)
}
if (serverResponse.isEmpty) {
System.exit(1)
}
val sessionId = serverResponse.get
Console.err.println("Using session id \"%s\"".format(sessionId))

// read from stdin or the given scriptfile
Expand Down Expand Up @@ -104,14 +101,22 @@ object JubaQLClient {
System.exit(0)
}

/** Gets a session id from a JubaQL gateway server.
*
* @return some session id if login was successful
*/
/**
* Gets a session id from a JubaQL gateway server.
*
* @return some session id if login was successful
*/
def getSessionId(hostname: String, port: Int,
err: PrintStream = Console.err): Option[String] = {
sessionId: String = null, err: PrintStream = Console.err): Option[String] = {
val url = :/(hostname, port) / "login"
val req = Http(url.POST OK as.String)
val req = sessionId match {
case null => Http(url.POST OK as.String)
case _ => {
val payloadData = ("session_id" -> sessionId)
val json: String = compact(render(payloadData))
Http(url << json OK as.String)
}
}
req.either.apply() match {
case Left(error) =>
// if request fails or is non-2xx, print error
Expand Down