JSON API for Limit Control


#1

Hi,
We are looking to use Json API for an internal service that we need. It’s a Expense limit control. Our services include charging subscribed users periodically certain amounts, but we have the possibility to cap the chargings if they go above configurable limits (for example, monthly, by service, etc). For this we plan to have a running count of expenses by user and some other dimensions: day, month, service, periods, etc. We have some problems wrapping our heads around what would be a correct representation of this for Json API, and specifically how to handle the operation of atomically checking if a new expense would pass any limit to stop it, or add to the current counts if it doesn’t. The actual list of individual expenses doesn’t need to be part of the resource (we are not even storing them in this DB), only the running totals. We have a customer resource that represents a customer, and we were thinking to add a relationship to a resource that represents the running totals. But we are not sure how we would make a call that would allow us to check if a new amount passes the limits, and that updates the running totals. We were toying with the idea of using PATCH passing an amount, this would update the running totals or return a 409 if a limit would be passed.
Any idea?


#2

Or you could make the running total an attribute of the customer.

You could do, or I can see a case for using 402 - payment required or 403 - forbidden.
I could also see a case for using a POST to submit a claim to a collection of claims, with the total tracked in the meta section; but this might be less appropriate since you’re not interested in the individual claims here.
Another option could be to use a POST to create a claim request (i.e. not the actual claim).


#3

A problem that we wanted to avoid is to have to always fill that attribute. There are other use cases that don’t need it, and there’s a cost to retrieve them that we would like to avoid. Is it possible to have a “phantom” attribute that is only returned if explicitly requested?

Example if requested:

{
  "data": {
    "type": "customer",
    "id": "234",
    "attributes": {
        "name": "test",
        "expenses": {
            "monthly": 4.99,
            "daily": 1.99
        }
    },
    "links": {
      "self": "http://localhost:8081/customer/234"
    }
  }
}

If not requested, the expenses attribute would not be present.

What would be the correct call to check and update? Would a PATCH to “self” be ok? What is the consensus regarding the Content-type for PATCH? We’ve been reading several topics on that but there are quite different opinions.

PATCH /customer/234
Content-type: application/vnd.custom…

{“update”:“expenses”, “value”:2.00}

would return a 409 if 2.00 would exceed any of the limits, or 200 (and daily and monthly totals would be updated) if everything is fine.

Sounds good?


#4

You could use meta for your phantom attribute
GET http://localhost:8081/customer/234?meta[expense]

{
  "meta":{
    "expenses": {
       "monthly": 4.99,
       "daily": 1.99
     }
  },
  "data": {
    "type": "customer",
    "id": "234",
    "attributes": {
        "name": "test"
    },
    "links": {
      "self": "http://localhost:8081/customer/234"
    }
  }
}

#5

I believe the spec requires the content type to be JSON API, and no other types are allowed.