|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 47 Degrees, LLC. <http://www.47deg.com> |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 | + * this software and associated documentation files (the "Software"), to deal in |
| 6 | + * the Software without restriction, including without limitation the rights to |
| 7 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 8 | + * the Software, and to permit persons to whom the Software is furnished to do so, |
| 9 | + * subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 16 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 17 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 18 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +package github4s |
| 23 | + |
| 24 | +import scala.concurrent.Future |
| 25 | +import fr.hmil.roshttp._ |
| 26 | +import fr.hmil.roshttp.body.{BodyPart, BulkBodyPart} |
| 27 | +import java.nio.ByteBuffer |
| 28 | + |
| 29 | +import cats.implicits._ |
| 30 | +import fr.hmil.roshttp.response.SimpleHttpResponse |
| 31 | +import fr.hmil.roshttp.util.HeaderMap |
| 32 | +import fr.hmil.roshttp.body.Implicits._ |
| 33 | +import fr.hmil.roshttp.exceptions.HttpException |
| 34 | + |
| 35 | +import scala.concurrent.ExecutionContext.Implicits.global |
| 36 | +import github4s.GithubResponses.{GHResponse, GHResult, JsonParsingException, UnexpectedException} |
| 37 | +import github4s.GithubDefaultUrls._ |
| 38 | +import github4s.Decoders._ |
| 39 | +import github4s.HttpClient.HttpCode400 |
| 40 | +import io.circe.Decoder |
| 41 | +import io.circe.parser._ |
| 42 | +import monix.reactive.Observable |
| 43 | + |
| 44 | +import scala.util.{Failure, Success} |
| 45 | + |
| 46 | +case class CirceJSONBody(value: String) extends BulkBodyPart { |
| 47 | + override def contentType: String = s"application/json; charset=utf-8" |
| 48 | + override def contentData: ByteBuffer = ByteBuffer.wrap(value.getBytes("utf-8")) |
| 49 | +} |
| 50 | + |
| 51 | +trait HttpRequestBuilderExtensionJS { |
| 52 | + |
| 53 | + import monix.execution.Scheduler.Implicits.global |
| 54 | + |
| 55 | + val userAgent = { |
| 56 | + val name = github4s.BuildInfo.name |
| 57 | + val version = github4s.BuildInfo.version |
| 58 | + s"$name/$version" |
| 59 | + } |
| 60 | + |
| 61 | + implicit def extensionJS: HttpRequestBuilderExtension[SimpleHttpResponse, Future] = |
| 62 | + new HttpRequestBuilderExtension[SimpleHttpResponse, Future] { |
| 63 | + def run[A](rb: HttpRequestBuilder[SimpleHttpResponse, Future])( |
| 64 | + implicit D: Decoder[A]): Future[GHResponse[A]] = { |
| 65 | + val request = HttpRequest(rb.url) |
| 66 | + .withMethod(Method(rb.httpVerb.verb)) |
| 67 | + .withQueryParameters(rb.params.toSeq: _*) |
| 68 | + .withHeader("content-type", "application/json") |
| 69 | + .withHeader("user-agent", userAgent) |
| 70 | + .withHeaders(rb.authHeader.toList: _*) |
| 71 | + .withHeaders(rb.headers.toList: _*) |
| 72 | + |
| 73 | + rb.data |
| 74 | + .map(d => request.send(CirceJSONBody(d))) |
| 75 | + .getOrElse(request.send()) |
| 76 | + .map(toEntity[A]) |
| 77 | + .recoverWith { |
| 78 | + case e => Future.successful(Either.left(UnexpectedException(e.getMessage))) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + def toEntity[A](response: SimpleHttpResponse)(implicit D: Decoder[A]): GHResponse[A] = |
| 84 | + response match { |
| 85 | + case r if r.statusCode < HttpCode400.statusCode ⇒ |
| 86 | + decode[A](r.body).fold( |
| 87 | + e ⇒ Either.left(JsonParsingException(e.getMessage, r.body)), |
| 88 | + result ⇒ |
| 89 | + Either.right( |
| 90 | + GHResult(result, r.statusCode, rosHeaderMapToRegularMap(r.headers)) |
| 91 | + ) |
| 92 | + ) |
| 93 | + case r ⇒ |
| 94 | + Either.left( |
| 95 | + UnexpectedException( |
| 96 | + s"Failed invoking get with status : ${r.statusCode}, body : \n ${r.body}")) |
| 97 | + } |
| 98 | + |
| 99 | + private def rosHeaderMapToRegularMap( |
| 100 | + headers: HeaderMap[String]): Map[String, IndexedSeq[String]] = |
| 101 | + headers.flatMap(m => Map(m._1.toLowerCase -> IndexedSeq(m._2))) |
| 102 | + |
| 103 | +} |
0 commit comments