Skip to content

Instantly share code, notes, and snippets.

View arschles's full-sized avatar
🎯
Focusing

Aaron Schlesinger arschles

🎯
Focusing
View GitHub Profile
//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)
//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
//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)
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
//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)
@arschles
arschles / svcc2013-Validation.scala
Last active December 24, 2015 04:59
this is a set of classes the ensures the compiler will check your code to make sure you're dealing with errors
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 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] {
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()
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)
}
}
@arschles
arschles / sequentialFutures.scala
Last active December 23, 2015 09:39
executing futures sequentially. the f method returns futures that are mutually exclusive with all other futures executed on the same key
^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