-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApiClient.scala
37 lines (30 loc) · 1.27 KB
/
ApiClient.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.gu.facia.client
import com.gu.facia.client.models.{ConfigJson, CollectionJson}
import play.api.libs.json.{Format, Json}
import scala.concurrent.{ExecutionContext, Future}
object ApiClient {
val Encoding = "utf-8"
}
case class ApiClient(
bucket: String,
/** e.g., CODE, PROD, DEV ... */
environment: String,
s3Client: S3Client
)(implicit executionContext: ExecutionContext) {
import com.gu.facia.client.ApiClient._
private def retrieve[A: Format](key: String): Future[Option[A]] = s3Client.get(bucket, key) map {
case FaciaSuccess(bytes) =>
Some(Json.fromJson[A](Json.parse(new String(bytes, Encoding))) getOrElse {
throw new JsonDeserialisationError(s"Could not deserialize JSON in $bucket, $key")
})
case FaciaNotAuthorized(message) => throw new BackendError(message)
case FaciaNotFound(_) => None
case FaciaUnknownError(message) => throw new BackendError(message)
}
def config: Future[ConfigJson] =
retrieve[ConfigJson](s"$environment/frontsapi/config/config.json").map(_ getOrElse {
throw new BackendError("Config was missing!! OH MY GOD")
})
def collection(id: String): Future[Option[CollectionJson]] =
retrieve[CollectionJson](s"$environment/frontsapi/collection/$id/collection.json")
}