Welcome to the SRP Forum! Please refer to the SRP Forum FAQ post if you have any questions regarding how the forum works.
Ideas on 'overloading' HTTP_RESOURCE_SERVICES
Suppose I want to convert data between the database and the API. For example I want to alter data before exposing it to the public. This means I also have to convert this back, when saving it to the database. In my case; I'd just like to 'protect' certain fields in the record and don't update them at all.
Since BASIC+ isn't OO I was thinking maybe I could implement something that works with callbacks. I'd like to have points in the HTTP_RESOURCE_SERVICE where I can 'break' in the code, make some modifications, and then proceed. So for example before parsing the json data to the ItemRec, I could clear this data and make sure it is skipped.
Any idea's on this? Have people already implemented it? I could create a copy of HTTP_RESOURCE_SERVICES and implement the custom logic for this endpoint there. But if anything changes in resource services, I'd have to do this in every piece of code that was copied from this. I'd rather not have copies of code, I'd rather re-use it.
I'm very curious about other user's point of views.
Since BASIC+ isn't OO I was thinking maybe I could implement something that works with callbacks. I'd like to have points in the HTTP_RESOURCE_SERVICE where I can 'break' in the code, make some modifications, and then proceed. So for example before parsing the json data to the ItemRec, I could clear this data and make sure it is skipped.
Any idea's on this? Have people already implemented it? I could create a copy of HTTP_RESOURCE_SERVICES and implement the custom logic for this endpoint there. But if anything changes in resource services, I'd have to do this in every piece of code that was copied from this. I'd rather not have copies of code, I'd rather re-use it.
I'm very curious about other user's point of views.
Comments
I format the urls like this
api/myresource/resourceid/something
in the endpoint service http_myresource_services, the beginning of the PostItem has a case statement comparing 'something' to determine what it needs to do to the data. Each case has a gosub that manipulates the HTTPPostString as Don suggests and then returns allowing the PostItem code to continue as it would currently be.
Meanwhile I have tried implementing the callback idea. I'm not sure if I will keep on this path, but for this particular problem it seems to work. In HTTP_RESOURCE_SERVICES, the PostDatabaseItem can be fed with a fourth parameter that contains the name of the callback. Then inside the decoding loop, the JSONType is being checked.
I added the callback here:
If CallBack NE '' then call @CallBack('GetJSONType', JSONType, ColumnName) end
This now allowed me to return the type 'SKIP' to JSONType, which I added to the case statement to make sure it does nothing. Then I created a subroutine which implements this service.
I figured I might later on use the same kind of procedure for data conversion.
I guess the downside is that I am editting HTTP_RESOURCE_SERVICES, so updates to the http framework are harder to implement.
In the end, however, I believe that any solution that works for you is a good solution. Time will tell whether you favor it or if you find deficiencies that annoy you in the long run.