-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): Handle correctly PillarsError in API (#86)
- Loading branch information
Showing
15 changed files
with
228 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
@host = http://localhost:9876 | ||
|
||
### | ||
# @name home | ||
GET {{ host }}/ | ||
|
||
### | ||
# @name user list | ||
GET {{ host }}/v0/user | ||
|
||
### | ||
# @name user create | ||
POST {{ host }}/v0/user | ||
|
||
{ | ||
"firstName": "John", | ||
"lastName": "Doe", | ||
"age": 25, | ||
"country": "FR", | ||
"email": "[email protected]" | ||
} | ||
|
||
### | ||
# @name user get | ||
GET {{ host }}/v0/user/1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package example | ||
|
||
import cats.syntax.all.* | ||
import example.codec.json.given | ||
import pillars.PillarsError | ||
import sttp.tapir.* | ||
import sttp.tapir.codec.iron.given | ||
import sttp.tapir.json.circe.jsonBody | ||
|
||
object Endpoints: | ||
val home: Endpoint[Unit, Unit, Unit, String, Any] = endpoint.get.out(stringBody) | ||
|
||
private val base = endpoint.in("v0").errorOut(jsonBody[PillarsError.View]) | ||
private val userBase = base.in("user") | ||
|
||
val createUser: Endpoint[Unit, UserView, PillarsError.View, UserView, Any] = userBase | ||
.in(jsonBody[UserView]) | ||
.post | ||
.out(jsonBody[UserView]) | ||
.name("create user") | ||
|
||
val listUser: Endpoint[Unit, Unit, PillarsError.View, List[UserView], Any] = userBase | ||
.get | ||
.out(jsonBody[List[UserView]]) | ||
.name("list user") | ||
|
||
val getUser: Endpoint[Unit, Email, PillarsError.View, UserView, Any] = userBase | ||
.in(path[Email].name("e-mail")) | ||
.get | ||
.out(jsonBody[UserView]) | ||
.name("get user") | ||
|
||
val deleteUser: Endpoint[Unit, Email, PillarsError.View, UserView, Any] = userBase | ||
.in(path[Email].name("e-mail")) | ||
.delete | ||
.out(jsonBody[UserView]) | ||
.name("delete user") | ||
|
||
val all = List( | ||
home, | ||
createUser, | ||
listUser, | ||
getUser, | ||
deleteUser | ||
) | ||
end Endpoints |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package example | ||
|
||
import io.circe.generic.semiauto.deriveCodec | ||
import io.github.iltotore.iron.* | ||
import io.github.iltotore.iron.circe.given | ||
import sttp.tapir.Schema | ||
import sttp.tapir.codec.iron.given | ||
object codec: | ||
object db: | ||
import skunk.* | ||
import skunk.codec.all.* | ||
|
||
val countryCode: Codec[CountryCode] = varchar(2).eimap(CountryCode.either)(_.value) | ||
val countryName: Codec[CountryName] = varchar.eimap(CountryName.either)(_.value) | ||
val country: Codec[Country] = | ||
(countryCode *: countryName *: text).imap(Country.apply)(c => (c.code, c.name, c.niceName)) | ||
|
||
val firstName: Codec[FirstName] = text.eimap(FirstName.either)(_.value) | ||
val lastName: Codec[LastName] = text.eimap(LastName.either)(_.value) | ||
val email: Codec[Email] = text.eimap(Email.either)(_.value) | ||
val age: Codec[Age] = int4.eimap(Age.either)(_.value) | ||
val user: Codec[User] = | ||
(firstName *: lastName *: email *: age *: country).imap(User.apply)(u => | ||
(u.firstName, u.lastName, u.email, u.age, u.country) | ||
) | ||
end db | ||
|
||
object json: | ||
import io.circe.* | ||
given Codec[UserView] = deriveCodec[UserView] | ||
given Schema[UserView] = Schema.derived | ||
end codec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package example | ||
|
||
import cats.effect.IO | ||
import cats.syntax.all.* | ||
import pillars.Controller | ||
import pillars.Controller.HttpEndpoint | ||
import pillars.Pillars | ||
|
||
final case class HomeController()(using Pillars[IO]) extends Controller[IO]: | ||
def list: HttpEndpoint[IO] = Endpoints.home.serverLogicSuccess: _ => | ||
"👋 Hi".pure[IO] | ||
val endpoints = List(list) | ||
end HomeController | ||
|
||
final case class UserController()(using Pillars[IO]) extends Controller[IO]: | ||
def list: HttpEndpoint[IO] = Endpoints.listUser.serverLogic: _ => | ||
Left(errors.api.NotImplemented.view).pure[IO] | ||
|
||
def create: HttpEndpoint[IO] = Endpoints.createUser.serverLogic: _ => | ||
Left(errors.api.NotImplemented.view).pure[IO] | ||
|
||
def get: HttpEndpoint[IO] = Endpoints.getUser.serverLogic: _ => | ||
Left(errors.api.NotImplemented.view).pure[IO] | ||
|
||
def delete: HttpEndpoint[IO] = Endpoints.deleteUser.serverLogic: _ => | ||
Left(errors.api.NotImplemented.view).pure[IO] | ||
|
||
val endpoints = List(list, create, get, delete) | ||
end UserController |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package example | ||
|
||
import io.github.iltotore.iron.* | ||
import pillars.ApiServer | ||
import pillars.PillarsError | ||
import pillars.PillarsError.* | ||
import sttp.model.StatusCode | ||
|
||
object errors: | ||
enum api(val number: PillarsError.ErrorNumber, override val status: StatusCode, val message: PillarsError.Message) | ||
extends ApiServer.Error: | ||
case NotImplemented extends api(ErrorNumber(1), StatusCode.NotImplemented, Message("Not implemented")) | ||
case NotFound extends api(ErrorNumber(2), StatusCode.NotFound, Message("Not found")) | ||
case AlreadyExists extends api(ErrorNumber(3), StatusCode.Conflict, Message("Already exists")) | ||
end api | ||
end errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package example | ||
|
||
final case class UserView( | ||
firstName: Option[FirstName], | ||
lastName: Option[LastName], | ||
email: Email, | ||
age: Option[Age], | ||
country: Option[CountryCode] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters