react-admin support for WordPress API? - wordpress-rest-api

My team is attempting to get react-admin working with the WordPress REST API. We need all the standard functionality you'd expect, including filtering, sorting, and associations.
The issue is that the WordPress REST API follows slightly different conventions than the data providers listed in https://marmelab.com/react-admin/DataProviders.html.
An example inconsistency
react-admin expects Header: X-Total-Count
WordPress uses Header: X-WP-Total
Question
Is there a best practice strategy for composing an existing data provider and only slightly tweaking it to work with the WordPress API? For example, mapping the incoming X-WP-Total header to X-Total-Count?
I appreciate any help on solving this problem, and I'll post any updates for future viewers, thank you.

If anyone else stumbles on this question: I was able to get a connection to the WP-JSON API working with the tutorial code by copying the compiled data provider from ./node_modules/ra-data-json-server/lib/index.js into my project root and changing 4 instances of x-total-count to x-wp-total.

to solve this issue, i have replaced X-WP-Total occurrences by X-Total-Count in WP code base, and it's worked.
Thank you

Related

REST API - how relevant is WRML today?

I'm trying to figure out what the latest best practice is when it comes to REST APIs and finding an elegant way to "tell" the client what the response will look like. I'm no web expert. But I just recently joined a new team and I've noticed that in the client code, they have hardcoded URI to APIs... and anytime the schema of the return data changes, they have to patch their client code.
Trying to find a way to make things more dynamic by:
introducing patterns to "discover" API servers.
looking into HATEOAS.
More than anything else though, what I'm trying improve is having to change the client code each time the logic on the server changes as far as the body of a GET response.
I've been reading this:
https://www.programmableweb.com/news/rest-api-design-put-type-content-type/2011/11/18
And in particular, the following comments stood out to me: (under the WRML heading)
this media type communicates, directly to clients, distinct and
complementary bits of information regarding the content of a message.
The Web Resource Modeling Language (WRML, www.wrml.org) provides this
"pluggable" media type to give rich web applications direct access to
structural information and format serialization code. The media type's
self-descriptive and pluggable design reduces the need for information
to be communicated out-of-band and then hard-coded by client
developers
Questions
is WRML still a thing? this book that i'm reading is from 2011... and I'm assuming a lot has changed since then.
Can I cheaply build my own inhouse solution where we use the Content-Type or some other header to provide the clients with schema information?
can you point me to an example / sample code where someone is using custom values in Content-Type or other Headers to accomplish something similar?
Or if you have any other suggestions I'm all ears.
Thank you.
I don't know much about WRML, but I would look into:
HATEOAS formats like HAL/HAL Forms and Siren, which are self-describing.
JSON-Schema to describe responses and requests (and yes they can be linked from HATEOAS responses).
If you don't want to go the hypermedia route, OpenAPI and RAML
I've been developing Ketting for the last 5 years and HATEOAS has been nothing short of magic lately as all the tools have been falling into place.

How to document http error codes using spring-restdocs

Is there a way to document http error codes using spring-restdocs? I did not find anything about the same on http://docs.spring.io/spring-restdocs/docs/current/reference/html5/
Any help with an example will be greatly appreciated.
Regards,
Surya
Spring REST Docs doesn't provide anything to help you document HTTP error codes. The theory is that it isn't really necessary to document error codes, and certainly not on every single resource in the API.
The HTTP spec provides definitions for its error codes and your API should adhere to the definitions. This allows users of the API to use their existing knowledge of HTTP, rather than having to learn something new that's specific to your API. If you aren't sure that your users will already have this knowledge, you could include some information about HTTP error codes in an overview section of the documentation. This is the approach taken in one of Spring REST Docs' sample projects. Both the Asciidoctor source and resulting HTML documentation are available online .

REST API including/filtering additional fields/resources/collections

I'm developing a REST API using Zend Framework 1.12.3. It would be useful having a param to be able to return additional information to be returned (other collections/resources or parent/child resources).
E.g. http://api.example.com/project/:id?include=Company,Company.users
where Company, Company.users represent the company to which the project is assigned to and the users of that company, and therefore these two collections are included in the response that otherwise wouldn't have been included.
I've read briefly about this (Limiting which fields are returned by the API here http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api#limiting-fields), but I would like to know more.
Are there any guidelines for best practices regarding this issue? Do you have any example regrading this?
Have you ever faced a similar issue (including fields, filtering fields, etc), and how did you solve it?
Some examples of syntax used for field expansion/field selection in the wild:
Facebook's field expansion. I quite like the syntax here that allows pagination of sub resources.
LinkedIn's field selectors
We wanted to provide similar functionality on our APIs, and have gone with similar syntax to the Facebook field expansion. Unfortunately there is not much in the way of of libraries to help with this, so we have been rolling our own implementation. (There is yoga for Java, and shona for scala is in early stages, but neither of those help you with PHP).

How to start with writing a RESTful service?

I am trying to create a RESTful web service that accepts JSON arguments and gives out a JSON response.
What I want is to accept HTTP requests made to my URL endpoint.
Something like,
POST /the/endpoint HTTP/1.1
Host: mywebsite.com
{"name":"yourname", "department":"your_department"}
Do a DB read at the backend and give relevant parameters like, say Manager name, salary etc as a JSON object, as the response.
What's the best way to go about it? I was thinking of using Java servlets for this? Is there a better way?
PS - I am just getting started so detailed answers or links to tutorials as to how to implement it would be much appreciated.
Thanks.
Yes you can easily do this with Servlets and some Json Libs for Marshalling /unmarshalling the Json Object to Java Object.
You can make use of Json libs like
Jackson ,
Gson etc
But you must know that REST application doesnt end with just handling the request and response , but it needs to take care of other non-functional requirements like
Authentication
Authorization
Security etc
Building this from a scratch from a Servlet is overkill and waste of time when there are ready made frameworks that these things for you
My favorite is Spring MVC 3.0
Check their project site for more details
Just to show you how easy to set up one in Spring MVC , check this below tutorial
Spring 3 REST Tutorial
Pls rate the post if it helps , Cheers.
If you want to go with Java, I suggest that you take a look at JAX-RS... And since REST is a complex topic, here is a url with tons of informations on it. http://code.google.com/p/implementing-rest/
As a complete beginner, I believe the best way to implement a (nearly) RESTful API without having to read a lot is simply to implement the API just using HTML pages and HTML forms with the back-end processing to handle them.
The rules are:
Use <a> tags to provide links to related resources. (navigation)
Use <form> tags to initiate any kind of processing operation on the server. (actions)
You can then make it properly RESTful by using progressive enhancement to add Javascript AJAX requests that perform PUT, PATCH, and DELETE instead of using POST for those three (of course, keeping POST for creating resources where the client doesn't know the resultant URI).
You can then click around and test the API in a web browser! Tools like Selenium can automate this.
If you need to provide JSON, this can be added after the API has been designed and tested, although libraries exist to process HTML or XHTML responses too, so JSON isn't necessarily required for machine readability.
if you are using php with symfony try:https://github.com/FriendsOfSymfony/FOSRestBundle this lets you create a real REST full servicer very quick.
Vogella made my day very easy when i started Web services with an super example here with eclipse screen shots ..Have a look here.

Problems understanding the URL implementation of a REST Proxy in ExtJS 4

I am a newbie with ExtJS 4. I am having problems understanding the implmentation for the "/" syntax for the URL in a Rest Proxy.
For example when using a REST type Proxy the URL config in many examples is just "/users".
I'm confused by this and can't seem to understand it's implmentation. I'm expecting to see an actual executable ".php" or such URL which I have used successfully in an Ajax Proxy. In other examples I see "app.php/users". I understand the php file which I expect but the "/users" part I don't understand. I've spent a few hours trying to find the answer for this but nothing I found has defined it for me.
I'm sure the explanation is very simple.
Thank You in Advance.
For the full explanation you should study up on REST.
Something like /users is standard usage for a REST HTTP request, it is not just ExtJS implementation. Specifically to answer your question, the reason no file extension is included is to denote that this is a logical url, i.e. there is not a physical users.xml file necessarily, instead it is dynamically generated using RESTful webservices.
In other words, you would need to set-up a RESTful webservice architecture to use this ExtJS proxy configuration.
The link above and this tutorial on implementing RESTful webservices with Java helped me alot.
EDIT:
I found a better tutorial (I think) here, also showing implementations in something other than Java.