In our How do I create a resource? article, we demonstrated three basic methods. The second method uses the GetObject service. It is capable of automatically creating a resource object along with related properties similar to how the GetDatabaseItem service works. However, the GetObject service doesn't have to create the entire resource object. When full automation is not practical, the developer will need a way to add a property to an empty (or partially created) resource object. This article will show how this is done.

It is important to understand that the GetObject service returns a handle to a JSON object. This is useful because it can be shared with other services (referred to as companion services) that use a handle to update a resource object. The AddProperty service will be used to add a simple name/value pair to the resource object like so:

objResource     = HTTP_Resource_Services('GetObject')
If Error_Services('NoError') then
    HTTP_Resource_Services('AddProperty', objResource, 'firstName', 'William')
    HTTP_Resource_Services('AddProperty', objResource, 'lastName', 'Hudson')
    // Serialize the JSON object.
    jsonResource    = HTTP_Resource_Services('GetSerializedResource', objResource)
end

In the above code, the GetObject service is called without any arguments. This simply returns a handle to an empty resource object. It doesn't have to be completely empty, but for the sake of a simple demonstration we'll start with an empty resource object and use the AddProperty service to add two properties: firstName and lastName. The values are hardcoded, although production code will most likely draw from a database row or other digital source. We finish by calling the GetSerializedResource service so that we get a stringified JSON object that we can inspect or use in our API. Here is what our resource object looks like:

{
   "firstName":"William",
   "lastName":"Hudson"
}

Another service that might add a little more convenience is AddProperties. It's a wrapper around the AddProperty service that allows the developer to pass in an @FM delimited list of property names and values into a single call. For example:

objResource     = HTTP_Resource_Services('GetObject')
If Error_Services('NoError') then
    PropertyNames   = 'firstName' : @FM : 'lastName'
    PropertyValues  = 'William'   : @FM : 'Hudson'
    HTTP_Resource_Services('AddProperties', objResource, PropertyNames, PropertyValues)
    // Serialize the JSON object.
    jsonResource    = HTTP_Resource_Services('GetSerializedResource', objResource)
end

Developers will need to decide if the AddProperties service provides enough convenience in lieu of multiple AddProperty calls. One possible beneficial use case is when the values are already stored in an @FM delimited array (such as a database row).

  • No labels