From 748cd75e2da53c067359496b843845becd38e642 Mon Sep 17 00:00:00 2001 From: Kevin Lee Date: Sat, 23 Jan 2021 18:27:19 +1100 Subject: [PATCH] Issue #146 - A simpler way to handle request body JSON --- .../scala/kevinlee/github/GitHubApi.scala | 41 +++++++++---------- .../scala/kevinlee/http/HttpRequest.scala | 17 ++++++-- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/main/scala/kevinlee/github/GitHubApi.scala b/src/main/scala/kevinlee/github/GitHubApi.scala index 97b5456..9c7faef 100644 --- a/src/main/scala/kevinlee/github/GitHubApi.scala +++ b/src/main/scala/kevinlee/github/GitHubApi.scala @@ -2,7 +2,6 @@ package kevinlee.github import cats.Monad import cats.syntax.all._ -import io.circe._ import kevinlee.git.Git import kevinlee.github.data._ import kevinlee.http.{HttpClient, HttpRequest} @@ -66,16 +65,16 @@ object GitHubApi { repo: GitHubRepoWithAuth, ): F[Either[GitHubError, Option[GitHubRelease.Response]]] = { val url = s"$baseUrl/repos/${repo.gitHubRepo.org.org}/${repo.gitHubRepo.repo.repo}/releases" - val body = Encoder[GitHubRelease.CreateRequestParams].apply(params) - val httpRequest = HttpRequest.withHeadersAndBody( - HttpRequest.Method.post, - HttpRequest.Uri(url), - HttpRequest.Header("accept" -> DefaultAccept) :: - repo - .accessToken - .toHeaderList, - body, - ) + val httpRequest = HttpRequest + .withHeadersAndBody[GitHubRelease.CreateRequestParams]( + HttpRequest.Method.post, + HttpRequest.Uri(url), + HttpRequest.Header("accept" -> DefaultAccept) :: + repo + .accessToken + .toHeaderList, + params, + ) httpClient .request[Option[GitHubRelease.Response]](httpRequest) .map( @@ -91,16 +90,16 @@ object GitHubApi { ): F[Either[GitHubError, Option[GitHubRelease.Response]]] = { val url = s"$baseUrl/repos/${repo.gitHubRepo.org.org}/${repo.gitHubRepo.repo.repo}/releases/${params.releaseId.releaseId}" - val body = Encoder[GitHubRelease.UpdateRequestParams].apply(params) - val httpRequest = HttpRequest.withHeadersAndBody( - HttpRequest.Method.patch, - HttpRequest.Uri(url), - HttpRequest.Header("accept" -> DefaultAccept) :: - repo - .accessToken - .toHeaderList, - body, - ) + val httpRequest = HttpRequest + .withHeadersAndBody[GitHubRelease.UpdateRequestParams]( + HttpRequest.Method.patch, + HttpRequest.Uri(url), + HttpRequest.Header("accept" -> DefaultAccept) :: + repo + .accessToken + .toHeaderList, + params, + ) httpClient .request[Option[GitHubRelease.Response]](httpRequest) .map( diff --git a/src/main/scala/kevinlee/http/HttpRequest.scala b/src/main/scala/kevinlee/http/HttpRequest.scala index 66174b3..d5ff9c6 100644 --- a/src/main/scala/kevinlee/http/HttpRequest.scala +++ b/src/main/scala/kevinlee/http/HttpRequest.scala @@ -2,7 +2,7 @@ package kevinlee.http; import cats.{Applicative, Show} import cats.syntax.all._ -import io.circe.Json +import io.circe.{Encoder, Json} import io.estatico.newtype.macros._ import HttpRequest.Method.{Delete, Get, Patch, Post, Put} import org.http4s.{Request, Header => Http4sHeader, Uri => Http4sUri} @@ -176,8 +176,19 @@ object HttpRequest { def withHeaders(httpMethod: Method, uri: Uri, headers: List[Header]): HttpRequest = HttpRequest(httpMethod, uri, headers, List.empty[Param], none[Json]) - def withHeadersAndBody(httpMethod: Method, uri: Uri, headers: List[Header], body: Json): HttpRequest = - HttpRequest(httpMethod, uri, headers, List.empty[Param], body.some) + def withHeadersAndBody[A: Encoder]( + httpMethod: Method, + uri: Uri, + headers: List[Header], + body: A, + ): HttpRequest = + HttpRequest( + httpMethod, + uri, + headers, + List.empty[Param], + Encoder[A].apply(body).some, + ) def withoutBody(httpMethod: Method, uri: Uri): HttpRequest = HttpRequest(httpMethod, uri, List.empty[Header], List.empty[Param], none[Json])