[akka-user] Re: Akka HTTP Client Unmarshalling - different success and error responses

2017-08-22 Thread Gary Struthers

>
> I use Either but I unmarshal all text to Left and JSON to Right


def mapPlain(entity: HttpEntity): Future[Left[String, Nothing]] = {
  Unmarshal(entity).to[String].map(Left(_))
}

def mapChecking(entity: HttpEntity): Future[Right[String, AnyRef]] = {
  Unmarshal(entity).to[CheckingAccountBalances[BigDecimal]].map(Right(_))
} 

  def typedResponse(mapLeft: (HttpEntity) => Future[Left[String, Nothing]],
mapRight: (HttpEntity) => Future[Right[String, AnyRef]])(response: 
HttpResponse)
(implicit ec: ExecutionContext, logger: LoggingAdapter, materializer: 
Materializer):
Future[Either[String, AnyRef]] = {
  response.status match {
case OK => {
  val st = response.entity.contentType.mediaType.subType
  st match {
case "json"  => mapRight(response.entity)
case "plain" => mapLeft(response.entity)
  }
}
case BadRequest => Future.successful(Left(s"FAIL bad 
request:${response.status}"))
case _ => Unmarshal(response.entity).to[String].flatMap { entity =>
  val error = s"FAIL ${response.status} $entity"
  logger.error(error)
  Unmarshal(error).to[String].map(Left(_))
}
  }
  }

Gary
https://garyaiki.github.io/dendrites/HttpJson.html

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


[akka-user] Re: Akka HTTP Client Unmarshalling - different success and error responses

2017-08-22 Thread Richard Rodseth
Answer appears to be yes. Here's what my for comprehension looks like, and
the order of the DTOs in the Either doesn't matter.

val responseFuture = for {

  requestEntity <- Marshal(userAttributes).to

  val request = HttpRequest(method = HttpMethods.POST, uri = addUserURL,
headers = headers, entity = requestEntity)

  response <- http.singleRequest(request)

  strict <- response.toStrict(1 second)

  unmarshalled <- Unmarshal(strict).to[Either[AddUserResponseDTO,
AddUserErrorDTO]]

} yield (response, unmarshalled)

On Tue, Aug 22, 2017 at 1:00 PM, Richard Rodseth  wrote:

> Spoke too soon
>
> Exception in thread "main" akka.http.scaladsl.unmarshalling.Unmarshaller$
> EitherUnmarshallingException: Failed to unmarshal Either[AddUserErrorDTO,
> AddUserResponseDTO] (attempted AddUserResponseDTO first). Right failure:
> Object is missing required member 'id', Left failure: Substream Source
> cannot be materialized more than once
>
> Do I need to place a toStrict somewhere in my for comprehension?
>
>
>
> On Tue, Aug 22, 2017 at 12:28 PM, Richard Rodseth 
> wrote:
>
>> Having some success with Either unmarshaller. I don't think it's
>> documented, but here's a test:
>>
>> https://github.com/akka/akka-http/blob/master/akka-http-test
>> s/src/test/scala/akka/http/scaladsl/unmarshalling/UnmarshallingSpec.scala
>>
>> On Tue, Aug 22, 2017 at 11:16 AM, Richard Rodseth 
>> wrote:
>>
>>> I'm trying to use Akka HTTP client (singleRequest) with an API that
>>> returns different JSON for the success and error cases. How is that best
>>> handled? Also, I'm not sure if the explicit marshalling and unmarshalling
>>> below is the right way to do things.
>>>
>>> Any help much appreciated.
>>>
>>> def addUser(baseURL: String, userAttributes: AddUserRequestDTO,
>>> cookieHeader: Cookie)(implicit http: HttpExt, materializer: Materializer,
>>> ec: ExecutionContext) = {
>>> val headers = List(cookieHeader)
>>> val addUserURL = ...
>>>
>>> val responseFuture = for {
>>>   requestEntity <- Marshal(userAttributes).to
>>> val request = HttpRequest(method = HttpMethods.POST, uri =
>>> addUserURL, headers = headers, entity = requestEntity)
>>>   response <- http.singleRequest(request)
>>>   x <- Unmarshal(response.entity).to[AddUserResponseDTO]
>>>   //body <- response.entity.dataBytes.runFold(ByteString(""))(_ ++
>>> _)
>>>   //val x = body.utf8String
>>> } yield (response, x)
>>> responseFuture
>>>
>>>   }
>>>
>>
>>
>

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


[akka-user] Re: Akka HTTP Client Unmarshalling - different success and error responses

2017-08-22 Thread Richard Rodseth
Spoke too soon

Exception in thread "main"
akka.http.scaladsl.unmarshalling.Unmarshaller$EitherUnmarshallingException:
Failed to unmarshal Either[AddUserErrorDTO, AddUserResponseDTO] (attempted
AddUserResponseDTO first). Right failure: Object is missing required member
'id', Left failure: Substream Source cannot be materialized more than once

Do I need to place a toStrict somewhere in my for comprehension?



On Tue, Aug 22, 2017 at 12:28 PM, Richard Rodseth 
wrote:

> Having some success with Either unmarshaller. I don't think it's
> documented, but here's a test:
>
> https://github.com/akka/akka-http/blob/master/akka-http-
> tests/src/test/scala/akka/http/scaladsl/unmarshalling/
> UnmarshallingSpec.scala
>
> On Tue, Aug 22, 2017 at 11:16 AM, Richard Rodseth 
> wrote:
>
>> I'm trying to use Akka HTTP client (singleRequest) with an API that
>> returns different JSON for the success and error cases. How is that best
>> handled? Also, I'm not sure if the explicit marshalling and unmarshalling
>> below is the right way to do things.
>>
>> Any help much appreciated.
>>
>> def addUser(baseURL: String, userAttributes: AddUserRequestDTO,
>> cookieHeader: Cookie)(implicit http: HttpExt, materializer: Materializer,
>> ec: ExecutionContext) = {
>> val headers = List(cookieHeader)
>> val addUserURL = ...
>>
>> val responseFuture = for {
>>   requestEntity <- Marshal(userAttributes).to
>> val request = HttpRequest(method = HttpMethods.POST, uri =
>> addUserURL, headers = headers, entity = requestEntity)
>>   response <- http.singleRequest(request)
>>   x <- Unmarshal(response.entity).to[AddUserResponseDTO]
>>   //body <- response.entity.dataBytes.runFold(ByteString(""))(_ ++ _)
>>   //val x = body.utf8String
>> } yield (response, x)
>> responseFuture
>>
>>   }
>>
>
>

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


[akka-user] Re: Akka HTTP Client Unmarshalling - different success and error responses

2017-08-22 Thread Richard Rodseth
Having some success with Either unmarshaller. I don't think it's
documented, but here's a test:

https://github.com/akka/akka-http/blob/master/akka-http-tests/src/test/scala/akka/http/scaladsl/unmarshalling/UnmarshallingSpec.scala

On Tue, Aug 22, 2017 at 11:16 AM, Richard Rodseth 
wrote:

> I'm trying to use Akka HTTP client (singleRequest) with an API that
> returns different JSON for the success and error cases. How is that best
> handled? Also, I'm not sure if the explicit marshalling and unmarshalling
> below is the right way to do things.
>
> Any help much appreciated.
>
> def addUser(baseURL: String, userAttributes: AddUserRequestDTO,
> cookieHeader: Cookie)(implicit http: HttpExt, materializer: Materializer,
> ec: ExecutionContext) = {
> val headers = List(cookieHeader)
> val addUserURL = ...
>
> val responseFuture = for {
>   requestEntity <- Marshal(userAttributes).to
> val request = HttpRequest(method = HttpMethods.POST, uri = addUserURL,
> headers = headers, entity = requestEntity)
>   response <- http.singleRequest(request)
>   x <- Unmarshal(response.entity).to[AddUserResponseDTO]
>   //body <- response.entity.dataBytes.runFold(ByteString(""))(_ ++ _)
>   //val x = body.utf8String
> } yield (response, x)
> responseFuture
>
>   }
>

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.