I'm training with CakePHP. Currently I have a problem.
I don't know how to paging with json data.
My application will call to API and receive JSON data.
The format look like
{"success":1,"films":[{"film_id":"384","title":"GROSSE WONDERFUL","description":"A Epic Drama of a Cat And a Explorer who must Redeem a Moose in Australia","release_year":"2006","language_id":"1","original_language_id":null,"rental_duration":"5","rental_rate":"4.99","length":"49","replacement_cost":"19.99","rating":"R","special_features":"Behind the Scenes","last_update":"2006-02-15 05:03:42"},{"film_id":"984","title":"WONDERFUL DROP","description":"A Boring Panorama of a Woman And a Madman who must Overcome a Butler in A U-Boat","release_year":"2006","language_id":"1","original_language_id":null,"rental_duration":"3","rental_rate":"2.99","length":"126","replacement_cost":"20.99","rating":"NC-17","special_features":"Commentaries","last_update":"2006-02-15 05:03:42"}]}
This is php code
$data = array('filmId' => $search_data['filmId'], 'filmTitle' => $search_data['filmTitle'], 'releaseYear' => $search_data['releaseYear'], 'rating' => $search_data['rating']);
$HttpSocket = new HttpSocket();
$records = $HttpSocket->post($apiFilmsByConditions, $data);
$records = json_decode($records, true);
I try to use paginate of CakePHP but it seems that this will get data from database, not from API.
This is API link http://oxuhandmade.com/api/get_all_films.php
Can you help me on this case?
Doesn't the API query the database? An API is an "Application programming interface", meaning that it allows you to operate on the database from the "outside", based on a simple interface, triggered by some requests. So there is no point in wanting to limit the results via API, but not via database.
To limit the results an API returns, you need to pass a parameter together with your request, process it in the backend, query the database with the desired limit, and return the results to whoever made the request. Or simply hardcode the desired limit on the API, if it doesn't need to be dynamic.
The Paginator from CakePHP makes this work a whole lot easier, you only have to feed it some conditions and parameters for paginating the results, and it does the magic for you.
Related
I have phonorgraph object with billions of rows and we are querying it through object set service
for example, I want to get all DriverLicences from certain city.
#Function()
public getDriverLicences(city: string): ObjectSet<DriverLicences> {
let drivers = Objects.search().DriverLicences().filter(row => row.city.exactMatch(city));
return drivers ;
}
I am facing this error when I am trying query it from slate:
ERROR 400: {"errorCode":"INVALID_ARGUMENT","errorName":"ObjectSet:PagingAboveConfiguredLimitNotAllowed","errorInstanceId":"0000-000","parameters":{}}
I understand that I am probably retrieving more than 100 000 results but I need all the results because of the implemented logic in the front is a complex slate dashboard built by another team that we cannot re-factor.
The issue here is that, specifically in the Slate <> Function connector, there is a "translation layer" that serializes the contents of the object set and provides a response data structure that materializes the property:value pairs for each object in the set.
This clearly doesn't work for large object sets where throwing so much data into the browser is likely to overwhelm the resources allocated to the tab.
From context it seems like you might be migrating an existing Slate app over to Functions; in the current version, how is the query limiting the number of results returned? It certainly must not be returning several 100 thousand results for further processing on the front end? (And if so, that might be an anti-pattern to consider addressing).
As for options that you could currently explore, you can sort your object set and then specify a smaller limit to return:
Objects.search().DriverLicences().filter(row => row.city.exactMatch(city)).orderBy(date_of_issue).take(100)
You'll find a few more details in the Functions documentation Reference entry on Ontology API: Object Sets in the section on Ordering and limiting.
You can even make a work around for the (current) lack of paging when return an ObjectSet to Slate by using the last value from the property ordered on (i.e. date_of_issue) as a filter in the subsequent request and return the next N objects.
This can work if you need a Slate table or HTML widget that renders on set of results then, on a user action, gets the next page.
Please note the question is purposely vague.
Assume, I have an API that returns paginated data of books. 100 book objects per page [This data is present in the headers like 'total-pages'. The response body is in the format of a list of JSON objects.
[
{},
{},
.
.
.
]
Assume book object has all the necessary data such as title, description, number of sales, etc, and so on. How do I return top-selling n number of books [top-selling => Highest number of sales]?
You cannot provide any parameters such as
GET endpoint/orderby=popularity&order=desc
to this endpoint and you cannot make any database query optimizations. How do we solve this problem with just pure code?
I'll be grateful for any ideas or suggestions.
Since the endpoint is limited in the parameters it accepts, your only option is to fetch all the pages (by continuously looping until it errors) and then sort it in your program it self.
If the above solution is slow, you can optimize by relaxing your requirements from return top-selling n number of books to return a few top-selling books which are in the top X%
I am using the the /api/views/.../rows.json call to access the data and the metadata for a dataset, e.g. -
https://data.colorado.gov/api/views/wumm-7awb/rows.json
Problem is I don't want to access all of the data but using limit and offset parameters does not seem to have any effect.
I know about the SODA API endpoint, e.g. https://data.colorado.gov/resource/wumm-7awb.json, that has limit and offset but it doesn't contain the metadata that the /api/views returns
Alternative question - can the /resource API include the metadata?
Thanks
How about using the /resource/wumm-yawb.json endpoint to fetch the data with $limit and $offset and just use /api/views/wumm-yawb.json to fetch the metadata?
What metadata are you looking for? Our team is designing new metadata APIs and it'd be great to know what you're working with.
I 'm going to make a CakePHP application which collaborates with a mobile application. Mobile application is going to query CakePHP app to check if some vouchers are valid or not. I need to change some of my views such that their outputs be in json format in order to be parsed by mobile application easily.
To be specific if mobile app calles example.com/vouchers/check/1234 Cake should return something like this as response: {"validity":"valid"} or {"validity":"invalid"} which is the result of checking the validity of voucher with id 1234.
Basically, you should use extensions when you expect non HTML responses (JSON in this case)
So request
/vouchers/check/1234.json
and use the JsonView as per docs and as per ajax-and-cakephp tutorial.
To sum it up:
Use this to allow json extension being enabled:
Router::parseExtensions();
Router::setExtensions(array('json', ...));
Don't forget to include the RequestHandler component in the controllers $components list.
Add this to your action:
$data = array(
'validity' => ...,
);
$this->set(compact('data')); // Pass $data to the view
$this->set('_serialize', 'data'); // Let the JsonView class know what variable to use
I am a beginner to Magento. I am trying to extend the current API classes in Magento to fulfill my requirements and retrieve data in JSON format. I need:
I need to get all stores in a website
I need to get all Categories and Subcategories in a specific store
I need to get all products in a specific Category.
All data retrieved should be in JSON format.
Any blog/Forum topic? Any kind of help?
Thanks in advance.
Please refer to this Magento wiki page http://www.magentocommerce.com/wiki/doc/webservices-api/custom-api#creating_custom_adapter_for_api.
Steps:
You need to create a new API Server Adapter that should implement Mage_Api_Model_Server_Adapter_Interface.
Create a controller that will run your api server adapter
Implement Mage_Api_Model_Server_Adapter_Interface::run() method for process JSON request and return result in JSON. See Mage_Api_Model_Server_Handler_Abstract for understanding Magento API workflow.
never been is such situation , but an idea came to mind is to invoke a SOAP service or XML-RPC , then convert whatever data needed to JSON.
Magento offers SOAP or XML-RPC web service to be automatically generated with specific roles for users, very useful.
That would be a better approach and it is not at all complicated. Refer to this to see.
http://www.magentocommerce.com/wiki/5_-_modules_and_development/web_services/additional_information
See here
https://github.com/app-z/magento-android-web-api
There is even Random Products list
Is it what you want?
//
// Random Products Items
//
// http://localhost/magento/web-api.php?route=feed/web_api/random&limit=4&key=key1
//
function random_products($limit){
$json = array('success' => true);
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect(array('name', 'thumbnail', 'price')); //feel free to add any other attribues you need.
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
$products->getSelect()->order('RAND()')->limit($limit);
foreach($products as $product){
$json['products'][] = array(
'id' => $product->getId(),
'name' => $product->getName(),
'href' => $product->getProductUrl(),
'thumb' => (string)Mage::helper('catalog/image')->init($product, 'thumbnail'),
'pirce' => Mage::helper('core')->currency($product->getPrice(), true, false) //." ".$currencyCode,
);
}
return $json;
}
Inchoo has written a free REST, JSON, and AMF adapter for Magento. You can find it here: http://www.magentocommerce.com/magento-connect/inchoo-api.html