Skip to content

Commit

Permalink
http4s: pass apikey for schema-list method (close #166)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuwy committed Jul 30, 2021
1 parent a29429c commit 66b293c
Showing 1 changed file with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import cats.effect.Sync
import io.circe.Json
import org.http4s.circe._
import org.http4s.client.{Client => HttpClient}
import org.http4s.{EntityDecoder, Header, Headers, Request, Uri}
import org.http4s.{EntityDecoder, Header, Headers, Request, Status, Uri}
import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaList}
import com.snowplowanalytics.iglu.core.circe.CirceIgluCodecs._
import org.http4s.Method.GET
Expand Down Expand Up @@ -55,14 +55,10 @@ object Http4sRegistryLookup {
): EitherT[F, RegistryError, Json] =
for {
uri <- EitherT.fromEither[F](toPath(http, key))
headers =
http.apikey.fold[Headers](Headers.empty)(apikey => Headers.of(Header("apikey", apikey)))
request = Request[F](method = GET, uri = uri, headers = headers)
json <- EitherT(Sync[F].attempt(client.expect[Json](request))).leftMap[RegistryError] { e =>
val error = s"Unexpected exception fetching: $e"
RegistryError.RepoFailure(error)
}
} yield json
headers = http.apikey.fold[Headers](Headers.empty)(apikey => Headers.of(Header("apikey", apikey)))
response = runRequest[F, Json](client, Request[F](method = GET, uri = uri, headers = headers))
result <- EitherT(response)
} yield result

def httpList[F[_]: Sync](
client: HttpClient[F],
Expand All @@ -73,11 +69,10 @@ object Http4sRegistryLookup {
): EitherT[F, RegistryError, SchemaList] =
for {
uri <- EitherT.fromEither[F](toSubpath(http, vendor, name, model))
sl <- EitherT(Sync[F].attempt(client.expect[SchemaList](uri))).leftMap[RegistryError] { e =>
val error = s"Unexpected exception listing: $e"
RegistryError.RepoFailure(error)
}
} yield sl
headers = http.apikey.fold[Headers](Headers.empty)(apikey => Headers.of(Header("apikey", apikey)))
response = runRequest[F, SchemaList](client, Request[F](method = GET, uri = uri, headers = headers))
result <- EitherT(response)
} yield result

def toPath(cxn: Registry.HttpConnection, key: SchemaKey): Either[RegistryError, Uri] =
Uri
Expand All @@ -94,5 +89,37 @@ object Http4sRegistryLookup {
.fromString(s"${cxn.uri.toString.stripSuffix("/")}/schemas/$vendor/$name/jsonschema/$model")
.leftMap(e => RegistryError.ClientFailure(e.message))

def runRequest[F[_]: Sync, A: EntityDecoder[F, *]](client: HttpClient[F], req: Request[F]): F[Either[RegistryError, A]] = {
val responseResult = client.run(req).use[F, Either[RegistryError, A]] {
case Status.Successful(response) =>
response.as[A].map(_.asRight)
case Status.ClientError(response) if response.status.code == 404 =>
(RegistryError.NotFound: RegistryError).asLeft[A].pure[F]
case Status.ServerError(response) =>
response.bodyText.compile.string.map { body =>
val error = s"Unexpected server response: $body"
RegistryError.RepoFailure(error).asLeft
}
case Status.ClientError(response) =>
response.bodyText.compile.string.map { body =>
val error = s"Unexpected server response: $body"
RegistryError.ClientFailure(error).asLeft
}
case response =>
response.bodyText.compile.string.map { body =>
val error = s"Unexpected response: $body"
RegistryError.ClientFailure(error).asLeft
}
}

responseResult.attempt.map {
case Right(result) =>
result
case Left(exception) =>
val error = Option(exception.getMessage).getOrElse(exception.toString)
RegistryError.ClientFailure(error).asLeft
}
}

implicit def schemaListDecoder[F[_]: Sync]: EntityDecoder[F, SchemaList] = jsonOf[F, SchemaList]
}

0 comments on commit 66b293c

Please sign in to comment.