This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1. a Header is just a type alias for (String, String) | |
val singleHeader: Header = "X-SVCC-Hello" -> "World" | |
//2. a HeaderList is a NonEmptyList - you need to have at least one Header to compile | |
val headerList1: HeaderList = NonEmptyList(singleHeader) | |
val headerList2: HeaderList = NonEmptyList() // this won't compile | |
//3. Headers is an Option[HeaderList], so your code needs to check for no Headers to compile | |
//(we found that a lot of our engineers were forgetting to send some necessary headers in various HTTP requests) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1. build the URL from the protocol, then the host, then the path | |
val urlBuilder1 = url(http, "siliconvalley-codecamp.com") / "Session" / "2013" / "newman-a-functional-rest-client-in-scala" | |
//2. won't compile because we didn't build from the protocol first | |
val urlBuilder2 = url("silicon-valley-codecamp.com") / "Session" | |
//3. won't compile because we tried to add a path after the query string was already started | |
val urlBuilder3 = url(http, "silicon-valley-codecamp.com") / "Session" ? ("hello" -> "world") / "2013" | |
//4. if you can compile, toURL won't throw, and you get a URL that you can pass to a Newman HttpClient method |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//the class into which we want to decode some JSON | |
//the JSON looks like this: {"one": 1, "two": 2} | |
case class MyClass(one: Int, two: String) | |
val MyClassJSONR: JSONR[MyClass] = new JSONR[MyClass] { | |
//the method that transforms some JSON into a Result | |
//Result is either a Failure (instead of throwing an exception) or Success | |
override def read(json: JValue): Result[MyClass] = { | |
val fieldOne: Result[Int] = field[Int]("one")(json) | |
val fieldTwo: Result[Int] = field[Int]("two")(json) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val mediaType = MediaType.custom("image/png") | |
val contentType = ContentType(mediaType, None) | |
val body = Array(1.toByte) //I also tried Array.empty[Byte], and it had the same behavior | |
val entity = HttpEntity(contentType, body) | |
val response = HttpResponse(StatusCodes.OK, entity) | |
responder ! ChunkedResponseStart(response).withAck(Ack) //responder is the standard spray actor for the HTTP request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//create one HttpClient. notice that requests come exclusively from the client. | |
//Newman has other HttpClient implementations, including ones that cache. use the best for the job | |
val httpClient: HttpClient = new ApacheHttpClient() | |
val url = new URL("siliconvalley-codecamp.com") | |
val headers: Headers = None | |
//create one HttpRequest | |
val request = httpClient.get(url, headers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed trait Validation[Failure, Success] | |
case class Failure[Failure, Success](f: Failure) extends Validation[Failure, Success] | |
case class Success[Failure, Success](s: Success) extends Validation[Failure, Success] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this is a type class that lets us describe some type T | |
trait Describable[T] { | |
def getDescription(t: T): String | |
} | |
//we can't inherit from this unless we're in the same file. inconvenient | |
sealed abstract class SealedClass(val a: Int, val b: Int) | |
//make SealedClass belong to the type class Describable | |
val DescribableSealedClass = new Describable[SealedClass] { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val host = "http://www.siliconvalley-codecamp.com/" | |
val url = new URL(host) | |
val client = ClientBuilder() | |
.codec(Http()) | |
.hosts(host) //there are more params you can set here | |
.build() | |
val headers: Map[String, String] = ??? | |
val method: Method = ??? // this is a org.jboss.netty.handler.codec.http.HttpMethod | |
val channelBuf: ChannelBuffer = ??? //this is a org.jboss.netty.buffer.ChannelBuffer | |
val req = RequestBuilder() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val req = new HttpGet | |
val url = new URL("http://http://www.siliconvalley-codecamp.com/") | |
req.setURI(url.toURI) | |
val headers: List[(String, String)] = ??? | |
headers.foreach { tup: (String, String) => | |
val (headerName, headerValue) = tup | |
if(!headerName.equalsIgnoreCase("Content-Type")) { | |
req.addHeader(headerName, headerValue) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
^CAarons-Macbook-Pro:Desktop aaron$ scala syncFutures.scala | |
creating future 0 | |
key/0 waiting | |
creating future 1 | |
key/0 proceeding | |
creating future 2 | |
key/1 waiting | |
key/2 waiting | |
key/0 completed with Success(()) | |
key/1 proceeding |