...

/

Product Routes - Load and Update Product

Product Routes - Load and Update Product

Learn how to implement tapir and http4s endpoints for loading and updating products and to combine them.

Tapir endpoint for loading a product

Having the basics settled, we can try to write our first endpoint. Let’s refactor our product routes. We will define our endpoints in the companion object of the class.

Press + to interact
//Our type is Endpoint[ProductId, StatusCode, Product, Nothing]
val getProduct=endpoint.get
.in("product" / path[ProductId]("id"))
.errorOut(statusCode)
.out(jsonBody[Product])

So what do we have here? First, we specify the HTTP method by using the get function of the endpoint (Line 2). Now, we need to define our path and inputs. We do this by using the in helper, which accepts path fragments separated by slashes and a path[T] helper, which allows us to extract a type directly from a path fragment (Line 3). This way, we define our entry point product/id, in which id must match our ProductId type.

To be able to be more flexible about our returned status codes, we use the ...

Access this course and 1400+ top-rated courses and projects.