I consume some data from a singleRequest with a custom body, and i tried to serialize it to a custom case class with Unmarshall but it doesn't work; the body response is like:
{
"teams": [
{
"id": "21312",
"name": "team1",
"color": "#4169E1",
"avatar": null,
"members": [
{
"user": {
"id": 12737636,
"username": "user2",
"email": "[email protected]",
"color": "#827718",
"profilePicture": null,
"initials": "U2",
"role": 4,
"custom_role": null,
"last_active": "1613419183004",
"date_joined": "1612542647748",
"date_invited": "1612542548880"
},
"invited_by": {
"id": 68732,
"username": "user1",
"color": "#f8306d",
"email": "[email protected]",
"initials": "U1",
"profilePicture": "https://attachments.clickup.com/profilePictures/image.jpg"
},
"can_see_time_spent": true,
"can_see_time_estimated": true,
"can_see_points_estimated": true,
"can_edit_tags": false
},
{
"user": {
"id": 12715129,
"username": "user3",
"email": "[email protected]",
"color": "#04a9f4",
"profilePicture": null,
"initials": "IM",
"role": 4,
"custom_role": null,
"last_active": "1613483780281",
"date_joined": "1611598611255",
"date_invited": "1611597360856"
},
"invited_by": {
"id": 4371505,
"username": "user34",
"color": "#f8306d",
"email": "[email protected]",
"initials": "U34",
"profilePicture": "https://attachments.clickup.com/profilePictures/image.jpg"
},
"can_see_time_spent": true,
"can_see_time_estimated": true,
"can_see_points_estimated": true,
"can_edit_tags": false
}
]
}
]
}
Code to fetch the data:
val responseFuture = Http()
.singleRequest(HttpRequest( method = GET ,uri = "https://apiRest/api/v2/team" )
.withHeaders(RawHeader("authorization","xxxxxxxxxxxx")))
I have this case class to serialize it:
case class Invited_By (id: Long, username:Option[String] , color : Option[String], email: Option[String], initials : Option[String], profilePicture: Option[String])
case class User ( id: Long , username: Option[String] , email: Option[String] , color: Option[String] , profilePicture: Option[String] , initials: Option[String] , role: Option[Int], custom_role: Option[String] , last_active: Option[String] , date_joined: Option[String] , date_invited: Option[String] )
case class Member (user: User, invited_By: Invited_By, can_see_time_spent: Boolean, can_see_time_estimated:Boolean, can_see_points_estimated:Boolean, can_edit_tags:Boolean)
case class Team (id : String, name: Option[String], color: Option[String], avatar: Option[String], members: Seq[Member] )
case class Teams( teams: Seq[Team] )
i tried with this; this didn't work :
import play.api.libs.json._
import play.api.libs.functional.syntax._
implicit val TeamsReads: Reads[Teams] = Json.reads[Teams]
val responseFuture = Http()
.singleRequest(HttpRequest( method = GET ,uri = "https://apiRest/api/v2/team" )
.withHeaders(RawHeader("authorization","XXXXXXXXXXXX")))
.flatMap{
res =>
Unmarshal(res).to[String]
.map {
data =>
val jsValue:JsValue = Json.parse(data)
val result: JsResult[Teams] = jsValue.validate[Teams]
result
}
}
is there a better way to do it ??
note: i found akka-http-spray-json , but i don't know how to implement it