Restangular: add prefix to id on all requests - restangular

In my api, I need to specify the id as "id:" + some id, eg /suppliers/id:1234
Is there a way to add this globally so that I can do Restangular.all('someresource').get('1234') as usual in my services (instead of doing Restangular.all('someresource').get('id:' + resourceID) kind of stuff for each one)

I think you can use addFullRequestInterceptor
https://github.com/mgonto/restangular#addfullrequestinterceptor

Related

QLIK Sense - REST api chain call

I need to integrate data in my Qlik Sense project using cloud REST api.
I need to call a chain of API as I firstly need the Token
Basically:
1) "Token" REST passing user+psw getting token
2) "API2" REST passing token received from 1 in the BODY
I think I need to use the data script feature, I'm able to create separately the 2 REST call, but how can I pass tokn dinamically in the Body?
Is there a specific code to be added?
Thx
Find an answer here:
https://community.qlikview.com/thread/224957
Basically just edit and parse Body variable:
let vRequestBody = '{"call":"ListarCategorias","app_key":"XXXXXXXX","app_secret":"XXXXXXXXXX","param":[{"pagina":"$(vPagina)","registros_por_pagina":100,"apenas_importado_api":"N"}]}';
let vRequestBody = replace(vRequestBody,'"', chr(34)&chr(34));
and use this at the end of "RestConnectorMasterTable" default scripting snippet WITH CONNECTION(BODY "$(vRequestBody)"):
RestConnectorMasterTable:
SQL SELECT
"__KEY_root",
(SELECT
"codigo",
"totalizadora",
"transferencia",
"__FK_categoria_cadastro"
FROM "categoria_cadastro" FK "__FK_categoria_cadastro")
FROM JSON (wrap on) "root" PK "__KEY_root"
WITH CONNECTION(BODY "$(vRequestBody)");

REST Resource route naming for get and ResourceByID and ResourceByName

I am trying to write 2 Rest GET methods.
Get user by Id
Get user by userName.
I need to know if there is any resource naming convention for this. Both my id and username are strings.
I came up with:
/api/{v}/users/{userid}
/api/{v}/users/username/{username}
However, 2) doesn't seem correct and if I change 2) to /api/{v}/users/{username}, I am mapping to 1) as both id and username are strings. Or is it considered acceptable to use /api/{v}/userbyName/{username}?
How should I name my resource route in case 2) ?
First of all: https://vimeo.com/17785736 (15 minutes which will solve all your questions)
And what is unique? Is the username unique or only the id or both are unique?
There is a simple rule for all that:
/collection/item
However, 2) doesn't seem correct and if I change 2) to /api/{v}/users/{username}, I am mapping to 1) as both id and username are strings.
If your item can be identified with an id and also with an unique username - it doesn't matter if it's the username or the id - simply look for both (of course your backend needs to handle that) and retrieve it.
According to your needs this would be correct:
/api/{v}/users/{userid}
/api/{v}/users/{username}
but I would choose only to use: /api/{v}/users/{userid} and filter by username only with a query parameter (description for that down there below)
Also will I break any rules if I come up with
/api/{v}/userbyName/{username}
Yes - /api/{v}/userbyName/{username} will break the rule about /collection/item because userByName is clearly not a collection it would be a function - but with a real restful thinking api there is no function in the path.
Another way to get the user by name would be using a filter/query paramter - so the ID will be available for the PathParameter and the username only as filter. which than would look like this:
/api/{v}/users/?username={username}
This also don't break any rules - because the query parameter simply filters the whole collection and retrieves only the one where username = username.
How should I name my resource route in case 2) ?
Your 2) will break a rule - so I can't/won't suggest you a way to do it like this.
Have a look at this: https://vimeo.com/17785736 this simple presentation will help you a lot about understanding rest.
Why would you go this way?
Ever had a look at a javascript framework like - let's say ember. (Ember Rest-Adapter). If you follow the idea described up there and maybe also have a look at the json format used by ember and their rest adapter - you can make your frontend developer speed up their process and save a lot of money + time.
By REST you send back links, which can contain URI templates. For example: /api/{v}/users/{userid} in your case, where v and userid are template variables. Since the URI structure does not matter from a client perspective you can use whatever structure you want. Ofc. it is more convenient to use nice and short URIs, because it is easier to write the routing with them.
According to the URI standard the path contains the hierarchical while the query contains the non-hierarchical part of the URI, but this is just a loose constraint, in practice ppl use both one.
/api/{v}/users/name/{username}
/api/{v}/users/name:{username}
/api/{v}/users?name="{username}"
Ofc. you can use a custom convention, for example I use the following:
I don't use plural resource name by collections
I end collection path with slash
I use slash by reducing a collection to sub-collections or individual items
I don't use slash to give the value of a variable in the path, I use colon instead
I use as few variables and as short URI as I can
I use query by reducing a collection to sub-collections especially by defining complex filters with logical operators
So in you case my solution would be
/api/{v}/user/
/api/{v}/user/name:{username}
/api/{v}/user/{userid}
and
/api/{v}/user/?firstName="John"
/api/{v}/user/?firstName="John|Susan"&birthYear="1980-2005"
or
/api/{v}/user/firstName:John/
/api/{v}/user/firstName:John|Susan/birthYear:1980-2005/
etc...
But that's just my own set of constraints.
Each resource should have a unique URI.
GET /users/7
{
"id": 7,
"username": "jsmith",
"country": "USA"
}
Finding the user(s) that satisfy a certain predicate should be done with query parameters.
GET /users?username=jsmith
[
"/users/7"
]

API Blueprint schema usage

We want to use API blueprint together with a schema. Let's say, we want to specify that PUT to a resource accepts an Account in the payload and GET on the same resource returns an Account payload. So I need to specify that Account is used in GET and PUT and I need to specify the Account itself. I do not know where to specify it, what's the canonical way? Unfortunately I was not able to find it in the examples.
Reusing one message payload in multiple action is where can utilize the concept of a resource model.
Simply define a account model and then reuse it later like so:
# Account [/account]
+ Model (application/json)
+ Body
{ ... }
+ Schema
{ ... }
## Retrieve an Account [GET]
+ Response 200
[Account][]
## Update an Account [PUT]
+ Request
[Account][]
+ Response 204

Order of API endpoint declaration in Nancy Web Framework

I am using NancyFX to host our REST APIs for Web site. We have user table in database, which I would like to update for:
1) Full user update - updates all fields
2) Partial user update - updates only single field
We are using Nancy 0.7 - so currently it does not have PATCH support - I can only use PUT
I have defined my API like
PUT ["/user/{username}"] - for complete update using passed-in user object value
PUT ["/user/{username}/id/{newid}"] - for updating user id only
However, when I call the second API (to update id only) - it never gets trapped by Nancy - and Nancy always call the method to fully update user i.e. PUT ["/user/{username}"]
No matter, what order I declare the APIs, Nancy always call the full user update endpoint only.
Need help, so that I can use both APIs using PUT from our client applications properly.
In general, it is a good idea to UrlEncode any dynamic data components of your URI.
So, in your case:
PUT - /user/xyz#yahoo.com/id/123
would become
PUT - /user/xyz%40yahoo.com/id/123
Nancy will take care of decoding the value for you, so when you extract it from your parameters dynamic object it will be back to xyz#yahoo.com
Found the problem -
It is to do with '#' character in user name - special character.
if username contains '#' character then Nancy never matches the route for
PUT - /user/xyz#yahoo.com/id/123 to
PUT ["/user/{username}/id/{newid}"]
it always matches route for
PUT - /user/xyz#yahoo.com/id/123 to
PUT ["/user/{username}"]

Sales Force Exposing Custom Objects via REST API

I am new to sales force and I have a problem. I would like to manipulate (created,update,delete and select) data from my custom objects using the REST API.
I have managed to get the sample working and it is sending me the data for accounts. Details
Now I would like to do the same for the Custom Object I have created.
I have tried this code but it is not working.
HttpClient httpclient = new HttpClient();
GetMethod get = new GetMethod(instanceUrl + "/services/data/v22.0/sobjects/Employee__c/EC-1000");
get.setRequestHeader("Authorization", "OAuth " + accessToken);
httpclient.executeMethod(get);
System.out.println("Status:" + get.getStatusCode());
System.out.println("Status Text:" + get.getStatusText());
Output is:
Status:404
Status Text:Not Found
I created an object with name employee and ID EC-1000.
The above works for the default objects that is Account.
It works exactly the same way, except you use your custom object's API name instead of the standard object name, e.g. If you have a custom object called Handsets, its api name will be Handsets__c, and you can do a POST to /services/data/v22.0/sobjects/Handsets__c to create a new one.
To access a particular record you need the 18 character record Id, just like for the account (or you need an externalId field setup).