How to make one of the REST API parameter as optional

Hi all,

I am using Scala with Lagom and want to make one of the input parameter as optional in the path of the rest Call method. Suppose the service descriptor is as below:

trait CounterService extends Service {

  def getCountsOfRecords(Id: String, count: Int):
                ServiceCall[NotUsed, JsValue]

  override final def descriptor = {		
    import Service._
    named("Counts")
    .withCalls(
      restCall(GET,
        "/userId/:id/topcounts/:count", getCountsOfRecords _),
      restCall(GET,
        "/userId/:id/topcounts", getCountsOfRecords _),
    )
    .withAutoAcl(true)
  }
}

Here, I want to use the the same handler for two different purposes,

  1. Show top 10 records by default
  2. Show top of records

For example:

  1. /userId/12/topcounts - Should return default top 10 records
  2. /userId/12/topcounts/20 - Should return top 20 records

Could you please suggest me the way I should do it?

Thanks in advance.

Could anyone please give your inputs.

Thanks.

Changing to:

def getCountsOfRecords(Id: String, count: Option[Int]): ServiceCall[NotUsed, JsValue]```

will definitely work with `?counts` style, and I suspect also with `:count` style.

It has been quite some time since I tried, but IIRC, supplying a default for parameters doesn't work, so in your implementation, you can use `count.getOrElse(10)`.