Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #146 - A simpler way to handle request body JSON #154

Merged
merged 1 commit into from
Jan 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions src/main/scala/kevinlee/github/GitHubApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
17 changes: 14 additions & 3 deletions src/main/scala/kevinlee/http/HttpRequest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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])
Expand Down