deployd - post method to retrieve object - deployd

Trying to use deployd.com framework for simulating a backend service. I am fairly new to deployd, appreciate any example or pointer in the right direction. Trying to retrieve an object using POST method. It looks like POST is used to create objects.
In my case, POST data contains a few query fields that will be used to query an object and send json back. Is this possible using deployd by modifying onPost method or some other way?
I cannot use GET with query parms due to security restrictions.
here is post request data
[
{
customer:"sam",
city:"noWhere",
}
]
the POST event should query by customer and city, then return matching customer object
[
{
customer:"sam",
postcode:"352345",
city:"noWhere",
country:"US"
}
]

Related

Siesta Service configureTransformer for URL decode models based upon parameters

I have an endpoint which supports post requests. The URL is the same for all requests but the parameters will be different for each request. It is basically a free-form query service when the client can formulate the query and fields that will be returned in the response. I would like to be able to define methods on the service which will represent specific queries and a model for each query. But I am uncertain as to how I would go about configuring the transformer for each "query based" endpoint.
Is there a way to accomplish this or is it best to simply work with a json dictionary?
Thanks...
I think that I found the solution to my problem and it was rather simple. It was just a matter of building the resource and supplying it to configureTransformer.
func getUserIds() -> Request {
let res = resource(endPoint)
.withParam("query", "SELECT id FROM users where status='Active'")
configureTransformer(res) {
try self.jsonDecoder.decode(UserIdResponse.self, from: $0.content)
}
return res.request(.post)
}

How to get item table values using OData Model?

I have a OData model and data is as follows
Here, Under ZRECRUITMENT_TRACERRzSet(1), I have "toCandidates" which is Association and it has item level data.
How can I access it's values in the controller.
I tried using
oModel.getProperty('/ZRECRUITMENT_TRACERRzSet(1)/toCandidates')
But it's not helping.
You need to set the 'expand' property in your binding, so whenever the request is sent to the OData service, the "toCandidates" node will come with the data in the response, and not the URL only. Then your getProperty(...) will give you the data.
Whenever you are binding your data you need to do something like this (for example with ElementBinding):
oMyControl.bindElement({
path: "/ZRECRUITMENT_TRACERRzSet(1)",
parameters: {expand: "toCandidates"}
});

Grails pass object to the view and back again

I have some data that I need to persist through multiple actions within my Grails app. Due to the nature of the data, I would prefer not to store the data in the session. Here is an example of what I would like to do.
class MyController{
def index(){
MyObject object = MyObject.new(params.first, params.second, params.third)
[gspObject:object]
}
def process(){
MyObject object = params.gspObject
//continue from here
}
}
In my GSP if I do
<g:form action="process" params="[gspObject:gspObject]">
Then I get the error
Cannot cast object 'net.package.MyObject#699c14d8' with class 'java.lang.String' to class 'net.package.MyObject'
My question is, If I want to get the object back that I sent to the gsp, how can I get that? Is there some kind of scope that I can save the object in that would be a little safer then session? Is there a way to pass the object into the page itself and pass it back in the next request?
Grails has many layers, but at the bottom you have plain old HTTP just like in any web app. It's a stateless protocol, and you send a text or binary response, and receive text or text + binary requests. But you can't expect to be able to send an arbitrary object to a web browser in HTML and receive it back again in the same state as when you sent it - where is this Java/Groovy JVM object going to be stored in the browser?
You have basically two options. One is to store it at the server, which is less work because it remains as the same object the whole time. The session is a good location because it's coupled to the user, is created on-demand and can automatically time out and be removed, etc. The other is to do what you're trying to do - send it to the client and receive it back - but you are going to have to serialize it from an object (which could be a complex object containing arbitrarily many other objects) and deserialize it from the format you used on the client back into Java/Groovy objects.
JSON is a good option for serialization/marshalling. You could store the stringified object in a hidden form element if your page uses a form, or in a querystring arg if you click a link from this page to the next in the workflow. Don't send all of the object's data though, only what you need to rebuild it. Anything that's available in the database should be referenced by id and reloaded.
Something like
[gspObject: object as JSON]
or
[gspObject: [first: object.first, first: object.firstsecond, ...] as JSON]
will get it in the correct format for sending, and then you can parse the JSON from the request to reinstantiate the instance.

RestKit - use identificationAttributes that are not part of the response

In RestKit is it possible to use identificationAttributes that are actually not part of the JSON response?
My case is the following - I have a service that lists all articles for the currently logged-in user like http://example.com/json/articles.json
My problem is the following - since the application allows multiple users to login, I keep the articles in the database together with the userId for each article. If I set the articleMapping.identificationattributes = #["articleId"], then I have a problem if two users using the device have the same article - it will be overwritten regardless of the userId, because it is not part of the response.
To sum up the facts:
For the JSON request I do not send the userId, it is part of the
server session only, so I think that I cannot use RKRoute
I do the mapping of the article with the user manually after RestKit mapping.
I do not have the userId property as part of the JSON response, it exists only inside the ArticleManagedObject.
Is there a way to inform RestKit that during the mapping, it should check the articleId+userId combination as an identificator? I tried using identificationPredicate with no success.
EDIT:
An example response from the server, when UserA is logged in:
{
"data":{
"articles":[
{
"articleId":1,
"title":"Objective C Basics"
},
{
"articleId":2,
"title":"Xcode Basics"
}
]
}
}
and here is the response when UserB is logged in:
{
"data":{
"articles":[
{
"articleId":1,
"title":"Objective C Basics"
},
{
"articleId":3,
"title":"Java Basics"
}
]
}
}
If UserA logs in, everything is fine. But if UserB logs in from the same device, then article 1 is mapped to UserB, and from now on, the connection between UserA and article 1 is lost.
As I understand from your suggestion, the only solution is to return also the user id from the service, set RKUnionAssignmentPolicy and let RestKit take care of the mapping (currently I am manually making the mapping between articles and users after RestKit).
Another question that I have - is it possible to set the identificationAttributes or identificationPredicate so that it makes a separation between object article 1 for UserA and object article 1 for UserB.
You currently do the user to article mapping outside RestKit, this is fine, but you will need to modify this process a little.
To begin with, I'm assuming here that the article response is the full set of articles for the user. If not then things get more tricky and you'll need to modify the below to account:
Start by getting all of the existing articles for a user. With this we're going to look at what needs to be removed and what needs to be added.
As we iterate through the articles we have received we can check the existing articles for a match, if we find one we have no work to do. If we don't find a match we need to add the relationship to the existing set, which will be a union with any relationship to any other user.
Next we want to remove the list of new articles from the list of the existing articles to get the list of deletions, for these we just need to break the link, again leaving other users unchanged.

RestKit POSTed managed object becomes duplicate when response is enveloped in an array

When I POST a Core Data managed object using RestKit, RestKit doesn't update the existing object but creates a new object instead.
The returned JSON always contains the newly created object, but wraps it into a plural keyed array. I found that if I change this to just the one object, the updating works as expected. However, I would like to keep the array in the response so that all responses are consistently in the plural form.
Is there any way I can make RestKit update the record, even when returned from the server wrapped in an array?
Possible solution: identificationAttribute?
On my entities I have an identificationAttribute called remoteID. This is the primary unique key of the record. This will be 0 before the POST, because the object is not yet on the server. I thought that by adding a second identificationAttribute called insertionID, setting that before the POST, and then returning it in the response, would allow RK to find the existing entity in the local store. Alas, it didn't work.
What did work however, was setting the remoteID before the POST, to the next auto increment value on the server! What could explain that it works with remoteID, but not with a second insertionID?
Example request
{
"user": {
"email": "example#example.com"
}
}
Response
{
"users": [{
"email": "example#example.com"
}]
}
I would like to keep the array in the response so that all responses are consistently in the plural form.
You shouldn't, because this is an individual request and response, not a composite.
Your idea about identificationAttribute is correct, but doesn't apply when the response is an array. The array is the trigger (or one of the possible triggers) to abandon the match with the source object and create new objects. Changing this would be hard.
Without knowing more about your real situation, 2 alternatives:
Change the JSON
POST a dictionary instead of the real object and then you won't have a duplicate
When you use multiple identification attributes, all must match for the destination object to be found.
Take care - don't create multiple mappings for the same entity with different identification attributes or you will most likely be debugging the lookup cache for a long time trying to work out what's happening...
If the identity matches before the request is made then the array isn't an issue. To explain the above in more detail:
When you POST an object, RestKit expects to get that object back. So, if you POST one object and get an array back it doesn't know what to do, because it can't map an array into an object. So, it tries to lookup based on the identification attribute (if they exist). If the POSTed object didn't have an id and the returned object does then it will never match. If you set it before POSTing then it will match.

Resources