How to filter REST API JSON result by passing params - rest

I'm trying to consume JIRA 2 API and trying to get custom fields. I want to further filter by passing appropriate criteria in URI itself. Current query I'm using is something similar to this:
http://localhost:8522/jira522/rest/api/2/issue/createmeta?expand=projects.issuetypes.fields
The result I'm getting from above request is about 2000 lines.. How can I further filter to get only Custom_fields and also under custom fields I need to only the ones which are required?
I'm pretty new to REST API. Please guide me If anything is wrong... TIA. I spent a lot of time browsing but don't know what exactly I need to search for or where exactly I need to get started.

You can use another queryParam just like expand and add further filtering or pagination.
http://localhost:8522/jira522/rest/api/2/issue/createmeta?expand=projects.issuetypes.fields&limit=1000

Related

Can regex be used to query a REST API?

There is a free test REST API at https://jsonplaceholder.typicode.com/comments
I've figured out that I can get certain objects by specifying their id like this.
https://jsonplaceholder.typicode.com/comments?id=1
I can also do the same for email. Are more complicated queries possible? What if I wanted to get all objects with ids 1-10, or all objects with a certain word in the body? Could I use something like a logical OR to get all objects with either foo in the name or bar in the body? Can regex be used?
you can use https://jsonplaceholder.typicode.com/comments to get all comments and then filter them as you need. axios only takes a url string. So, yes, you can use regex to build that string.Though you can't get object with id 1-10 or use email instead of id if the API doesn't support it.If the API supports it, you can do it.

REST API Get single latest resource

I'm designing a REST api and interested if anyone can help with best practice in the following scenario.
I have...
GET Customers/{customerId}/Orders - to get all customer orders
GET Customers/{customerId}/Orders/{orderId} - to get a particular order
I need to provide the ability to get a customers most recent order. What is best practice in this scenario? Simply get all and sort by date or provide a specific method?
I need to provide the ability to get a customers most recent order.
Of course you could provide query parameters to filter, sort and slice the orders collection, but why not making it simpler and give the latest order if the client needs it?
You could use something like (returning a representation of a single order):
GET /customers/{customerId}/orders/latest
The above URL will map an order that will change over the time and it's perfectly fine.
Say there is also a case where you need last 5 orders. How would your route(s) look like?
The above approach focus on the ability to get a customers most recent order requirement. If returning the last 5 orders requirement eventually comes up after some time, I would probably introduce another mapping such as /recent that returns a representation of a collection with the recent orders and accepts a query parameter that indicates the amount of orders to be returned (5 would be the default value if the parameter is omitted).
The /latest mapping would still be valid and would return a representation of the very latest order only.
Providing query parameters to filter, sort and slice the orders collection is still a valid approach.
The key is: If you know the client who will consume the API, target it to their needs. Otherwise, make it more generic. And when modifying the API, be careful with breaking changes and versioning the API is also welcome.
I think there is no need for another route.
Pass something like &order=-created_at&limit=1 in your get request
Or &order=created_at&orderby=DESC&limit=1 (note I'm not sure about naming your params so maybe you could use &count=1 instead of &limit=1, ditto order params)
I think it also depends whether you are using pagination or not on that route, so perhaps additional params are required
Customers/{customerId}/Orders?order=-created_at&limit=1
The Github API for the similar use case is using latest, to fetch the single resource which is latest.
https://docs.github.com/en/rest/reference/repos#get-the-latest-release
So to fetch a single resource which is latest you can use.
GET /customers/{customerId}/orders/latest
However would like to know what community think about this.
IMO the resource/latest gives an impression that the response will be a list of resource sorted by latest to oldest.

REST: Filter primary resource by properties on related resource

I'm looking for some guidance/advice/input on the concept of filtering resources when making a REST API call. Let's say I have Users and Posts, and a User creates a Post. If I want to get all Posts, I might have a route as follows:
GET /api/posts
Now if I wanted to get all posts that were created after a certain date, I might add a filter parameter like so
GET /api/posts?created_after=2017-09-01
However, let's say I want to get all posts by Users that were created after a certain date. Is this the right format?
GET /api/posts?user.created_after=2017-09-01
When it comes to filtering, grouping, etc, I'm having a hard time figuring out the right stuff to do for REST APIs, particularly when using a paginated API. If I do this client side (which was my initial thought) then you potentially end up with a variable number of resources per page, based on what meets your criteria. It seems complicated to add all of this logic as query parameters over the API, but I can't see any other way to do it. Is there a standard for this kind of thing?
There is no objective 'right' way. If using user.created_after logically makes sense in the context of your API, then there's nothing really wrong with it.
Personally, I would not use user.created_after.
I would rather prefer one of the following options:
Option I: /api/posts/users/{userid}?created_after=2017-09-01
Option II: /api/posts/?user={userid}&created_after=2017-09-01
The reason is simple: It looks wrong to me to create dynamic query parameters. Instead you can combine the query parameters (Option II) or even define a more specific resource (Option I).
Regarding pagination: the standard approach is something like this: In addition to filter parameters, you define the following parameters: page and pageSize. When constructing the request, client will specify something like page=2&pageSize=25&orderBy=creationDate.
It's important to note that server must always validate the parameters and can potentially ignore or override incorrect parameters (e.g. page doesn't exist, or pageSize is too big may not return an error, but instead returning reasonable output. This really depends on your business case)

Use time range in Ad Insights of Facebook Marketing API

I'm trying to obtain impressions of my ads between two dates
I'm using Graph API Explorer with this path:
act_0123456789/ads?fields=insights{ad_id,ad_name,impressions}
I want to use 'time_range' attribute that we can find it in Marketing API reference but I don't know the syntax. Anyone can help me?
I can't comment on the approach you're using as I have not used it myself. However, here is an alternative approach which I have used with success:
https://graph.facebook.com/v2.5/act_xyz/insights?level=<yourLevel>&fields=ad_id,ad_name,impressions&time_range[since]=2016-02-15&time_range[until]=2016-02-16&limit=25
where <yourLevel> can be one of: ad, adset, campaign
Also note that I am using straight up http requests in java which is why I show you the request itself. Hopefully you can extrapolate to your own solution.
Using the same endpoint you've shown in your question
act_0123456789/ads?fields=insights{ad_id,ad_name,impressions}
The way to specify a time range would be
act_0123456789/ads?fields=insights.time_range({"since":"2017-08-07","until":"2017-08-14"}){ad_id,ad_name,impressions}
(of course, those two dates are provided just as an example)
Easiest way of doing this is
act_0123456789?fields=ads{insights.time_range({"since":"2019-03-03","until":"2019-03-03"}).time_increment(1){impressions}}
Remove .time_increment(1) if you don't want day by day data
you can use {} for subfields like
act_0123456789?fields=campaigns{ads{name,insights,adcreatives{image_url}}}
you can use . and () for parameters like; always make sure you use you use fields only after parameters like this order .(){}
act_0123456789?fields=campaigns.limit(1).time_range({"since":"2019-03-03","until":"2019-03-03"}).time_increment(1).breakdowns(country){ads{name,insights.time_range({"since":"2019-03-03","until":"2019-03-03"}).time_increment(1).breakdowns(country),adcreatives{image_url}}}

SharePoint SOAP service GetListItem with respect to updated date?

Right now I am working on a project to fetch data from a SharePoint list using SOAP API. I tried and successfully fetches the complete list, but now I want to fetch some specific data that is updated after a specific date.
Is this possible to fetch such data using SOAP query. I can see last update filed when I view single item at the bottom. Is this some how possible to use that filed?
Yes you can use the Web Services to do lot of things just like filtering a list result. I don't know which language you use, but with JavaScript you can look at these two frameworks that should help you:
http://aymkdn.github.io/SharepointPlus/ : easy way to create your queries (I created it)
http://spservices.codeplex.com/ : the most popular framework but less easy to use (it's my point of view)
You can also look at the documentation on MSDN (the param to use is query): http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems.aspx
At last found the answer,
The last update date and time can be retrieved from the list column "Modified".
The soap response will have the value in the attribute "ows_Modified".
Muhammad Usman