Action linking in REST that will create a resource based on a existing resource - rest

I'm struggling to get action links into my resource to fulfill HATEOAS.
I would like to create a new resource based on an existing resource and still fulfill a sound hypermedia strategy. In my case, append an existing test request on a generic server side job queue.
My current flow (simplified for clarity) for doing this does not use hypermedia to link the action of actually queuing the test_request (creating a job resource on the jobs collection).
Create test_request resource
POST http://example.org/api/v1/test_request
{
'name': 'Some example tests',
'test': 'test1'
}
201, CREATED
{
'id': 3
'name': 'Some example tests',
'test': 'test1',
'links': [
{
'rel': 'self',
'href': 'http://example.org/api/v1/test_request/3',
'method': 'GET',
},
{
'rel': 'remove',
'href': 'http://example.org/api/v1/test_request/3',
'method': 'DELETE',
}]
}
Generate a job resource
POST http://example.org/api/v1/jobs
{
'type': 'test_request',
'id': 3
}
201, CREATED
{
'id': 12,
'type': 'test_request',
'status': 'new',
'links': [
{
'rel': 'self',
'href': 'http://example.org/api/v1/jobs/12',
'method': 'GET',
},
{
'rel': 'remove',
'href': 'http://example.org/api/v1/jobs/12',
'method': 'DELETE',
}]
}
To fulfill Hypermedia criteria on my REST API I'm really tempted to add an action link on the test_request resource so that a new job resource is created for the same.
Modified test_request representation
{
'id': 3
'name': 'Some example tests',
'test': 'test1',
'links': [
{
'rel': 'self',
'href': 'http://example.org/api/v1/test_request/3',
'method': 'GET',
},
{
'rel': 'remove',
'href': 'http://example.org/api/v1/test_request/3',
'method': 'DELETE',
},
{
'rel': 'http://example.org/rels/queue',
'href': 'http://example.org/api/v1/test_request/3/queue',
'method': 'PUT',
}]
}
So, the first examples are (hopefully) RESTful were my second approach with the queue action are maybe more unRESTful. However, it's really nice to have a link in the test_request representation that clearly states an action to make the next transition.
What's best practice here?

Your question isn't super clear, so i'm gonna go with my interpretation of what you're asking, maybe that can help get an answer.
The root question is a domain question that only you can really answer. Can and should a Test exist without being part of a job? You need to think through your use cases and make a decision. If a test is a weak entity, then you should only allow the creation through the jobs.
Regarding your strive for RESTful architecture. I would suggest eliminating the concept of the id field from your API. The self link is the ID. When you create the job you can provide the collection of links to the tests that you want to be included in the job. Something like
POST http://example.org/api/v1/jobs
{
'type': 'test_request',
'links': [
{
rel : "test",
href : "http://example.org/api/v1/test_request/3",
},
{
rel : "test",
href : "http://example.org/api/v1/test_request/4",
}
]
}
Note: i'm unsure how your hypertext format deals with multiple links for the same relationship...i made a guess.
It's my opinion that Primary Keys and Foreign Keys should never exist in a RESTful API. Links should always be used.

Related

Custom module not showing in Odoo 10 (Windows 7, IDE Eclipse)

I have creating a simple module in Odoo10 but its not showing in the Apps list. I upgraded few times but still am unable to view it.
{
'name': 'ABC',
'version': '1.0',
'category': 'Testing',
'description': """Test""",
'depends': ['sale'],
'author': 'xxxx',
'data': [
'views/abc_views.xml',
],
'demo': [],
'installable': True,
'auto_install': False,
}
I dnt know what parameter or thing im missing here.
Hopes for suggestion

How to set fields() in Yii2 Restful API

I would like to list only certain fields in "index" request and list more fields in detail request. For example,
When user calls: http://api.example.com/users, Server returns:
{
'id':1,
'name': "John"
},
{
'id':2,
'name': "Henry"
}
...
When user calls: http://api.example.com/users/1, server returns:
{
'id':1,
'name': "John",
'gender': "M"
'dob': "1995-01-01"
'address': "1 Bay Road"
},
How can I set the fields() or extraFields() function to do that without asking user to add new parameters like "expand=gender,dob,address"?
Thank you.
As the Guide,you could use like this:
http://api.example.com/users?fields=id,name
http://api.example.com/users/1?fields=id,name,gender,dob,address

ExtJS5: Get rid of root property in proxy

I'm trying to connect a REST API to my ExtJS application.
For GET /user alike requests I return a response as follows:
{items: [{id: 1, ...}, {id: 2, ....}], total: 2}
So I created a model for that:
Ext.define('model.User', {
extend: 'Ext.data.Model',
fields: [
{ name: 'id', type: 'int' },
{ name: 'name' },
],
proxy: {
reader: {
type: 'json',
totalProperty: 'total',
rootProperty: 'items'
},
type: 'rest',
url: '/Api/User',
}
});
The grids load data and all look perfect. Now I want to be able to request a single record which my api serves as {id: 1, ...}.
But when I do model.User.load(1) the success handler is never triggered because the response doesn't contain items property. If I put my record in that property, it will work but also will look ugly for other API users.
How can I make it work without the root property? I can't find any events for proxy/reader on a model to change it dynamically.
The rootProperty can also be a function, so you could do something like:
rootProperty: function(raw) {
return raw.items ? raw.items : raw;
}

jsTree ui plugin not responding to configs, can't enforce select_limit

I can't get any of the ui configs to be responded to. The one that's really irking me is select_limit. This is my tree:
$('#jstree_demo_div').jstree({ 'core': {
'data': [
'Simple root node',
{
'text': 'Root node 2',
'state': {
'opened': true,
'selected': true
},
'children': [
{ 'text': 'Child 1' },
'Child 2'
]
}
],
'ui': {
'select_limit': 1,
"select_multiple_modifier": "alt"
},
'plugins': ['ui']
} });
My tree displays just fine and I can interact with it, I just can't for the life of me to get select_limit to be adhered to. Similarly, select_multiple_modifier isn't getting adhered to either. I must be doing some setup piece wrong, can anyone help?
Thanks!!!
I've answered my own question...jsTree the select_limit feature is no longer supported, and nobody has bothered to update the documentation. There are probably plenty of features that are no longer supported.
Summary: Out of date documentation. Use jsTree at your own risk.
You can use multiple item of the core:
"core": {
"multiple": false
}

Extjs 4.2 Proxy Rest id parameter

I am trying to access a REST service using extjs proxy rest, but the url that is being sent looks weird, take a look:
/rest/v1/distribution-list/1*?id=1*
I dont know why 'id' is being sent.
It shoul send '/rest/v1/distribution-list/1'
Any ideas?
this is my model
Ext.define('Wave.model.DistributionList', {
extend: 'Ext.data.Model',
fields: [
{name: 'id'},
{name: 'name', type: 'string'},
{name: 'status', type: 'string'}
],
proxy: {
type: 'rest',
noCache: false,
reader: {
type: 'json'
},
writer: {
type: 'json'
},
actionMethods: {
create: 'POST',
read: 'GET', // defaults to GET
update: 'POST',
destroy: 'DELETE'
},
api: {
read: '/rest/v1/distribution-list/',
create: '/rest/v1/distribution-list/',
update: '/rest/v1/distribution-list/',
destroy: '/rest/v1/distribution-list/'
}
}
});
Cheers
-Henrique
the ID have been send is setted by Extjs.you can change that using the idParam to changed to other one;
Working with sencha-touch 2.3.1 and rest proxy, ExtJS creates the URL to the action methods with query string parameters, like you said: /rest/v1/distribution-list/?id=1.
If you don't want to append the id, you can change appendId to false inside proxy config.