Skip to content

Commit 737f66b

Browse files
committed
improved usage of optional type
1 parent 30c660d commit 737f66b

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

app/controllers/InstanceRegistryController.scala

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ import akka.stream.Materializer
2727
import play.api.libs.streams.ActorFlow
2828
import actors.{ClientSocketActor, PublishSocketMessageActor}
2929
import play.api.mvc._
30-
import scala.concurrent.ExecutionContext
30+
import scala.concurrent.{Future, ExecutionContext}
3131
import authorization.AuthProvider
3232
import authorization.AuthAction
33-
import play.api.libs.json.Json
34-
33+
import play.api.libs.json.{Json, JsValue}
3534

3635

3736
trait MyExecutionContext extends ExecutionContext
@@ -205,23 +204,30 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
205204

206205
def authentication(): Action[AnyContent] = Action.async {
207206
request =>
208-
val json = request.body.asJson.get
209-
210-
val username = (json \ "username").as[String]
211-
val password = (json \ "password").as[String]
212-
213-
ws.url(instanceRegistryUri + "/users" + "/authenticate")
214-
.withAuth(username, password, WSAuthScheme.BASIC)
215-
.withHttpHeaders(("Delphi-Authorization", s"${AuthProvider.generateJwt()}"))
216-
.post("")
217-
.map { response =>
218-
if (response.status == 200) {
219-
Ok(Json.obj("token" -> response.body, "refreshToken" -> ""))
220-
} else {
221-
new Status(response.status)
207+
//val json = request.body.asJson.get
208+
209+
210+
val jsonBody: Option[JsValue] = request.body.asJson
211+
212+
jsonBody.map { json =>
213+
214+
val username = (json \ "username").as[String]
215+
val password = (json \ "password").as[String]
216+
217+
ws.url(instanceRegistryUri + "/users" + "/authenticate")
218+
.withAuth(username, password, WSAuthScheme.BASIC)
219+
.withHttpHeaders(("Delphi-Authorization", s"${AuthProvider.generateJwt()}"))
220+
.post("")
221+
.map { response =>
222+
if (response.status == 200) {
223+
Ok(Json.obj("token" -> response.body, "refreshToken" -> ""))
224+
} else {
225+
new Status(response.status)
226+
}
222227
}
223-
}(myExecutionContext)
224-
}
228+
}.getOrElse{ Future(BadRequest("Invalid body"))}
229+
230+
}
225231

226232
/**
227233
* This function is used to add a label to specific instance.

0 commit comments

Comments
 (0)