what is the REST url endpoint standard for download - rest

I know it is HTTP GET rest type to get all or a particular resource details in REST webservices. What if i need to create a rest webservice which downloads a list of employee (filtered by search criteria) details into a file? It must be a GET call but how does the endpoint URL will look like?
//baseurl/employee/download?q=searchParam
Is this correct way to having my endpoint URL?

There is no specific 'download' concept in REST. Normally you have a collection resource
GET /baseurl/employee
that returns a lis of employyes:
[
{
"id": 123,
"firstName": "John",
"lastName": "Doe"
},
{
"id": 456,
"firstName": "Jahn",
"lastName": "Spencer"
}
]
You can filter this list using a query parameter:
GET /baseurl/employee?firstName=John
[
{
"id": 123,
"firstName": "John",
"lastName": "Doe"
}
]
That's it. The client can do what he wants with this response.
If the server supports multiple representations like JSON and XML, the client can request the representation he wants.

Related

Response tag with URL parameter

Is there any way in an Insomnia request to use the response body of a sub-request in the request body, and at the same time specify a URL parameter for that sub-request?
Consider an API with endpoints like these:
[GET] _.base_url/customers/{id}
[GET] _.base_url/products/{id}
[POST] _.base_url/invoices
When POSTing to the invoices endpoint, the API expects a JSON body along these lines (highly simplified, of course):
{
"date": "2022-03-31",
"currency": "USD",
"customer": {
"id": 123,
"name": "Something Corp.",
"address": "Here, there and everywhere"
},
"items": [
{
"id": 456,
"description": "Thingamajig",
"price": 11500
},
{
"id": 789,
"description": "Doodad",
"price": 23900
}
]
}
That is, it expects the customer field and each of the items fields to be full objects, not just IDs. (And yes, that’s stupid and not RESTful, but I don’t control the API.)
The proper way to retrieve such objects would obviously be to call the two GET endpoints with the IDs as part of the URL, and then using the responses as response tags in the POST request – but I cannot find a way to do this.
There’s a lengthy GitHub discussion on per-request veriables which has so far not produced any results, but even the suggested PR in that thread doesn’t seem like it would support defining the URL parameters when calling the request, rather than in the request itself.
Is there some way to achieve this in Insomnia?

how to divide entities and share it in clean architecture

For my new project in flutter, I am trying to follow the Clean Architecture and diving each different feature in domain, data and presentation`.
Now, I have initiated with the Authentication feature where I have started creating the Entities however, I am pretty much confused and stuck on how to modularize the code based on the Clean Architecture practice.
For example,
The response of my login service is as follow,
so do I need to create entities and models for all JSON nested objects like for response , data , user , accountData and permissions ?
or is there any way to use IResponse to store common elements like status , message and data and then use only for relevant feature.
Not sure whether it is allowed to share entities in between the features. Like user block below can be a feature in Authorization and Employee
{
"status": "success",
"message": "successfully login",
"data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NywiaXNMb2dnZWRJbiI6dHJ1ZSwidGVuYW50SWQiOjEyLCJpYXQiOjE2MjU5OTE5MTAsImV4cCI6MTYyNjA3ODMxMH0.I0z9OIDnQS-MI1ya6usqycoryZ1TBwj3K52BRfrpMuY",
"user": {
"user_id": 7,
"email": "jd#gmail.com",
"first_name": "John",
"last_name": "Doe",
"phone": "",
"date_of_birth": "2015-08-06T00:00:00.000Z",
},
"accountData": {
"name": "Amazon",
"account_id": 1
},
"permissions": [
"ViewAllEmployee",
"AddNewEmployee"
]
}
}

Does Sprint Data Rest support out of the box column queries?

I have a very simple example using Spring Data Rest and JPA which exposes a person resources. When launching the application it works as expected and I can POST and GET instances of the resource.
When sending a GET against /person I get the following response:
"_embedded": {
"person": [
{
"firstName": "FN",
"lastName": "LN",
"_links": {
"self": {
"href": "http://localhost:9090/person/1"
},
"person": {
"href": "http://localhost:9090/person/1"
},
"address": {
"href": "http://localhost:9090/person/1/address"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:9090/person{?page,size,sort}",
"templated": true
},
"profile": {
"href": "http://localhost:9090/profile/person"
},
"search": {
"href": "http://localhost:9090/person/search"
}
},
"page": {
"size": 20,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}
As you can see the person resource has a firstName attribute with a value of FN.
My question is should the following GET query work out of the box?
/person?firstName=FN
or is this something that needs to be implemented with a custom search method?
Needless to say it isn't working for me but I'm seeing conflicting information as to if it is supported out of the box.
Thanks in advance,
My question is should the following GET query work out of the box?
No. You will get an error like
{
"cause": {
"cause": null,
"message": "For input string: \"findByName\""
},
"message": "Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'findByName'; nested exception is java.lang.NumberFormatException: For input string: \"findByName\""
}
Spring Data Rest has the following URL structure.
/{pluralEntityName}/{primaryKey} e.g when the entity name is Person and the primary key is Long it will be /persons/1
We see this error message because Spring Date Rest tries to convert the second argument after the entity name to the primary key of that entity. In my Person entity, the primary key is Long, so It tries to convert findByName to Long and fails.
or is this something that needs to be implemented with a custom search method?
Yes, if you want to do a search on the repository you need to write a method in the repository following Spring Data JPA's method name conventions then Spring Data JPA will automatically convert this method to an SQL query and Spring Data Rest will automatically expose this method as an endpoint.
e.g If you write a method like:
List findByNameContains(String name);
This method will be exposed with Spring Data Rest and you will be able to access it from the following endpoint:
http://localhost:8080/persons/search/findByNameContains?name=Mahsum
Btw, you can see all available search method by visiting http://localhost:8080/persons/search

Facebook graph api: Can i use /search and /{id} in 1 request?

Is it possible to call /search and /{id} in 1 request instead of 2? where id is a result of /search?
If so, would i be limited to the id of the first result, or could i call /{id} for each /search result
This depends on the search type, for example if you search for a User via first/last name like
/search?type=user&q=Mark%20Zuckerberg&fields=id,first_name,last_name,name,link
you can specify the fields parameter with the fields you want to query. The result look like:
{
"data": [
{
"id": "10101640953722381",
"first_name": "Mark",
"last_name": "Zuckerberg",
"name": "Mark Zuckerberg",
"link": "https://www.facebook.com/app_scoped_user_id/10101640953722381/"
},
...
]
}
Keep in mind that you only can access public fields with the search.

Graph API dependant query

Having the facebook username, I need the user ID to access his feed, so right now I'm using this call:
https://graph.facebook.com/john.doe
which returns something like:
{
"id": "612029xxx",
"first_name": "John",
"gender": "male",
"last_name": "Doe",
"link": "https://www.facebook.com/john.doe",
"locale": "en_US",
"name": "John Doe",
"username": "john.doe"
}
there I use the id to get links:
https://graph.facebook.com/612029xxx/links/?access_token=xxx
But I would like to make a batch request with a dependant query to make just one call:
https://graph.facebook.com/batch=[{"method":"GET","name":"do-me-first","relative-url":"?id=john.doe",{"method":"GET","relative-url":"links","depends-on":"do-me-first"}}]&access_token=xxx
but it gives me an error: Cannot query users by their username (john.doe).
Why can I access the user info with the username in the single call, and not in the dependant one??
P.S.: Probably the second query hasn't been properly constructed, but since it is giving me this first error, I couldn't know!