All Downloads are FREE. Search and download functionalities are using the official Maven repository.

as.oauth.token.scala Maven / Gradle / Ivy

There is a newer version: 0.13.4
Show newest version
package dispatch.as.oauth

import dispatch.oauth._
import com.ning.http.client.Response
import com.ning.http.client.oauth._

object Token extends (Response => Either[String, RequestToken]) {
  def apply(res: Response) = tokenDecode(dispatch.as.String(res))
  private def decode(str: String) = java.net.URLDecoder.decode(str, "utf-8")
  private def formDecode(str: String) =
    (for (pair <- str.trim.split('&'))
      yield pair.split('=')
    ).collect {
      case Array(key, value) => decode(key) -> decode(value)
      case Array(key) => decode(key) -> ""
    }

  private def tokenDecode(str: String) = {
    val params = formDecode(str)
    (for {
      ("oauth_token", tok) <- params
      ("oauth_token_secret", secret) <- params
    } yield new RequestToken(tok, secret)).headOption.toRight {
      "No token found in response: \n\n" + str
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy