Skip to content

Instantly share code, notes, and snippets.

@zph
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save zph/c150a88958e854e8be88 to your computer and use it in GitHub Desktop.

Select an option

Save zph/c150a88958e854e8be88 to your computer and use it in GitHub Desktop.
// package gala
import scala.sys
import scala.concurrent.Future
import scala.concurrent.duration._
import akka.actor.ActorSystem
import akka.util.Timeout
import akka.pattern.ask
import akka.io.IO
import spray.can.Http
import spray.http._
import HttpMethods._
class Gala(file: String) {
}
class Util {
type Token = String
def e(key: String) : Token = sys.env.getOrElse(key, "").asInstanceOf[Token]
val pwd = e("PWD")
val github_auth_token = e("GITHUB_AUTH_TOKEN")
}
class CLI{
def main(args: Array[String]) = {}
}
class Api {
def get = {
//execute a GET request
val url = "https://api.github.com/users/zph/events"
implicit val system: ActorSystem = ActorSystem()
implicit val timeout: Timeout = Timeout(15.seconds)
import system.dispatcher // implicit execution context
for {
response <- IO(Http).ask(HttpRequest(GET, Uri(url))).mapTo[HttpResponse]
_ <- IO(Http) ? Http.CloseAll
} yield {
system.log.info("Request-Level API: received {} response with {} bytes",
response.status, response.entity.data.length)
response.header[HttpHeaders.Server].get.products.head
response
}
}
}
- What's right/common way to make API request as Future, handle response, and then extract values from
return value? (say, the body content and convert to JSON representation in Scala)
- How do I extract the HttpResponse once I unbox the Option?
- Where are Objects used instead of Classes, what are their limitations? Objects seem analogous to
Singletons in Ruby.
scala> val api = new Api api: Api = Api@6f5277ad
scala> val response = api.get
response: scala.concurrent.Future[spray.http.HttpResponse] =
scala.concurrent.impl.Promise$DefaultPromise@2c6cd510
scala> response.value
res1: Option[scala.util.Try[spray.http.HttpResponse]] = Some(Success(HttpResponse(200
OK,HttpEntity(application/json;
charset=UTF-8,[{"id":"2564223433","type":"PushEvent","actor":{"id":1026584,"login":"zph","gravatar_id":"","url":"https://api.github.com/users/zph","avatar_url":"https://avatars.githubusercontent.com/u/1026584?"},"repo":{"id":16684004,"name":"zph/zph.github.io","url":"https://api.github.com/repos/zph/zph.github.io"},"payload":{"push_id":567521062,"size":1,"distinct_size":1,"ref":"refs/heads/source","head":"b3aa063d1ed4de101cbbb2a819fc49b15416e3a0","before":"bab1fdadaf5a585d50e4ee996c54f70938b1d24e","commits":[...),List(X-Served-By:
318e55760cf7cdb40e61175a4d36cd32, Vary: Accept-Encoding, X-Content-Type-Options: nosniff,
Strict-Transport-Security: max-age=31536000; include...
# I played around with pattern matching and understand concept of doing pattern matching on Some|None
scala> response.value.get.toString
res3: String = Success(HttpResponse(200 OK,HttpEntity(application/json;
charset=UTF-8,[{"id":"2564223433","type":"PushEvent","actor":{"id":1026584,"login":"zph","gravatar_id":"","url":"https://api.github.com/users/zph","avatar_url":"https://avatars.githubusercontent.com/u/1026584?"},"repo":{"id":16684004,"name":"zph/zph.github.io","url":"https://api.github.com/repos/zph/zph.github.io"},"payload":{"push_id":567521062,"size":1,"distinct_size":1,"ref":"refs/heads/source","head":"b3aa063d1ed4de101cbbb2a819fc49b15416e3a0","before":"bab1fdadaf5a585d50e4ee996c54f70938b1d24e","commits":[...),List(X-Served-By:
318e55760cf7cdb40e61175a4d36cd32, Vary: Accept-Encoding, X-Content-Type-Options: nosniff,
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload, X-GitHub-Request-Id: ADDA...
scala> val api = new Api
api: Api = Api@6f5277ad
scala> val response = api.get
response: scala.concurrent.Future[spray.http.HttpResponse] = scala.concurrent.impl.Promise$DefaultPromise@2c6cd510
scala> response.value
res1: Option[scala.util.Try[spray.http.HttpResponse]] = Some(Success(HttpResponse(200 OK,HttpEntity(application/json; charset=UTF-8,[{"id":"2564223433","type":"PushEvent","actor":{"id":1026584,"login":"zph","gravatar_id":"","url":"https://api.github.com/users/zph","avatar_url":"https://avatars.githubusercontent.com/u/1026584?"},"repo":{"id":16684004,"name":"zph/zph.github.io","url":"https://api.github.com/repos/zph/zph.github.io"},"payload":{"push_id":567521062,"size":1,"distinct_size":1,"ref":"refs/heads/source","head":"b3aa063d1ed4de101cbbb2a819fc49b15416e3a0","before":"bab1fdadaf5a585d50e4ee996c54f70938b1d24e","commits":[...),List(X-Served-By: 318e55760cf7cdb40e61175a4d36cd32, Vary: Accept-Encoding, X-Content-Type-Options: nosniff, Strict-Transport-Security: max-age=31536000; include...
# I played around with pattern matching and understand concept of doing pattern matching on Some|None
scala> response.value.get.toString
res3: String = Success(HttpResponse(200 OK,HttpEntity(application/json; charset=UTF-8,[{"id":"2564223433","type":"PushEvent","actor":{"id":1026584,"login":"zph","gravatar_id":"","url":"https://api.github.com/users/zph","avatar_url":"https://avatars.githubusercontent.com/u/1026584?"},"repo":{"id":16684004,"name":"zph/zph.github.io","url":"https://api.github.com/repos/zph/zph.github.io"},"payload":{"push_id":567521062,"size":1,"distinct_size":1,"ref":"refs/heads/source","head":"b3aa063d1ed4de101cbbb2a819fc49b15416e3a0","before":"bab1fdadaf5a585d50e4ee996c54f70938b1d24e","commits":[...),List(X-Served-By: 318e55760cf7cdb40e61175a4d36cd32, Vary: Accept-Encoding, X-Content-Type-Options: nosniff, Strict-Transport-Security: max-age=31536000; includeSubdomains; preload, X-GitHub-Request-Id: ADDA...
> console
[info] Compiling 1 Scala source to /Users/zph/src/scala/gala/target/scala-2.10/classes...
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_40-ea).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val api = new Api
api: Api = Api@6f5277ad
scala> val response = api.get
response: scala.concurrent.Future[spray.http.HttpResponse] = scala.concurrent.impl.Promise$DefaultPromise@2c6cd510
scala> [INFO] [02/07/2015 01:34:06.689] [default-akka.actor.default-dispatcher-2] [ActorSystem(default)] Request-Level API: received 200 OK response with 25735 bytes
response
res0: scala.concurrent.Future[spray.http.HttpResponse] = scala.concurrent.impl.Promise$DefaultPromise@2c6cd510
scala> response.
andThen asInstanceOf collect failed fallbackTo filter
flatMap foreach isCompleted isInstanceOf map mapTo
onComplete onFailure onSuccess ready recover recoverWith
result toString transform value withFilter zip
scala> response.
andThen asInstanceOf collect failed fallbackTo filter
flatMap foreach isCompleted isInstanceOf map mapTo
onComplete onFailure onSuccess ready recover recoverWith
result toString transform value withFilter zip
scala> response.value
res1: Option[scala.util.Try[spray.http.HttpResponse]] = Some(Success(HttpResponse(200 OK,HttpEntity(application/json; charset=UTF-8,[{"id":"2564223433","type":"PushEvent","actor":{"id":1026584,"login":"zph","gravatar_id":"","url":"https://api.github.com/users/zph","avatar_url":"https://avatars.githubusercontent.com/u/1026584?"},"repo":{"id":16684004,"name":"zph/zph.github.io","url":"https://api.github.com/repos/zph/zph.github.io"},"payload":{"push_id":567521062,"size":1,"distinct_size":1,"ref":"refs/heads/source","head":"b3aa063d1ed4de101cbbb2a819fc49b15416e3a0","before":"bab1fdadaf5a585d50e4ee996c54f70938b1d24e","commits":[...),List(X-Served-By: 318e55760cf7cdb40e61175a4d36cd32, Vary: Accept-Encoding, X-Content-Type-Options: nosniff, Strict-Transport-Security: max-age=31536000; include...
scala> response.value.
asInstanceOf canEqual collect exists filter
filterNot flatMap flatten fold forall
foreach get getOrElse isDefined isEmpty
isInstanceOf iterator map nonEmpty orElse
orNull productArity productElement productIterator productPrefix
toLeft toList toRight toString withFilter
scala> response.value.
asInstanceOf canEqual collect exists filter
filterNot flatMap flatten fold forall
foreach get getOrElse isDefined isEmpty
isInstanceOf iterator map nonEmpty orElse
orNull productArity productElement productIterator productPrefix
toLeft toList toRight toString withFilter
scala> response.value.get
res2: scala.util.Try[spray.http.HttpResponse] = Success(HttpResponse(200 OK,HttpEntity(application/json; charset=UTF-8,[{"id":"2564223433","type":"PushEvent","actor":{"id":1026584,"login":"zph","gravatar_id":"","url":"https://api.github.com/users/zph","avatar_url":"https://avatars.githubusercontent.com/u/1026584?"},"repo":{"id":16684004,"name":"zph/zph.github.io","url":"https://api.github.com/repos/zph/zph.github.io"},"payload":{"push_id":567521062,"size":1,"distinct_size":1,"ref":"refs/heads/source","head":"b3aa063d1ed4de101cbbb2a819fc49b15416e3a0","before":"bab1fdadaf5a585d50e4ee996c54f70938b1d24e","commits":[...),List(X-Served-By: 318e55760cf7cdb40e61175a4d36cd32, Vary: Accept-Encoding, X-Content-Type-Options: nosniff, Strict-Transport-Security: max-age=31536000; includeSubdomains; p...
scala> response.value.get.
asInstanceOf isInstanceOf toString
scala> response.value.get.
asInstanceOf isInstanceOf toString
scala> response.value.get.toString
res3: String = Success(HttpResponse(200 OK,HttpEntity(application/json; charset=UTF-8,[{"id":"2564223433","type":"PushEvent","actor":{"id":1026584,"login":"zph","gravatar_id":"","url":"https://api.github.com/users/zph","avatar_url":"https://avatars.githubusercontent.com/u/1026584?"},"repo":{"id":16684004,"name":"zph/zph.github.io","url":"https://api.github.com/repos/zph/zph.github.io"},"payload":{"push_id":567521062,"size":1,"distinct_size":1,"ref":"refs/heads/source","head":"b3aa063d1ed4de101cbbb2a819fc49b15416e3a0","before":"bab1fdadaf5a585d50e4ee996c54f70938b1d24e","commits":[...),List(X-Served-By: 318e55760cf7cdb40e61175a4d36cd32, Vary: Accept-Encoding, X-Content-Type-Options: nosniff, Strict-Transport-Security: max-age=31536000; includeSubdomains; preload, X-GitHub-Request-Id: ADDA...
scala> response.value.get.
asInstanceOf isInstanceOf toString
scala> response.value.get
res4: scala.util.Try[spray.http.HttpResponse] = Success(HttpResponse(200 OK,HttpEntity(application/json; charset=UTF-8,[{"id":"2564223433","type":"PushEvent","actor":{"id":1026584,"login":"zph","gravatar_id":"","url":"https://api.github.com/users/zph","avatar_url":"https://avatars.githubusercontent.com/u/1026584?"},"repo":{"id":16684004,"name":"zph/zph.github.io","url":"https://api.github.com/repos/zph/zph.github.io"},"payload":{"push_id":567521062,"size":1,"distinct_size":1,"ref":"refs/heads/source","head":"b3aa063d1ed4de101cbbb2a819fc49b15416e3a0","before":"bab1fdadaf5a585d50e4ee996c54f70938b1d24e","commits":[...),List(X-Served-By: 318e55760cf7cdb40e61175a4d36cd32, Vary: Accept-Encoding, X-Content-Type-Options: nosniff, Strict-Transport-Security: max-age=31536000; includeSubdomains; p...
scala>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment