Working with Backbone Events to trigger post with different methods appended to Collections URL - rest

I am working with a handful of RESTFul API's that are as follows in concept.
domain.com/api/?id=xxxxx as the getter API call for the model/collection
In this case for me though there is several POST based setters that look like.
domain.com/api/start?id=xxxxx
domain.com/api/stop?id=xxxxx
domain.com/api/method?id=xxxxx
domain.com/api/thing1?id=xxxxx
domain.com/api/thing2?id=xxxxx
So I am trying to figure out how to trigger a POST based on the collection/model URL but appending start, stop, method, thing1, thing2 to it.. along with the id= as depicted.

Related

Swift Vapor 3 calling a router from inside another

I’ve a server application with Vapor 3, I set up three models Book, Author, Publisher, relative routers and controllers with CRUD and relationships (both Author and Publisher have a Sibling relationship with book).
All would be fine was not for the incoming data: the Json I receive has a different data structure, where the publishers and authors are stored in arrays inside the book.
I cannot decode on Book.self because I would lose the data relevant to publishers and authors, so I created BookTotal, which has no representation on the database (no migration) so I could
req.content.decode(BookTotal.self).flatMap
At this point I would like to call the save or update functions on the Publisher and Author controllers passing the BookTotal.Publisher and BookTotal.Author array.
But, while I can reach the methods either with something like
`PublisherController.Save()`
Or
let client = req.make(Client.self)
let response = client(“path/for/the/router”)
I don’t know how to pass them the data. The CRUD methods inside the controllers have Request as parameter but I don’t know how to build one.
let newReq = Request()
newReq.content = BookTotal.Author
Would be my guess, but Request wants a Container and I don’t know how I should go about that.
I searched around but couldn't find any example/tutorial on such issue.
Am I even on the right path with this?
So you have a couple of options to achieve what you want to do. One option is to rewrite your request handlers in your controllers to accept that data using one of the helper functions. So
func savePublisher(_ req: Request, data: Publisher) throws -> Future<Publisher>
You can then change router.post("your", "url", use: savePublisher) to router.post(Publisher.self, at: "your", "url", use: savePublisher). Then if your code you can call that method and pass the data through as needed.
A (potentially) better way would be to extract the code that saves a publisher out into a separate function which you can then call from your savePublisher route and your bookTotal route. For basic routes this is likely to be the same as the first option, but for most complex routes this is the way to go.

Spring Data Rest does not support post for item resource according to docs, but it does?

https://docs.spring.io/spring-data/rest/docs/3.2.1.RELEASE/reference/html/#repository-resources.item-resource
According to that, the only methods supported for a single item resource are GET, PUT, PATCH, and DELETE.
But I created a simple resource with an example project and it accepts POST for a single resource. Why?
It shouldn't. The RepositoryEntityController class is responsible for handling the repository endpoints, and there is no POST endpoit for BASE_MAPPING + "/{id}" in it.
Are you sure you didn't create any custom endpoints?
I had a similar problem using MongoRepository: on a single item resource, POST operations returned 200, although the body showed an error message.
I solved the problem by removing getter and setter methods from the id field.

Multiple payloads in MVC core rest api

I am developing a rest api on .Net core 2.2 following MVC pattern.
I have a controller with a post method like this...
// POST: api/Todo
[HttpPost]
public async Task<ActionResult<TodoItem>> PostTodoItem(string param, [FromBody] TodoItem item)
{
// some work...
return CreatedAtAction(nameof(GetTodoItem), new { id = item.Id }, item);
}
And it works fine.
The customer asked to have an api on the same route, but the Json body could have 2 different structures, bearing the same data on different schemas.
I considered using
PostTodoItem(string param, [FromBody] Object item)
{
// TryCast item to one of the possible POCO classes then work with the correct one.
}
Do you know a better way, maybe with some advanced routing and filtering option?
This is not really possible nor desirable. Pretty much the core tenant of REST is a URI uniquely represents a particular resource. If you've got a URI like POST /todo, then the post body should be a "todo" and it should create a new "todo" based on that. Here, that is a TodoItem, so that is all that should ever be posted.
REST aside, this just won't work. When your action is activated, the modelbinder attempts to bind the post body to the param(s) that the action accepts. It basically just news up whatever type the param is, and then attempts to find something from the post body to bind to the various properties on that type. This is an intentionally simplistic description of what's happening; the important part is that the type of the param informs how the post body is bound. If you bind to an object (which has no members) or even a base type, then the only members of the post body that will be bound are those that are present on that type, not derived types thereof. Anything that cannot be bound is discarded.
Long and short, you need a unique route for each type of thing you're working with. Under the hood, you can share or otherwise reuse code by factoring out common functionality into private methods, employing inheritance, etc., but you need a distinct action and route to handle each case.

Can you add an object that is available in the request context only?

In Spring (java) and in .NET also, you can add an object in request scope?
i.e. a user makes a request, you perform some look in a filter or base controller, and then you can add this object into the request object for this current request only
Now in your Action you can check if the key exists and use this object in your action method.
In Play 2 Requests are immutable so you would generally wrap them and pass them around rather than modifying them.
You'd usually use something called action composition to do what you want. Action composition lets you write common code for actions so that you can pre-process a requests and maybe pass the action some data from the request.
Check out the Authenticated example in the docs which provides the action with an AuthenticatedRequest object. The AuthenticatedRequest object wraps the existing Request (rather than modifying it) and adds an additional username value.
As Rich said, you can use Action Composition and, if you want to put more information in Request context, you can use Http.current().args.put("key","value").

Using different delegates for NSXmlParser

I am trying to figure out the best way to design something. I am writing an iPhone App and for the most part I am using async calls to a web service. This means that I cam setting up a URLConnection, calling start and letting it call me back when the data is available or an exception occurs. This works well and I think is the correct way to handle things.
For example:
I request a list of people from a web service. The resulting list is Xml Person elements which will be translated into an objective-c "Person" object by my XmlDelegate.
When I call the function to get the person, I pass in a "PersonResultDelegate", which is a protocol with a single function called "PersonReceived:(Person *)p". So, each time I get a complete Person object, I call that method and all is well. So, my detail view (or search result view) just receives the elements as they are available.
The problem comes when I need to obtain more then one specific object. In my specific case, I need to get the first and last appointment for a person. So, I need to make two API calls to obtain these two single Appointment objects. Each Appointment object will result in a call to the registered AppointmentResultDelegate, but how will I know which is the first and which is the last? I also need to somehow handle the case when there is no "first" or "last" Appointments and the Delegate will never get called.
What would be the correct way design wise to handle this? Should I add some additional context information to the initial request which is passed back to the handle in the delegate? An opaque piece of data which only makes sense to the person who made the initial call? What are my other options?
Solution
What I actually ended up doing is just passing an opaque piece of data along with the Appointment to the delegate. So, when I request an appointment object I have a method like:
getNextAppointment withDelegate:self withContext:#"next"
getPrevAppointment withDelegate:self withContext:#"prev"
This way when the delegate gets called I know what appointment is being delivered.
"Each Appointment object will result in a call to the registered AppointmentResultDelegate, but how will I know which is the first and which is the last?"
By looking at the order in which you receive these callbacks. Or by looking at some value in that xml data. Like a sequence or data. I don't know your data of course.