Can I access Facebook Ads archive using RestFB? - facebook-ads-api

If using Facebook's graph explorer, I could do https://graph.facebook.com/v14.0/ads_archive followed by the access token and search parameters, and it returns the ads posted by pages in a given country?
Can I do the same type of query using RestFB?
Thanks

Maybe you have to use the JsonObject as return value, but in general: yes:
FacebookClient facebookClient = new DefaultFacebookClient("access token", Version.VERSION_14_0);
JsonObject return = facebookClient.fetchObject("ads_archive", JsonObject.class, Parameter.with("search query parameter", "search query value"));
The access token is added automatically to the request, and you have only to provide the search parameters. If you have more than one parameter, just add them. A parameter list can be handeled by the fetchObject method.
If the result is a connection (the Facebook version of a page-able list) use fetchConnection instead of fetchObject. RestFB takes care of the paging.

Related

How to implement multiple values in single queryparameter in rest api?

eg:
http://host:port/template/products/?productid=1234,1235
how to implement rest api for the above url where in single queryparam passing multiple values and get the all existing records for the productids
Try something like below to accomplish what you are trying to do:
#RequestMapping(value="/products", method=RequestMethod.GET)
public String getMultipleProductsInfo(#RequestParam List<Long> productIdList) {
String productsInfoAsString = getExistingRecordsForTheProductIds(productIdList);
return productsInfoAsString;
}
Notice the
#RequestParam List productIdList
The comma separated list of product IDs are copied into the productIdList list object. You can either iterate through the list or send it over to the method that is responsible for fetching the results from the datasource, such as your database
Please note that the above code snippet returns the response as String. But you could make your API return a response in other formats too, such as JSON.

right way to retrieve query parameters in Spring Boot rest?

I am developing REST api using Spring Boot. I've a controller which accepts POST requests.
http://localhost:8085/carride/end-ride
In the above request i want to access the parameter ride_transection_id for finding particular transection object and also some other value as well.
So basically i have 3 way to do that.
1. i can use #PathVariable
#RequestMapping(value = "/end-ride", method = RequestMethod.POST)
public ResponseEntity<?> endRide(#PathVariable("ride_transection_id") long ride_transection_id,#RequestBody
SomeDTORequest someDTORequest ) {
//find transaction using path varibale
}
2.i can use #RequestParam
#RequestMapping(value = "/end-ride", method = RequestMethod.POST
public #ResponseBody item getitem(#RequestParam("ride_transection_id")
long ride_transection_id,#RequestBody SomeDTORequest someDTORequest ){
//find transaction using RequestParam varibale
}
i can use DTO Object SomeDTORequest and accept ride_transection_id into that with other value as well.
#RequestMapping(value = "/end-ride", method = RequestMethod.POST)
public ResponseEntity<?> endRide(#RequestBody SomeDTORequest someDTORequest ) {
//find transaction using path someDTORequest .getID()
}
i am little bit confuses.just want ask which is safest and right way to access the ride_transection_id ?
thanks
You can use any of them but every way is designed for a certain use.
Path variable:
is used when you need to access an entity using a certain field for example i want to access an order and this order is defined by id so to access this order i need the following request Get /order/{id}
Request Parameter:
when you want to send a specific variable or flag for a certain method
for example Get /orders?is_shipped=true, so this will get all shipped orders or you may need orders at certain page Get /orders?page=1
Request body:
when you need to update the entity by the put or patch request as you will update the entity using the entity's json representation which can be send through the request body
for example PUT /orders/{id}
body: {"title": "order_1"}
then the order with id {id} will be updated with the new title
Spring data rest
See also
Basically, all these 3 methods are fine. But if you want to develop or design RESTful services with best practices, I strongly recommend you should provide the querying service with #PathVariable and GET method such as GET /tickets/12. Otherwise, to digest request body with #RequestBody annotation to retrieve querying criteria for POST method is the second suggestion.
Because POST method is usually to be used for creating something. And for querying something, both #PathVariable and #RequestParam annotations are suitable for GET method. More specifically, #RequestParam is often to be used in Filtering, Sorting and Searching results. For example:
Filtering: GET /tickets?state=open - Here, state is a query parameter that implements a filter.
Sorting: GET /tickets?sort=-priority,created_at - Retrieves a list of tickets in descending order of priority. Within a specific priority, older tickets are ordered first.
Searching: GET /tickets?state=closed&sort=-updated_at - Retrieve recently closed tickets.
Please also refer to this article Best Practices for Designing a Pragmatic RESTful API.
Hope this helps you! :)

Passing parameters to Power BI filter programmatically

In my application I'm displaying a Power BI report. It already works, so there's no problems with showing any report by its ID (guid).
But there are some reports that need to be parametrized, for instance, with current year or person who views the report. That's my question: how to do it?
To be more specific, I'm embedding the report inside HTML <iframe> element. I set iframe URL to an URL received from report definition's embedUrl (received from REST API). I'm controlling it by JavaScript code that calls postMessage().
Report definition:
{
"id":"12345678-6418-4b47-ac7c-f8ac7791a0aa",
"name":"Retail Analysis Sample",
"webUrl":"https://app.powerbi.com/reports/12345678-6418-4b47-ac7c-f8ac7791a0aa",
"embedUrl":"https://app.powerbi.com/reportEmbed?reportId=12345678-6418-4b47-ac7c-f8ac7791a0aa"
}
JavaScript code to loads the report:
function onFrameLoaded() {
var m = {
action: "loadReport",
reportId: reportId,
accessToken: accessToken
};
iframe.contentWindow.postMessage(JSON.stringify(m), "*");
}
Now I feed to filter the report by a parameter from my custom application. Is there a way to send or pass a value to filter dataset in the report?
First of all, filter has to be defined in the report, so user can set it manually.
There are two possible ways to pass parameters (thus set filter) to the Power BI report from external source.
a) In Power BI Application
You can specify the filter by setting filter parameter in the report URL (in browser address bar). Parameter takes custom filter query:
https://app.powerbi.com/groups/me/reports/12345678-6418-4b47-ac7c-f8ac7791a0a7?filter=Store/PostalCode eq '15012'
where "12345678-6418-4b47-ac7c-f8ac7791a0a7" is a report id, and "Store" is a dataset, and PostalCode is a parameter to be filter out. "eq" is a equality operator.
URL should be encoded, so final url looks like this:
https://app.powerbi.com/groups/me/reports/12345678-6418-4b47-ac7c-f8ac7791a0a7?filter=Store/PostalCode%20eq%20%2715012%27
b) JavaScript sendMessage oDataFilter parameter
JavaScript (browser client side) controls the loaded BI report by postMessage() calls with parameters (just like in the question above). There is an extra option oDataFilter that can be set to filter the report.
Set it like this: oDataFilter: "Store/PostalCode eq '15012'"
Full code would look like this:
function onFrameLoaded() {
var m = {
action: "loadReport",
reportId: reportId,
accessToken: accessToken,
oDataFilter: "Store/PostalCode eq '15012'"
};
iframe.contentWindow.postMessage(JSON.stringify(m), "*");
}
Remarks
There must not be any dots in the filter parameters (datasource or parameter name) as the Power BI code rejects it silently as invalid names;
Microsoft created a powerbi-client with which you can do a lot more than just apply one filter. You can apply as many filters as you want and you can also choose default page, default filters, hide filter pane, hide pages navigation, etc.
You can find the client here:
https://microsoft.github.io/PowerBI-JavaScript/
Here is a demo application:
https://microsoft.github.io/PowerBI-JavaScript/demo/index.html
Here is the documentation:
https://github.com/Microsoft/PowerBI-JavaScript/wiki

What is the best way to notify api clients of null-valued properties of returned JSON object?

I have an asp.net web api 2 application that provides data in JSON format for api clients. For GET api methods, an api client programmer who is using Java and C++ languages to call those GET method apis. However, for null-valued properties of JSON objects, the client programmer says he receives "null" (null in-quote string) for those properties. In SQL Server database for nvarchar (string) and datetime columns of different database tables, I save those null-valued columns as null as normal SQL server convention but not "null" string.
My question is what is the best way to let api client programmers know if a null-valued property is null to distinguish it from real "null" string, e.g. {"state": "null"} a literal string. Thanks in advance.
I have many GET method apis which returns null for null-valued properties of JSON objects.
I test my GET Api methods for null properties with Postman or Advanced Rest Client tool, I do not see those tool returns null in "null" (in-quote string) for null-valued properties: (here is state and closeddate properties)
{"firstname":"abc","lastname":"def","state":null,"birthdate":"1992-05-25T00:00:00","closeddate":null}
One of GET method api looks like:
[HttpGet]
public HttpResponseMessage GetUserInfo(int userid)
{
var user = _userService.GetUserInfo(userid);
var statusCode = _userService.StatusCode;
var errorCode = _userService.ErrorCode;
return _statusCode == HttpStatusCode.OK
? Request.CreateResponse(_statusCode, account)
: Request.CreateResponse(_statusCode, errorCode);
}
Since you are asking for an opinion, here's one from the creator of SlashDB API: when in doubt, leave it out.
In the JSON data format keys with null value can be omitted from the result.
The following API call returns a Customer record, which does not have a value in the Company field:
HTML rendering: http://demo.slashdb.com/db/Chinook/Customer/CustomerId/2.html
JSON rendering: http://demo.slashdb.com/db/Chinook/Customer/CustomerId/2.json
XML format is more formal about it because you can define a schema, which prescribes what is the allowed shape of data. In particular, an element (tag) could have a minOccurs=0 attribute to indicate its optionality, and/or a nillable=True to indicate that a tag could be present without a value (i.e. ). For completeness here's the same record in XML and its schema:
XML rendering: http://demo.slashdb.com/db/Chinook/Customer/CustomerId/2.html
XSD schema: http://demo.slashdb.com/db/Chinook/Customer.xsd

Teach ASP.NET MVC to treat key-less query values as boolean flags

If a query value in URL doesn't have a key, e.g.: http://site.com?update
and an action has bool argument named update.
I want that update argument to received value true if 'update' value is present in the URL, and false otherwise.
Example:
Action: public ActionResult MyAction(string name, bool update) {...}
URL: http://site.com/path?name=Bob&update
Expected action call: controller.MyAction("Bob", true);
if URL is http://site.com/path?name=Bob (notice no update)
then expected call is controller.MyAction("Bob", false);
It is not a big deal, I do know I can just get Request.Query and find values with key=null, but I want to have it done through the framework.
Where do I begin?
I'm using ASP.NET MVC 3
Doing this through the framework would probably require you to implement your own value provider with a corresponding value provider factory and add the factory to ValueProviderFactories. You would probably implement it similar to the existing QueryStringValueProviderFactory and QueryStringValueProvider but then add your own implementation of GetValue that includes the additional logic you wanted to return true/false based on if there is a value provided for the query string key. Here is a link on adding a value provider, and check out the QueryStringValueProvider in the framework.
http://mgolchin.net/posts/19/dive-deep-into-mvc-ivalueprovider

Resources