A way to access Header after StatusCode exception?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

A way to access Header after StatusCode exception?

jraedisch
When sending a request like below and a 420 StatusCode is thrown, is there any way to access the
Retry-After header?

val result = try {Some(http(req...))}
catch {case sc:StatusCode if sc.code == 420 => None}

How else could I get it and postpone the next request accordingly?

Thanks again,

Jasper
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: A way to access Header after StatusCode exception?

n8han
Administrator
On 11/07/2010 05:39 PM, jraedisch [via Databinder] wrote:
> When sending a request like below and a 420 StatusCode is thrown, is
> there any way to access the
> Retry-After header?
>
> val result = try {Some(http(req...))}
> catch {case sc:StatusCode if sc.code == 420 => None}
>
> How else could I get it and postpone the next request accordingly?

Sure, in that case it's better if the exception isn't thrown. You can
use the "x" method on an Http instance to handle responses of any status
with a pattern matching expression. There's an example in HttpSpec:

http://sourced.implicit.ly/net.databinder/dispatch-http.test/0.7.7/HttpSpec.scala.html 
<http://sourced.implicit.ly/net.databinder/dispatch-http.test/0.7.7/HttpSpec.scala.html#26960>

Look for "allow any status code with x".

Nathan
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: A way to access Header after StatusCode exception?

jeffcrilly
Old post, but since I just got done digging into a very similar requirement, thought i'd post my solution...

I am using dispatch for an API, and when doing a "login" need to get back the status code and the headers, which may contain an "authorization" token when the login is successful....

(Fyi: There might be a better way.. I'm coming from java and this is like day 15 with scala and day 4 w/ databinder-dispatch.)

    val req = new Request(:/(hostname).secure as (username, password))
                                                                        //   v--- dont throw exceptions
    val (status: Int, response: BasicHttpResponse) = http x (req / loginUrlStr as_str  {
      case (status, response, _,  _) => (status, response)
    })

response will contain the headers, so ...    

    val authHeader = response.getHeaders("x-myapp-authorization") // is a java Array

    val authTokenStr = authHeader.length match {
      case 1 => Some(authHeader(0).getValue)
      case _ => None
    }

Tested the above w/ valid and invalid logins.  Works as expected.
With a bogus host address, the apache http client throws a ConnectException.  
That's ok; I'll probably just try/catch for that at a higher level.

thx

-jeff
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: A way to access Header after StatusCode exception?

n8han
Administrator
That's a good start. You can also use the header extractor and still handle any status code.

    val (status, headers) = http x ((req / loginUrlStr >:> identity)  {
      case (status, _, _,  out) => (status, out())
    })

Nathan

On 06/12/2011 04:13 AM, jeffcrilly [via Databinder] wrote:
Old post, but since I just got done digging into a very similar requirement, thought i'd post my solution...

I am using dispatch for an API, and when doing a "login" need to get back the status code and the headers, which may contain an "authorization" token when the login is successful....

(Fyi: There might be a better way.. I'm coming from java and this is like day 15 with scala and day 4 w/ databinder-dispatch.)

    val req = new Request(:/(hostname).secure as (username, password))
                                                                        //   v--- dont throw exceptions
    val (status: Int, response: BasicHttpResponse) = http x (req / loginUrlStr as_str  {
      case (status, response, _,  _) => (status, response)
    })

response will contain the headers, so ...    

    val authHeader = response.getHeaders("x-myapp-authorization") // is a java Array

    val authTokenStr = authHeader.length match {
      case 1 => Some(authHeader(0).getValue)
      case _ => None
    }

Tested the above w/ valid and invalid logins.  Works as expected.
With a bogus host address, the apache http client throws a ConnectException.  
That's ok; I'll probably just try/catch for that at a higher level.

thx

-jeff


If you reply to this email, your message will be added to the discussion below:
http://databinder.3617998.n2.nabble.com/A-way-to-access-Header-after-StatusCode-exception-tp5715327p6466775.html
To start a new topic under Databinder, email [hidden email]
To unsubscribe from Databinder, click here.

Loading...