forked from loafle/openapi-generator-original
adjust template folder to lang option (#2959)
* adjust template folder to lang option : scala-finch, java-msf4j, java-undertow, php-ze-ph * Changing file permissions * move ze-ph and finch resources folder
This commit is contained in:
committed by
Akihito Nakano
parent
689534cfd7
commit
6a1f33d2f8
@@ -0,0 +1,137 @@
|
||||
package org.openapitools
|
||||
|
||||
// TODO: properly handle custom imports
|
||||
import java.io._
|
||||
import java.util.UUID
|
||||
import java.time._
|
||||
import com.twitter.finagle.http.exp.Multipart.{FileUpload, InMemoryFileUpload, OnDiskFileUpload}
|
||||
|
||||
import org.openapitools.models._
|
||||
|
||||
trait DataAccessor {
|
||||
// TODO: apiInfo -> apis -> operations = TODO error
|
||||
private object TODO extends CommonError("Not implemented") {
|
||||
def message = "Not implemented"
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A Unit
|
||||
*/
|
||||
def Pet_addPet(body: Pet): Either[CommonError,Unit] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A Unit
|
||||
*/
|
||||
def Pet_deletePet(petId: Long, apiKey: Option[String]): Either[CommonError,Unit] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A Seq[Pet]
|
||||
*/
|
||||
def Pet_findPetsByStatus(status: Seq[String]): Either[CommonError,Seq[Pet]] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A Seq[Pet]
|
||||
*/
|
||||
def Pet_findPetsByTags(tags: Seq[String]): Either[CommonError,Seq[Pet]] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A Pet
|
||||
*/
|
||||
def Pet_getPetById(petId: Long, authParamapi_key: String): Either[CommonError,Pet] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A Unit
|
||||
*/
|
||||
def Pet_updatePet(body: Pet): Either[CommonError,Unit] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A Unit
|
||||
*/
|
||||
def Pet_updatePetWithForm(petId: Long, name: Option[String], status: Option[String]): Either[CommonError,Unit] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A ApiResponse
|
||||
*/
|
||||
def Pet_uploadFile(petId: Long, additionalMetadata: Option[String], file: FileUpload): Either[CommonError,ApiResponse] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A Unit
|
||||
*/
|
||||
def Store_deleteOrder(orderId: String): Either[CommonError,Unit] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A Map[String, Int]
|
||||
*/
|
||||
def Store_getInventory(authParamapi_key: String): Either[CommonError,Map[String, Int]] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A Order
|
||||
*/
|
||||
def Store_getOrderById(orderId: Long): Either[CommonError,Order] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A Order
|
||||
*/
|
||||
def Store_placeOrder(body: Order): Either[CommonError,Order] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A Unit
|
||||
*/
|
||||
def User_createUser(body: User): Either[CommonError,Unit] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A Unit
|
||||
*/
|
||||
def User_createUsersWithArrayInput(body: Seq[User]): Either[CommonError,Unit] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A Unit
|
||||
*/
|
||||
def User_createUsersWithListInput(body: Seq[User]): Either[CommonError,Unit] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A Unit
|
||||
*/
|
||||
def User_deleteUser(username: String): Either[CommonError,Unit] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A User
|
||||
*/
|
||||
def User_getUserByName(username: String): Either[CommonError,User] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A String
|
||||
*/
|
||||
def User_loginUser(username: String, password: String): Either[CommonError,String] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A Unit
|
||||
*/
|
||||
def User_logoutUser(): Either[CommonError,Unit] = Left(TODO)
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A Unit
|
||||
*/
|
||||
def User_updateUser(username: String, body: User): Either[CommonError,Unit] = Left(TODO)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.openapitools
|
||||
|
||||
import io.finch._
|
||||
import io.finch.circe._
|
||||
import io.circe.{Decoder, ObjectEncoder}
|
||||
import io.circe.generic.auto._
|
||||
import io.circe.generic.semiauto
|
||||
import io.circe.generic.semiauto._
|
||||
import io.circe.java8.time._
|
||||
import com.twitter.finagle.Http
|
||||
import com.twitter.finagle.util.LoadService
|
||||
import com.twitter.util.{Await, Future}
|
||||
|
||||
|
||||
class Server {
|
||||
|
||||
// Loads implementation defined in resources/META-INF/services/org.openapitools.DataAccessor
|
||||
val impls: Seq[DataAccessor] = LoadService[DataAccessor]()
|
||||
val db = if (impls.isEmpty) new DataAccessor { } else impls.head
|
||||
|
||||
val service = endpoint.makeService(db)
|
||||
|
||||
val server = Http.serve(":8080", service) //creates service
|
||||
|
||||
def close(): Future[Unit] = {
|
||||
Await.ready(server.close())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Launches the API service when the system is ready.
|
||||
*/
|
||||
object Server extends Server with App {
|
||||
Await.ready(server)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.openapitools
|
||||
|
||||
import com.twitter.finagle.Service
|
||||
import com.twitter.finagle.http.{Request, Response}
|
||||
import com.twitter.finagle.http.exp.Multipart.FileUpload
|
||||
import com.twitter.util.Future
|
||||
import io.finch._, items._
|
||||
import io.circe.{Encoder, Json}
|
||||
import io.finch.circe._
|
||||
import io.circe.generic.semiauto._
|
||||
|
||||
import org.openapitools.apis._
|
||||
|
||||
/**
|
||||
* Provides the paths and endpoints for all the API's public service methods.
|
||||
*/
|
||||
object endpoint {
|
||||
|
||||
def errorToJson(e: Exception): Json = e match {
|
||||
case Error.NotPresent(_) =>
|
||||
Json.obj("error" -> Json.fromString("something_not_present"))
|
||||
case Error.NotParsed(_, _, _) =>
|
||||
Json.obj("error" -> Json.fromString("something_not_parsed"))
|
||||
case Error.NotValid(_, _) =>
|
||||
Json.obj("error" -> Json.fromString("something_not_valid"))
|
||||
case error: CommonError =>
|
||||
Json.obj("error" -> Json.fromString(error.message))
|
||||
}
|
||||
|
||||
implicit val ee: Encoder[Exception] = Encoder.instance {
|
||||
case e: Error => errorToJson(e)
|
||||
case Errors(nel) => Json.arr(nel.toList.map(errorToJson): _*)
|
||||
}
|
||||
|
||||
/**
|
||||
* Compiles together all the endpoints relating to public service methods.
|
||||
*
|
||||
* @return A service that contains all provided endpoints of the API.
|
||||
*/
|
||||
def makeService(da: DataAccessor): Service[Request, Response] = (
|
||||
PetApi.endpoints(da) :+:
|
||||
StoreApi.endpoints(da) :+:
|
||||
UserApi.endpoints(da)
|
||||
).handle({
|
||||
case e: CommonError => NotFound(e)
|
||||
}).toService
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.openapitools
|
||||
|
||||
/**
|
||||
* The parent error from which most API errors extend. Thrown whenever something in the api goes wrong.
|
||||
*/
|
||||
abstract class CommonError(msg: String) extends Exception(msg) {
|
||||
def message: String
|
||||
}
|
||||
|
||||
/**
|
||||
* Thrown when the object given is invalid
|
||||
* @param message An error message
|
||||
*/
|
||||
case class InvalidInput(message: String) extends CommonError(message)
|
||||
|
||||
/**
|
||||
* Thrown when the given object is missing a unique ID.
|
||||
* @param message An error message
|
||||
*/
|
||||
case class MissingIdentifier(message: String) extends CommonError(message)
|
||||
|
||||
/**
|
||||
* Thrown when the given record does not exist in the database.
|
||||
* @param message An error message
|
||||
*/
|
||||
case class RecordNotFound(message: String) extends CommonError(message)
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
package org.openapitools.apis
|
||||
|
||||
import java.io._
|
||||
import org.openapitools._
|
||||
import org.openapitools.models._
|
||||
import org.openapitools.models.ApiResponse
|
||||
import java.io.File
|
||||
import org.openapitools.models.Pet
|
||||
import io.finch.circe._
|
||||
import io.circe.generic.semiauto._
|
||||
import com.twitter.concurrent.AsyncStream
|
||||
import com.twitter.finagle.Service
|
||||
import com.twitter.finagle.Http
|
||||
import com.twitter.finagle.http.{Request, Response}
|
||||
import com.twitter.finagle.http.exp.Multipart.{FileUpload, InMemoryFileUpload, OnDiskFileUpload}
|
||||
import com.twitter.util.Future
|
||||
import com.twitter.io.Buf
|
||||
import io.finch._, items._
|
||||
import java.io.File
|
||||
import java.time._
|
||||
|
||||
object PetApi {
|
||||
/**
|
||||
* Compiles all service endpoints.
|
||||
* @return Bundled compilation of all service endpoints.
|
||||
*/
|
||||
def endpoints(da: DataAccessor) =
|
||||
addPet(da) :+:
|
||||
deletePet(da) :+:
|
||||
findPetsByStatus(da) :+:
|
||||
findPetsByTags(da) :+:
|
||||
getPetById(da) :+:
|
||||
updatePet(da) :+:
|
||||
updatePetWithForm(da) :+:
|
||||
uploadFile(da)
|
||||
|
||||
|
||||
private def checkError(e: CommonError) = e match {
|
||||
case InvalidInput(_) => BadRequest(e)
|
||||
case MissingIdentifier(_) => BadRequest(e)
|
||||
case RecordNotFound(_) => NotFound(e)
|
||||
case _ => InternalServerError(e)
|
||||
}
|
||||
|
||||
implicit class StringOps(s: String) {
|
||||
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
lazy val localformatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
|
||||
lazy val datetimeformatter: DateTimeFormatter =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
|
||||
|
||||
def toLocalDateTime: LocalDateTime = LocalDateTime.parse(s,localformatter)
|
||||
def toZonedDateTime: ZonedDateTime = ZonedDateTime.parse(s, datetimeformatter)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a Unit
|
||||
*/
|
||||
private def addPet(da: DataAccessor): Endpoint[Unit] =
|
||||
post("pet" :: jsonBody[Pet]) { (body: Pet) =>
|
||||
da.Pet_addPet(body) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a Unit
|
||||
*/
|
||||
private def deletePet(da: DataAccessor): Endpoint[Unit] =
|
||||
delete("pet" :: long :: headerOption("api_key")) { (petId: Long, apiKey: Option[String]) =>
|
||||
da.Pet_deletePet(petId, apiKey) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a Seq[Pet]
|
||||
*/
|
||||
private def findPetsByStatus(da: DataAccessor): Endpoint[Seq[Pet]] =
|
||||
get("pet" :: "findByStatus" :: params("status")) { (status: Seq[String]) =>
|
||||
da.Pet_findPetsByStatus(status) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a Seq[Pet]
|
||||
*/
|
||||
private def findPetsByTags(da: DataAccessor): Endpoint[Seq[Pet]] =
|
||||
get("pet" :: "findByTags" :: params("tags")) { (tags: Seq[String]) =>
|
||||
da.Pet_findPetsByTags(tags) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a Pet
|
||||
*/
|
||||
private def getPetById(da: DataAccessor): Endpoint[Pet] =
|
||||
get("pet" :: long :: header("api_key")) { (petId: Long, authParamapi_key: String) =>
|
||||
da.Pet_getPetById(petId, authParamapi_key) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a Unit
|
||||
*/
|
||||
private def updatePet(da: DataAccessor): Endpoint[Unit] =
|
||||
put("pet" :: jsonBody[Pet]) { (body: Pet) =>
|
||||
da.Pet_updatePet(body) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a Unit
|
||||
*/
|
||||
private def updatePetWithForm(da: DataAccessor): Endpoint[Unit] =
|
||||
post("pet" :: long :: paramOption("name") :: paramOption("status")) { (petId: Long, name: Option[String], status: Option[String]) =>
|
||||
da.Pet_updatePetWithForm(petId, name, status) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a ApiResponse
|
||||
*/
|
||||
private def uploadFile(da: DataAccessor): Endpoint[ApiResponse] =
|
||||
post("pet" :: long :: "uploadImage" :: paramOption("additionalMetadata") :: fileUpload("file")) { (petId: Long, additionalMetadata: Option[String], file: FileUpload) =>
|
||||
da.Pet_uploadFile(petId, additionalMetadata, file) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
|
||||
implicit private def fileUploadToFile(fileUpload: FileUpload) : File = {
|
||||
fileUpload match {
|
||||
case upload: InMemoryFileUpload =>
|
||||
bytesToFile(Buf.ByteArray.Owned.extract(upload.content))
|
||||
case upload: OnDiskFileUpload =>
|
||||
upload.content
|
||||
case _ => null
|
||||
}
|
||||
}
|
||||
|
||||
private def bytesToFile(input: Array[Byte]): java.io.File = {
|
||||
val file = File.createTempFile("tmpPetApi", null)
|
||||
val output = new FileOutputStream(file)
|
||||
output.write(input)
|
||||
file
|
||||
}
|
||||
|
||||
// This assists in params(string) application (which must be Seq[A] in parameter list) when the param is used as a List[A] elsewhere.
|
||||
implicit def seqList[A](input: Seq[A]): List[A] = input.toList
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package org.openapitools.apis
|
||||
|
||||
import java.io._
|
||||
import org.openapitools._
|
||||
import org.openapitools.models._
|
||||
import org.openapitools.models.Order
|
||||
import io.finch.circe._
|
||||
import io.circe.generic.semiauto._
|
||||
import com.twitter.concurrent.AsyncStream
|
||||
import com.twitter.finagle.Service
|
||||
import com.twitter.finagle.Http
|
||||
import com.twitter.finagle.http.{Request, Response}
|
||||
import com.twitter.finagle.http.exp.Multipart.{FileUpload, InMemoryFileUpload, OnDiskFileUpload}
|
||||
import com.twitter.util.Future
|
||||
import com.twitter.io.Buf
|
||||
import io.finch._, items._
|
||||
import java.io.File
|
||||
import java.time._
|
||||
|
||||
object StoreApi {
|
||||
/**
|
||||
* Compiles all service endpoints.
|
||||
* @return Bundled compilation of all service endpoints.
|
||||
*/
|
||||
def endpoints(da: DataAccessor) =
|
||||
deleteOrder(da) :+:
|
||||
getInventory(da) :+:
|
||||
getOrderById(da) :+:
|
||||
placeOrder(da)
|
||||
|
||||
|
||||
private def checkError(e: CommonError) = e match {
|
||||
case InvalidInput(_) => BadRequest(e)
|
||||
case MissingIdentifier(_) => BadRequest(e)
|
||||
case RecordNotFound(_) => NotFound(e)
|
||||
case _ => InternalServerError(e)
|
||||
}
|
||||
|
||||
implicit class StringOps(s: String) {
|
||||
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
lazy val localformatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
|
||||
lazy val datetimeformatter: DateTimeFormatter =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
|
||||
|
||||
def toLocalDateTime: LocalDateTime = LocalDateTime.parse(s,localformatter)
|
||||
def toZonedDateTime: ZonedDateTime = ZonedDateTime.parse(s, datetimeformatter)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a Unit
|
||||
*/
|
||||
private def deleteOrder(da: DataAccessor): Endpoint[Unit] =
|
||||
delete("store" :: "order" :: string) { (orderId: String) =>
|
||||
da.Store_deleteOrder(orderId) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a Map[String, Int]
|
||||
*/
|
||||
private def getInventory(da: DataAccessor): Endpoint[Map[String, Int]] =
|
||||
get("store" :: "inventory" :: header("api_key")) { (authParamapi_key: String) =>
|
||||
da.Store_getInventory(authParamapi_key) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a Order
|
||||
*/
|
||||
private def getOrderById(da: DataAccessor): Endpoint[Order] =
|
||||
get("store" :: "order" :: long) { (orderId: Long) =>
|
||||
da.Store_getOrderById(orderId) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a Order
|
||||
*/
|
||||
private def placeOrder(da: DataAccessor): Endpoint[Order] =
|
||||
post("store" :: "order" :: jsonBody[Order]) { (body: Order) =>
|
||||
da.Store_placeOrder(body) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
|
||||
implicit private def fileUploadToFile(fileUpload: FileUpload) : File = {
|
||||
fileUpload match {
|
||||
case upload: InMemoryFileUpload =>
|
||||
bytesToFile(Buf.ByteArray.Owned.extract(upload.content))
|
||||
case upload: OnDiskFileUpload =>
|
||||
upload.content
|
||||
case _ => null
|
||||
}
|
||||
}
|
||||
|
||||
private def bytesToFile(input: Array[Byte]): java.io.File = {
|
||||
val file = File.createTempFile("tmpStoreApi", null)
|
||||
val output = new FileOutputStream(file)
|
||||
output.write(input)
|
||||
file
|
||||
}
|
||||
|
||||
// This assists in params(string) application (which must be Seq[A] in parameter list) when the param is used as a List[A] elsewhere.
|
||||
implicit def seqList[A](input: Seq[A]): List[A] = input.toList
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
package org.openapitools.apis
|
||||
|
||||
import java.io._
|
||||
import org.openapitools._
|
||||
import org.openapitools.models._
|
||||
import scala.collection.immutable.Seq
|
||||
import org.openapitools.models.User
|
||||
import io.finch.circe._
|
||||
import io.circe.generic.semiauto._
|
||||
import com.twitter.concurrent.AsyncStream
|
||||
import com.twitter.finagle.Service
|
||||
import com.twitter.finagle.Http
|
||||
import com.twitter.finagle.http.{Request, Response}
|
||||
import com.twitter.finagle.http.exp.Multipart.{FileUpload, InMemoryFileUpload, OnDiskFileUpload}
|
||||
import com.twitter.util.Future
|
||||
import com.twitter.io.Buf
|
||||
import io.finch._, items._
|
||||
import java.io.File
|
||||
import java.time._
|
||||
|
||||
object UserApi {
|
||||
/**
|
||||
* Compiles all service endpoints.
|
||||
* @return Bundled compilation of all service endpoints.
|
||||
*/
|
||||
def endpoints(da: DataAccessor) =
|
||||
createUser(da) :+:
|
||||
createUsersWithArrayInput(da) :+:
|
||||
createUsersWithListInput(da) :+:
|
||||
deleteUser(da) :+:
|
||||
getUserByName(da) :+:
|
||||
loginUser(da) :+:
|
||||
logoutUser(da) :+:
|
||||
updateUser(da)
|
||||
|
||||
|
||||
private def checkError(e: CommonError) = e match {
|
||||
case InvalidInput(_) => BadRequest(e)
|
||||
case MissingIdentifier(_) => BadRequest(e)
|
||||
case RecordNotFound(_) => NotFound(e)
|
||||
case _ => InternalServerError(e)
|
||||
}
|
||||
|
||||
implicit class StringOps(s: String) {
|
||||
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
lazy val localformatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
|
||||
lazy val datetimeformatter: DateTimeFormatter =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
|
||||
|
||||
def toLocalDateTime: LocalDateTime = LocalDateTime.parse(s,localformatter)
|
||||
def toZonedDateTime: ZonedDateTime = ZonedDateTime.parse(s, datetimeformatter)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a Unit
|
||||
*/
|
||||
private def createUser(da: DataAccessor): Endpoint[Unit] =
|
||||
post("user" :: jsonBody[User]) { (body: User) =>
|
||||
da.User_createUser(body) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a Unit
|
||||
*/
|
||||
private def createUsersWithArrayInput(da: DataAccessor): Endpoint[Unit] =
|
||||
post("user" :: "createWithArray" :: jsonBody[Seq[User]]) { (body: Seq[User]) =>
|
||||
da.User_createUsersWithArrayInput(body) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a Unit
|
||||
*/
|
||||
private def createUsersWithListInput(da: DataAccessor): Endpoint[Unit] =
|
||||
post("user" :: "createWithList" :: jsonBody[Seq[User]]) { (body: Seq[User]) =>
|
||||
da.User_createUsersWithListInput(body) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a Unit
|
||||
*/
|
||||
private def deleteUser(da: DataAccessor): Endpoint[Unit] =
|
||||
delete("user" :: string) { (username: String) =>
|
||||
da.User_deleteUser(username) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a User
|
||||
*/
|
||||
private def getUserByName(da: DataAccessor): Endpoint[User] =
|
||||
get("user" :: string) { (username: String) =>
|
||||
da.User_getUserByName(username) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a String
|
||||
*/
|
||||
private def loginUser(da: DataAccessor): Endpoint[String] =
|
||||
get("user" :: "login" :: param("username") :: param("password")) { (username: String, password: String) =>
|
||||
da.User_loginUser(username, password) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a Unit
|
||||
*/
|
||||
private def logoutUser(da: DataAccessor): Endpoint[Unit] =
|
||||
get("user" :: "logout") { () =>
|
||||
da.User_logoutUser() match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return An endpoint representing a Unit
|
||||
*/
|
||||
private def updateUser(da: DataAccessor): Endpoint[Unit] =
|
||||
put("user" :: string :: jsonBody[User]) { (username: String, body: User) =>
|
||||
da.User_updateUser(username, body) match {
|
||||
case Left(error) => checkError(error)
|
||||
case Right(data) => Ok(data)
|
||||
}
|
||||
} handle {
|
||||
case e: Exception => BadRequest(e)
|
||||
}
|
||||
|
||||
|
||||
implicit private def fileUploadToFile(fileUpload: FileUpload) : File = {
|
||||
fileUpload match {
|
||||
case upload: InMemoryFileUpload =>
|
||||
bytesToFile(Buf.ByteArray.Owned.extract(upload.content))
|
||||
case upload: OnDiskFileUpload =>
|
||||
upload.content
|
||||
case _ => null
|
||||
}
|
||||
}
|
||||
|
||||
private def bytesToFile(input: Array[Byte]): java.io.File = {
|
||||
val file = File.createTempFile("tmpUserApi", null)
|
||||
val output = new FileOutputStream(file)
|
||||
output.write(input)
|
||||
file
|
||||
}
|
||||
|
||||
// This assists in params(string) application (which must be Seq[A] in parameter list) when the param is used as a List[A] elsewhere.
|
||||
implicit def seqList[A](input: Seq[A]): List[A] = input.toList
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.openapitools.models
|
||||
|
||||
import io.circe._
|
||||
import io.finch.circe._
|
||||
import io.circe.generic.semiauto._
|
||||
import io.circe.java8.time._
|
||||
import org.openapitools._
|
||||
|
||||
/**
|
||||
* Describes the result of uploading an image resource
|
||||
* @param code
|
||||
* @param _type
|
||||
* @param message
|
||||
*/
|
||||
case class ApiResponse(code: Option[Int],
|
||||
_type: Option[String],
|
||||
message: Option[String]
|
||||
)
|
||||
|
||||
object ApiResponse {
|
||||
/**
|
||||
* Creates the codec for converting ApiResponse from and to JSON.
|
||||
*/
|
||||
implicit val decoder: Decoder[ApiResponse] = deriveDecoder
|
||||
implicit val encoder: ObjectEncoder[ApiResponse] = deriveEncoder
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.openapitools.models
|
||||
|
||||
import io.circe._
|
||||
import io.finch.circe._
|
||||
import io.circe.generic.semiauto._
|
||||
import io.circe.java8.time._
|
||||
import org.openapitools._
|
||||
|
||||
/**
|
||||
* A category for a pet
|
||||
* @param id
|
||||
* @param name
|
||||
*/
|
||||
case class Category(id: Option[Long],
|
||||
name: Option[String]
|
||||
)
|
||||
|
||||
object Category {
|
||||
/**
|
||||
* Creates the codec for converting Category from and to JSON.
|
||||
*/
|
||||
implicit val decoder: Decoder[Category] = deriveDecoder
|
||||
implicit val encoder: ObjectEncoder[Category] = deriveEncoder
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.openapitools.models
|
||||
|
||||
import io.circe._
|
||||
import io.finch.circe._
|
||||
import io.circe.generic.semiauto._
|
||||
import io.circe.java8.time._
|
||||
import org.openapitools._
|
||||
import java.time.ZonedDateTime
|
||||
|
||||
/**
|
||||
* An order for a pets from the pet store
|
||||
* @param id
|
||||
* @param petId
|
||||
* @param quantity
|
||||
* @param shipDate
|
||||
* @param status Order Status
|
||||
* @param complete
|
||||
*/
|
||||
case class Order(id: Option[Long],
|
||||
petId: Option[Long],
|
||||
quantity: Option[Int],
|
||||
shipDate: Option[ZonedDateTime],
|
||||
status: Option[String],
|
||||
complete: Option[Boolean]
|
||||
)
|
||||
|
||||
object Order {
|
||||
/**
|
||||
* Creates the codec for converting Order from and to JSON.
|
||||
*/
|
||||
implicit val decoder: Decoder[Order] = deriveDecoder
|
||||
implicit val encoder: ObjectEncoder[Order] = deriveEncoder
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.openapitools.models
|
||||
|
||||
import io.circe._
|
||||
import io.finch.circe._
|
||||
import io.circe.generic.semiauto._
|
||||
import io.circe.java8.time._
|
||||
import org.openapitools._
|
||||
import org.openapitools.models.Category
|
||||
import org.openapitools.models.Tag
|
||||
import scala.collection.immutable.Seq
|
||||
|
||||
/**
|
||||
* A pet for sale in the pet store
|
||||
* @param id
|
||||
* @param category
|
||||
* @param name
|
||||
* @param photoUrls
|
||||
* @param tags
|
||||
* @param status pet status in the store
|
||||
*/
|
||||
case class Pet(id: Option[Long],
|
||||
category: Option[Category],
|
||||
name: String,
|
||||
photoUrls: Seq[String],
|
||||
tags: Option[Seq[Tag]],
|
||||
status: Option[String]
|
||||
)
|
||||
|
||||
object Pet {
|
||||
/**
|
||||
* Creates the codec for converting Pet from and to JSON.
|
||||
*/
|
||||
implicit val decoder: Decoder[Pet] = deriveDecoder
|
||||
implicit val encoder: ObjectEncoder[Pet] = deriveEncoder
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.openapitools.models
|
||||
|
||||
import io.circe._
|
||||
import io.finch.circe._
|
||||
import io.circe.generic.semiauto._
|
||||
import io.circe.java8.time._
|
||||
import org.openapitools._
|
||||
|
||||
/**
|
||||
* A tag for a pet
|
||||
* @param id
|
||||
* @param name
|
||||
*/
|
||||
case class Tag(id: Option[Long],
|
||||
name: Option[String]
|
||||
)
|
||||
|
||||
object Tag {
|
||||
/**
|
||||
* Creates the codec for converting Tag from and to JSON.
|
||||
*/
|
||||
implicit val decoder: Decoder[Tag] = deriveDecoder
|
||||
implicit val encoder: ObjectEncoder[Tag] = deriveEncoder
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.openapitools.models
|
||||
|
||||
import io.circe._
|
||||
import io.finch.circe._
|
||||
import io.circe.generic.semiauto._
|
||||
import io.circe.java8.time._
|
||||
import org.openapitools._
|
||||
|
||||
/**
|
||||
* A User who is purchasing from the pet store
|
||||
* @param id
|
||||
* @param username
|
||||
* @param firstName
|
||||
* @param lastName
|
||||
* @param email
|
||||
* @param password
|
||||
* @param phone
|
||||
* @param userStatus User Status
|
||||
*/
|
||||
case class User(id: Option[Long],
|
||||
username: Option[String],
|
||||
firstName: Option[String],
|
||||
lastName: Option[String],
|
||||
email: Option[String],
|
||||
password: Option[String],
|
||||
phone: Option[String],
|
||||
userStatus: Option[Int]
|
||||
)
|
||||
|
||||
object User {
|
||||
/**
|
||||
* Creates the codec for converting User from and to JSON.
|
||||
*/
|
||||
implicit val decoder: Decoder[User] = deriveDecoder
|
||||
implicit val encoder: ObjectEncoder[User] = deriveEncoder
|
||||
}
|
||||
Reference in New Issue
Block a user