|
| 1 | +package net.wiringbits.webapp.utils.api |
| 2 | + |
| 3 | +import net.wiringbits.webapp.utils.api.models._ |
| 4 | +import play.api.libs.json._ |
| 5 | +import sttp.client3._ |
| 6 | +import sttp.model._ |
| 7 | + |
| 8 | +import scala.concurrent.{ExecutionContext, Future} |
| 9 | +import scala.util.{Failure, Success, Try} |
| 10 | + |
| 11 | +trait AdminDataExplorerApiClient { |
| 12 | + def getTables: Future[AdminGetTables.Response] |
| 13 | + |
| 14 | + def getTableMetadata( |
| 15 | + tableName: String, |
| 16 | + sort: List[String], |
| 17 | + range: List[Int], |
| 18 | + filters: String |
| 19 | + ): Future[List[Map[String, String]]] |
| 20 | + |
| 21 | + def viewItem(tableName: String, id: String): Future[Map[String, String]] |
| 22 | + |
| 23 | + def viewItems(tableName: String, ids: List[String]): Future[List[Map[String, String]]] |
| 24 | + |
| 25 | + def createItem(tableName: String, request: AdminCreateTable.Request): Future[AdminCreateTable.Response] |
| 26 | + |
| 27 | + def updateItem(tableName: String, id: String, request: Map[String, String]): Future[AdminUpdateTable.Response] |
| 28 | + |
| 29 | + def deleteItem(tableName: String, id: String): Future[AdminDeleteTable.Response] |
| 30 | +} |
| 31 | + |
| 32 | +object AdminDataExplorerApiClient { |
| 33 | + case class Config(serverUrl: String) |
| 34 | + |
| 35 | + private def asJson[R: Reads] = { |
| 36 | + asString |
| 37 | + .map { |
| 38 | + case Right(response) => |
| 39 | + // handles 2xx responses |
| 40 | + Success(response) |
| 41 | + case Left(response) => |
| 42 | + // handles non 2xx responses |
| 43 | + Try { |
| 44 | + val json = Json.parse(response) |
| 45 | + // TODO: Unify responses to match the play error format |
| 46 | + json |
| 47 | + .asOpt[ErrorResponse] |
| 48 | + .orElse { |
| 49 | + json |
| 50 | + .asOpt[PlayErrorResponse] |
| 51 | + .map(model => ErrorResponse(model.error.message)) |
| 52 | + } |
| 53 | + .getOrElse(throw new RuntimeException(s"Unexpected JSON response: $response")) |
| 54 | + } match { |
| 55 | + case Failure(exception) => |
| 56 | + println(s"Unexpected response: ${exception.getMessage}") |
| 57 | + exception.printStackTrace() |
| 58 | + Failure(new RuntimeException(s"Unexpected response, please try again in a minute")) |
| 59 | + case Success(value) => |
| 60 | + Failure(new RuntimeException(value.error)) |
| 61 | + } |
| 62 | + } |
| 63 | + .map { t => |
| 64 | + t.map(Json.parse).map(_.as[R]) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + class DefaultImpl(config: Config)(implicit |
| 69 | + backend: SttpBackend[Future, _], |
| 70 | + ec: ExecutionContext |
| 71 | + ) extends AdminDataExplorerApiClient { |
| 72 | + |
| 73 | + private val ServerAPI = sttp.model.Uri |
| 74 | + .parse(config.serverUrl) |
| 75 | + .getOrElse(throw new RuntimeException("Invalid server url")) |
| 76 | + |
| 77 | + private def prepareRequest[R: Reads] = { |
| 78 | + basicRequest |
| 79 | + .contentType(MediaType.ApplicationJson) |
| 80 | + .response(asJson[R]) |
| 81 | + } |
| 82 | + |
| 83 | + override def getTables: Future[AdminGetTables.Response] = { |
| 84 | + val path = ServerAPI.path :+ "admin" :+ "tables" |
| 85 | + val uri = ServerAPI.withPath(path) |
| 86 | + |
| 87 | + prepareRequest[AdminGetTables.Response] |
| 88 | + .get(uri) |
| 89 | + .send(backend) |
| 90 | + .map(_.body) |
| 91 | + .flatMap(Future.fromTry) |
| 92 | + } |
| 93 | + |
| 94 | + override def getTableMetadata( |
| 95 | + tableName: String, |
| 96 | + sort: List[String], |
| 97 | + range: List[Int], |
| 98 | + filters: String |
| 99 | + ): Future[List[Map[String, String]]] = { |
| 100 | + val path = ServerAPI.path :+ "admin" :+ "tables" :+ tableName |
| 101 | + val parameters: Map[String, String] = Map( |
| 102 | + "sort" -> sort.mkString("[", ",", "]"), |
| 103 | + "range" -> range.mkString("[", ",", "]"), |
| 104 | + "filters" -> filters |
| 105 | + ) |
| 106 | + val uri = ServerAPI |
| 107 | + .withPath(path) |
| 108 | + .addParams(parameters) |
| 109 | + |
| 110 | + prepareRequest[List[Map[String, String]]] |
| 111 | + .get(uri) |
| 112 | + .send(backend) |
| 113 | + .map(_.body) |
| 114 | + .flatMap(Future.fromTry) |
| 115 | + } |
| 116 | + |
| 117 | + override def viewItem(tableName: String, id: String): Future[Map[String, String]] = { |
| 118 | + val path = ServerAPI.path :+ "admin" :+ "tables" :+ tableName :+ id |
| 119 | + val uri = ServerAPI.withPath(path) |
| 120 | + |
| 121 | + prepareRequest[Map[String, String]] |
| 122 | + .get(uri) |
| 123 | + .send(backend) |
| 124 | + .map(_.body) |
| 125 | + .flatMap(Future.fromTry) |
| 126 | + } |
| 127 | + |
| 128 | + override def viewItems(tableName: String, id: List[String]): Future[List[Map[String, String]]] = { |
| 129 | + val path = ServerAPI.path :+ "admin" :+ "tables" :+ tableName |
| 130 | + val primaryKeyParam = Json.toJson(Map("id" -> id)).toString() |
| 131 | + val uri = ServerAPI.withPath(path).withParams(Map("filter" -> primaryKeyParam)) |
| 132 | + prepareRequest[List[Map[String, String]]] |
| 133 | + .get(uri) |
| 134 | + .send(backend) |
| 135 | + .map(_.body) |
| 136 | + .flatMap(Future.fromTry) |
| 137 | + } |
| 138 | + |
| 139 | + override def createItem(tableName: String, request: AdminCreateTable.Request): Future[AdminCreateTable.Response] = { |
| 140 | + val path = ServerAPI.path :+ "admin" :+ "tables" :+ tableName |
| 141 | + val uri = ServerAPI.withPath(path) |
| 142 | + |
| 143 | + prepareRequest[AdminCreateTable.Response] |
| 144 | + .post(uri) |
| 145 | + .body(Json.toJson(request).toString()) |
| 146 | + .send(backend) |
| 147 | + .map(_.body) |
| 148 | + .flatMap(Future.fromTry) |
| 149 | + } |
| 150 | + |
| 151 | + override def updateItem( |
| 152 | + tableName: String, |
| 153 | + id: String, |
| 154 | + request: Map[String, String] |
| 155 | + ): Future[AdminUpdateTable.Response] = { |
| 156 | + val path = ServerAPI.path :+ "admin" :+ "tables" :+ tableName :+ id |
| 157 | + val uri = ServerAPI.withPath(path) |
| 158 | + |
| 159 | + prepareRequest[AdminUpdateTable.Response] |
| 160 | + .put(uri) |
| 161 | + .body(Json.toJson(request).toString()) |
| 162 | + .send(backend) |
| 163 | + .map(_.body) |
| 164 | + .flatMap(Future.fromTry) |
| 165 | + } |
| 166 | + |
| 167 | + override def deleteItem(tableName: String, id: String): Future[AdminDeleteTable.Response] = { |
| 168 | + val path = ServerAPI.path :+ "admin" :+ "tables" :+ tableName :+ id |
| 169 | + val uri = ServerAPI.withPath(path) |
| 170 | + |
| 171 | + prepareRequest[AdminDeleteTable.Response] |
| 172 | + .delete(uri) |
| 173 | + .send(backend) |
| 174 | + .map(_.body) |
| 175 | + .flatMap(Future.fromTry) |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments