wvteijlingen / Spine

A Swift library for working with JSON:API APIs. It supports mapping to custom model classes, fetching, advanced querying, linking and persisting.
MIT License
264 stars 109 forks source link

Ability to get meta data on save() #166

Closed PanagiotisVakalis closed 7 years ago

PanagiotisVakalis commented 7 years ago

Our server sends a new token once the current token has been expired. This is being received through the JSON in meta, which is being returned by the server. I can retrieve the new token through meta during the find - GET request. I want to do the same on the save - POST / PATCH. I can see the new token through the debug, but I cannot get it in the onSuccess closure.

I have a method called setupSpine(withContentTypeHeader: acceptHeader: andAccepterHeader: ) which returns a Spine object.

When I am saving a resource I use

setupSpine(withContentTypeHeader: "contentType", andAcceptedHeader: "accepted").save(resource).onSuccess { (resource) in
    // Manipulate the returned resource
}

I would like to have the ability to get the meta as I do in the find operation

setupSpine(withContentTypeHeader: "contentType", acceptHeader: "accept").find(query).onSuccess { (resources, meta, jsonapi) in
    print("Meta: \(meta)")
}
PanagiotisVakalis commented 7 years ago

I managed to overcome this issue by creating a new function in the Spine class: saveWithMeta<T: Resource>(_ resource: T) -> Future<(resource: T, meta: Metadata?, jsonapi: JSONAPIData?), SpineError>. Using this function I can now use any top level meta which I am getting from the server.

@wvteijlingen what do you think about this approach?