Skip to content

Instantly share code, notes, and snippets.

@mpgarate
Created April 2, 2019 21:51
Show Gist options
  • Save mpgarate/e2ed4bc913fc5e29161f80ad2f9c8ca9 to your computer and use it in GitHub Desktop.
Save mpgarate/e2ed4bc913fc5e29161f80ad2f9c8ca9 to your computer and use it in GitHub Desktop.
diff --git a/merlin/example.scala b/merlin/example.scala
index e0f9146..887dfe8 100644
--- a/merlin/example.scala
+++ b/merlin/example.scala
@@ -14,7 +14,7 @@ object Example {
implicit val ListingJson = jsonFormat7(Listing)
- def main(args: Array[String]) {
+ def notmain(args: Array[String]) {
val listing = Listing("USD", "470761704", "housewares.home_decor", 495, "housewares.home_decor.Alphabet Letters.Metal Letters.Letters.Small Metal Letters.Wall Letters.Rustic Wedding.Wedding Letters.Rustic Decor.Rustic Home Decor.metal letter.nursery decor.metal wall decor.baby shower decor", "Alphabet Letters, Metal Letters, Letters, Small Metal Letters, Wall Letters, Rustic Wedding, Wedding Letters, Rustic Decor,Rustic Home Decor", "80490343")
val hosts = Host("blackbird01.ny5.etsy.com", 9710) :: Nil
diff --git a/merlin/src/main/scala/com/etsy/merlin/Engine.scala b/merlin/src/main/scala/com/etsy/merlin/Engine.scala
index 39a1361..b31f244 100644
--- a/merlin/src/main/scala/com/etsy/merlin/Engine.scala
+++ b/merlin/src/main/scala/com/etsy/merlin/Engine.scala
@@ -4,7 +4,7 @@ import java.net.URL
import java.util.zip.GZIPOutputStream
import java.io.ByteArrayOutputStream
-import scalaj.http.{Http,HttpRequest}
+import scalaj.http.{Http,HttpRequest,HttpResponse}
import scalaz._
import Scalaz._
@@ -23,7 +23,7 @@ trait Engine[M[_]] {
}
}
-class SimpleHttpEngine[E](servers: List[Host], headers: List[(String,String)] = List(), recover: Throwable => E, gzip: Boolean=false) extends Engine[({type λ[α] = E \/ α})#λ] {
+class SimpleHttpEngine[E](servers: List[Host], headers: List[(String,String)] = List(), recover: Throwable => E, gzip: Boolean=false, throwOnHttpError: Boolean = false) extends Engine[({type λ[α] = E \/ α})#λ] {
import Utils._
val allHeaders = headers ++ (if(gzip) {
@@ -60,8 +60,17 @@ class SimpleHttpEngine[E](servers: List[Host], headers: List[(String,String)] =
(new URL(new URL(randomServer), route)).toString
}
+ def handleRequest(request: HttpRequest): E \/ HttpResponse[String] = {
+ val resp = request.asString
+
+ (throwOnHttpError, resp.isError) match {
+ case (true, true) => -\/(s"Received error code ${resp.code} from server. \n ${resp.body}") !!
+ case _ => \/-(resp)
+ }
+ }
+
def execute(request: HttpRequest): E \/ String = for {
- resp <- request.asString !! recover
+ resp <- handleRequest(request) !! recover
} yield resp.body
def post(route: String, json: String): E \/ String = execute(buildPost(getPath(route), json))
@@ -69,7 +78,7 @@ class SimpleHttpEngine[E](servers: List[Host], headers: List[(String,String)] =
def get(route: String, params: Map[String,String]=Map()): E \/ String = execute(buildGet(getPath(route), params))
}
-case class JsonHttpEngine[E](servers: List[Host], f: Throwable => E, gzip: Boolean = false) extends SimpleHttpEngine[E](servers, List("Content-Type" -> "application/json"), f, gzip)
+case class JsonHttpEngine[E](servers: List[Host], f: Throwable => E, gzip: Boolean = false, throwOnHttpError: Boolean = false) extends SimpleHttpEngine[E](servers, List("Content-Type" -> "application/json"), f, gzip, throwOnHttpError)
-case class HttpEngine(servers: List[Host], headers: List[(String,String)] = List(), gzip: Boolean = false) extends SimpleHttpEngine[String](servers, headers, { case s => s"Unable to call server $s.getMessage" }, gzip)
+case class HttpEngine(servers: List[Host], headers: List[(String,String)] = List(), gzip: Boolean = false, throwOnHttpError: Boolean = false) extends SimpleHttpEngine[String](servers, headers, { case s => s"Unable to call server ${s.getMessage}" }, gzip, throwOnHttpError)
diff --git a/merlin/static/empty.html b/merlin/static/empty.html
new file mode 100644
index 0000000..e69de29
diff --git a/merlin/static/test.html b/merlin/static/test.html
new file mode 100644
index 0000000..9766475
--- /dev/null
+++ b/merlin/static/test.html
@@ -0,0 +1 @@
+ok
diff --git a/merlin/test.scala b/merlin/test.scala
new file mode 100644
index 0000000..3f4636a
--- /dev/null
+++ b/merlin/test.scala
@@ -0,0 +1,13 @@
+package merlin
+
+import scalaz.{-\/, \/-}
+
+import com.etsy.merlin.{Host, HttpEngine}
+
+object Test {
+ def main(args: Array[String]) {
+ val engine = HttpEngine(new Host("localhost", 8080) :: Nil, ("content-type", "application/json") :: Nil)
+
+ println(engine.get("/dne.html"))
+ }
+}
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment