Skip to content

Resource API

Benjamin Dupont edited this page Dec 11, 2019 · 3 revisions

Resource API

Resources in Blogify are fetched, updated, created and deleted through a single interface, which abstracts away all the logic

What is a Resource ?

A Resource represents a data model used by Blogify. They are implemented as Kotlin data classes, for example here is an example Resource:

data class Person (
    val firstName: String,
    val lastName: String,
    override val uuid: UUID = UUID.randomUUID() /* temporary */
): Resource(uuid)

Resources all have a number of things in common, we will explore those in the coming paragraphs.

All resources have a UUID

This is pretty self-explanatory.

You can generate a DTO from any resource

Blogify has an included mechanism for generating a blueprint for producing DTOs for client-to-server transfer, internally called a PropMap for property map.

Every PropMap stores a list of PropMap.PropertyHandle objects, which have three possible values: Ok, Computed or AccessDenied. Ok and Computed (both of which implement PropertyHandle.Valid) represent a property that can be accessed on an object of the class the PropMap was generated from, while AccessDenied is generated for a property that can't be read on that object.

A PropMap can be obtained in two ways :

val personPropMap = Person::class.cachedPropMap() // Method #1
val personPropMap = personObject.cachedPropMap() // Method #2

To generate a DTO from that PropMap, two methods are available :

  • Resource::sanitize(), which returns a Map<String, Any?> of all properties obtained from handles that implement PropertyHandle.Valid
  • Resource::slice(selectedPropertyNames: Set<String>), which returns a Map<String, Any?> of all properties obtained from the handles with names specified in selectedPropertyNames which implement Valid
    • Note - if a requested property is marked as AccessDenied or is not found, its name will be included in the _accessDenied and _notFound fields of the DTO, respectively.

CRUD operations with resources

The Service<R : Resource> class provides abstractions for CRUD operations on resources of its type R. Read the provided KDoc for more information

Note - ALL operations using Service return an Sr (SuspendableResult) object. Please see documentation on the Result library for more information