Re: How to do a REST WS?

2012-06-22 Thread Борислав Събев
Yes! I am aware. :) I didn't want to use the strict REST model in this 
application - yes some of this is actually copy/paste from an WebService I 
did.
As you can see all requests are POST - I wanted to avoid PUT and DELETE 
requests and implement strong authentication. Anyhow you're right, I didn't 
mention this in the reply.

On Friday, 22 June 2012 13:56:55 UTC+3, the_woodsman wrote:
>
> Sorry to be pedantic, but unless I don't understand these routes,  it's 
> worth mentioning that this example isn't strictly REST:
>
> *Router::connect('/:candidates/addrecord', array('controller'=> 
> 'candidates', 'action' => 'addRecord', '[method]' => 'POST'));
> Router::connect('/:candidates/editrecord', array('controller'=> 
> 'candidates', 'action' => 'editRecord', '[method]' => 'POST'));
> Router::connect('/:candidates/deleterecord', array('controller'=> 
> 'candidates', 'action' => 'deleteRecord', '[method]' => 'POST'));*
> *
> *
> REST URLs shouldn't contain any verbs, like add, edit, delete, etc, only 
> nouns. The verbs are implicit in the method, ie. POST vs PUT vs GET vs 
> DELETE, rather than all using POST.
>
> This is an HTTP based API, and there's nothing wrong with that! But just 
> to be clear about REST vs HTTP, I thought I should mention...
>
>
> On Friday, 22 June 2012 10:14:42 UTC+1, Борислав Събев wrote:
>>
>> Hi, Lucas.
>>
>> Firstly if you're developing a REST service it's best to use Cake 2.x and 
>> higher - you can just use the latest stable version. The new Cake version 
>> has a lot of improvements which will come in handy when you're doing a REST 
>> service.Then how would you go about building your REST sevice? I will try 
>> to describe this as 
>> a general overview of how things should all work together:
>>
>> Firstly you should set up some of the aspects of 
>> Routing
>> .
>>   Concerning *Router**::mapResources(**);* is the fast way to go - it "is 
>> used to setup a number of default routes for 
>> RESTaccess
>>  to your controllers".
>> If you want a more fine grade setup you should consider using custom 
>> REST 
>> routingwhich
>>  is what I personally prefer using:
>>
>> *Router::connect('/:candidates/addrecord', array('controller'=> 
>> 'candidates', 'action' => 'addRecord', '[method]' => 'POST'));
>> Router::connect('/:candidates/editrecord', array('controller'=> 
>> 'candidates', 'action' => 'editRecord', '[method]' => 'POST'));
>> Router::connect('/:candidates/deleterecord', array('controller'=> 
>> 'candidates', 'action' => 'deleteRecord', '[method]' => 'POST'));
>> *
>>   Once you've configured the routes you can proceed to identifying 
>> requests. Nevertheless this is still part of the initial Router 
>> configuration:
>> *   Router::parseExtensions('xml','json','rss'); *- This will instruct 
>> the router to parse out file extensions from the URL for e.g.:
>> *http://www.example.com/articles.xml* would parse a file extension of 
>> "xml". What this will yield is that the parsed file extension will become 
>> available in the Controller's 
>> $params property (in *$this->params['ext']*). This property is used 
>> (runtime) by the RequestHandler component to automatically switch to 
>> alternate layouts and templates, and load helpers corresponding to the 
>> given content. So this will greatly help you in the development of a REST 
>> service, if you set all your layouts/views by the conventions.
>>
>> So what about handling and serving responses to requests? Firstly you 
>> should add the 
>> RequestHandlercomponent.
>>  If you will be using it in all controllers (which should be the 
>> case :) ) you should add it the AppController's $components property:
>>
>> *public $components = array(
>> 'DebugKit.Toolbar',
>> 'Session',
>> 'Auth' => array(
>> 'loginRedirect' => array('controller' => 'users', 
>> 'action' => 'index'),
>> 'logoutRedirect' => array('controller' => 'users', 
>> 'action' => 'login')
>> ),
>> 'RequestHandler'
>> );
>> *
>> When the application receives a request for e.g. on: *
>> http://www.example.com/records.xml* by default this will call* 
>> RecordsController::**index()*.
>> Here's an example structure of a add method:
>> *
>> public function addRecord() {
>> if ($this->request->is('post')) {
>> //Authentication ?
>> //Validate incoming data
>> if ($this->RecorisXmld->saveAll($data)){
>> if($this->RequestHandler->isXml()){
>> //Serve a Xml responce 
>> }
>> if($this->RequestHandler->isRss()){
>> //Serve RSS
>> }
>> }
>> } 
>> }*

Re: How to do a REST WS?

2012-06-22 Thread Lucas Simon Rodrigues Magalhaes
thanks guys to help.


Here I am with another problem. I can not serve the content through a file, 
for example, when I call the url:

http://.../ws_billings/generate_extract_consolidated/1/01-06-2012/21-06-2012/

First parameter, validating a Token. Not implemented yet.
Second parameter and third parameter is the start date and end date

The browser renders the json by json_encode and displays on the screen.
{"0": {"Company": {"id": "47", "name": "ASDASDASDASd"}}

How to do this action make this type GET, display the file in 
companyXYZ-ddmmyy.json? For the software consumer read this feature the 
file and I want to do the manipulation.

And besides it can not generate the XML, it generates an error:

Error processing XML: characters useless after a document element
Position: 
http://.../ws_billings/generate_extract_consolidated/1/01-06-2012/21-06-2012/xml/a.xml
Number of line 1, column 446:

And when I generate JSON
 Error 404: 
 The requested address '/ 
ws_billings/generate_extract_consolidated/1/01-06-2012/21-06-2012/a.json' 
 was not found on server.  
 
The array that I send to the view is correct.


Another detail and doubt that there is a need to specify the type 
generated, it seems to me that the cake is that through the past 
extensions. But how to pass an extension of a get method?





I left the source code on gist [1]

[1] https://gist.github.com/2973381



Em sexta-feira, 22 de junho de 2012 07h56min55s UTC-3, the_woodsman 
escreveu:
>
> Sorry to be pedantic, but unless I don't understand these routes,  it's 
> worth mentioning that this example isn't strictly REST:
>
> *Router::connect('/:candidates/addrecord', array('controller'=> 
> 'candidates', 'action' => 'addRecord', '[method]' => 'POST'));
> Router::connect('/:candidates/editrecord', array('controller'=> 
> 'candidates', 'action' => 'editRecord', '[method]' => 'POST'));
> Router::connect('/:candidates/deleterecord', array('controller'=> 
> 'candidates', 'action' => 'deleteRecord', '[method]' => 'POST'));*
> *
> *
> REST URLs shouldn't contain any verbs, like add, edit, delete, etc, only 
> nouns. The verbs are implicit in the method, ie. POST vs PUT vs GET vs 
> DELETE, rather than all using POST.
>
> This is an HTTP based API, and there's nothing wrong with that! But just 
> to be clear about REST vs HTTP, I thought I should mention...
>
>
> On Friday, 22 June 2012 10:14:42 UTC+1, Борислав Събев wrote:
>>
>> Hi, Lucas.
>>
>> Firstly if you're developing a REST service it's best to use Cake 2.x and 
>> higher - you can just use the latest stable version. The new Cake version 
>> has a lot of improvements which will come in handy when you're doing a REST 
>> service.Then how would you go about building your REST sevice? I will try 
>> to describe this as 
>> a general overview of how things should all work together:
>>
>> Firstly you should set up some of the aspects of 
>> Routing
>> .
>>   Concerning *Router**::mapResources(**);* is the fast way to go - it "is 
>> used to setup a number of default routes for 
>> RESTaccess
>>  to your controllers".
>> If you want a more fine grade setup you should consider using custom 
>> REST 
>> routingwhich
>>  is what I personally prefer using:
>>
>> *Router::connect('/:candidates/addrecord', array('controller'=> 
>> 'candidates', 'action' => 'addRecord', '[method]' => 'POST'));
>> Router::connect('/:candidates/editrecord', array('controller'=> 
>> 'candidates', 'action' => 'editRecord', '[method]' => 'POST'));
>> Router::connect('/:candidates/deleterecord', array('controller'=> 
>> 'candidates', 'action' => 'deleteRecord', '[method]' => 'POST'));
>> *
>>   Once you've configured the routes you can proceed to identifying 
>> requests. Nevertheless this is still part of the initial Router 
>> configuration:
>> *   Router::parseExtensions('xml','json','rss'); *- This will instruct 
>> the router to parse out file extensions from the URL for e.g.:
>> *http://www.example.com/articles.xml* would parse a file extension of 
>> "xml". What this will yield is that the parsed file extension will become 
>> available in the Controller's 
>> $params property (in *$this->params['ext']*). This property is used 
>> (runtime) by the RequestHandler component to automatically switch to 
>> alternate layouts and templates, and load helpers corresponding to the 
>> given content. So this will greatly help you in the development of a REST 
>> service, if you set all your layouts/views by the conventions.
>>
>> So what about handling and serving responses to requests? Firstly you 
>> should add the 
>> RequestHandlercomponent.
>>  If you will be using it in all controllers (which should be the 
>> case :) 

Re: How to do a REST WS?

2012-06-22 Thread the_woodsman
Sorry to be pedantic, but unless I don't understand these routes,  it's 
worth mentioning that this example isn't strictly REST:

*Router::connect('/:candidates/addrecord', array('controller'=> 
'candidates', 'action' => 'addRecord', '[method]' => 'POST'));
Router::connect('/:candidates/editrecord', array('controller'=> 
'candidates', 'action' => 'editRecord', '[method]' => 'POST'));
Router::connect('/:candidates/deleterecord', array('controller'=> 
'candidates', 'action' => 'deleteRecord', '[method]' => 'POST'));*
*
*
REST URLs shouldn't contain any verbs, like add, edit, delete, etc, only 
nouns. The verbs are implicit in the method, ie. POST vs PUT vs GET vs 
DELETE, rather than all using POST.

This is an HTTP based API, and there's nothing wrong with that! But just to 
be clear about REST vs HTTP, I thought I should mention...


On Friday, 22 June 2012 10:14:42 UTC+1, Борислав Събев wrote:
>
> Hi, Lucas.
>
> Firstly if you're developing a REST service it's best to use Cake 2.x and 
> higher - you can just use the latest stable version. The new Cake version 
> has a lot of improvements which will come in handy when you're doing a REST 
> service.Then how would you go about building your REST sevice? I will try 
> to describe this as 
> a general overview of how things should all work together:
>
> Firstly you should set up some of the aspects of 
> Routing
> .
>   Concerning *Router**::mapResources(**);* is the fast way to go - it "is 
> used to setup a number of default routes for 
> RESTaccess
>  to your controllers".
> If you want a more fine grade setup you should consider using custom REST 
> routingwhich
>  is what I personally prefer using:
>
> *Router::connect('/:candidates/addrecord', array('controller'=> 
> 'candidates', 'action' => 'addRecord', '[method]' => 'POST'));
> Router::connect('/:candidates/editrecord', array('controller'=> 
> 'candidates', 'action' => 'editRecord', '[method]' => 'POST'));
> Router::connect('/:candidates/deleterecord', array('controller'=> 
> 'candidates', 'action' => 'deleteRecord', '[method]' => 'POST'));
> *
>   Once you've configured the routes you can proceed to identifying 
> requests. Nevertheless this is still part of the initial Router 
> configuration:
> *   Router::parseExtensions('xml','json','rss'); *- This will instruct 
> the router to parse out file extensions from the URL for e.g.:
> *http://www.example.com/articles.xml* would parse a file extension of 
> "xml". What this will yield is that the parsed file extension will become 
> available in the Controller's 
> $params property (in *$this->params['ext']*). This property is used 
> (runtime) by the RequestHandler component to automatically switch to 
> alternate layouts and templates, and load helpers corresponding to the 
> given content. So this will greatly help you in the development of a REST 
> service, if you set all your layouts/views by the conventions.
>
> So what about handling and serving responses to requests? Firstly you 
> should add the 
> RequestHandlercomponent.
>  If you will be using it in all controllers (which should be the 
> case :) ) you should add it the AppController's $components property:
>
> *public $components = array(
> 'DebugKit.Toolbar',
> 'Session',
> 'Auth' => array(
> 'loginRedirect' => array('controller' => 'users', 'action' 
> => 'index'),
> 'logoutRedirect' => array('controller' => 'users', 
> 'action' => 'login')
> ),
> 'RequestHandler'
> );
> *
> When the application receives a request for e.g. on: *
> http://www.example.com/records.xml* by default this will call* 
> RecordsController::**index()*.
> Here's an example structure of a add method:
> *
> public function addRecord() {
> if ($this->request->is('post')) {
> //Authentication ?
> //Validate incoming data
> if ($this->RecorisXmld->saveAll($data)){
> if($this->RequestHandler->isXml()){
> //Serve a Xml responce 
> }
> if($this->RequestHandler->isRss()){
> //Serve RSS
> }
> }
> } 
> }*
>
> CakePHP now (since 2.1) has Json and Xml view 
> classes. 
> What this will do is that for e.g. "After adding 'json' to 
> Router::parseExtensions() in your routes file, CakePHP will automatically 
> switch view classes when a request is done with the .json extension, or the 
> Accept header is application/json."
>
> So this basically is it. By enabling Cake's core features you could be 
> able to easily manage a REST service. Hope this helpe

Re: How to do a REST WS?

2012-06-22 Thread Борислав Събев
Hi, Lucas.

Firstly if you're developing a REST service it's best to use Cake 2.x and 
higher - you can just use the latest stable version. The new Cake version 
has a lot of improvements which will come in handy when you're doing a REST 
service.Then how would you go about building your REST sevice? I will try 
to describe this as 
a general overview of how things should all work together:

Firstly you should set up some of the aspects of 
Routing
.
  Concerning *Router**::mapResources(**);* is the fast way to go - it "is 
used to setup a number of default routes for 
RESTaccess
 to your controllers".
If you want a more fine grade setup you should consider using custom REST 
routingwhich
 is what I personally prefer using:

*Router::connect('/:candidates/addrecord', array('controller'=> 
'candidates', 'action' => 'addRecord', '[method]' => 'POST'));
Router::connect('/:candidates/editrecord', array('controller'=> 
'candidates', 'action' => 'editRecord', '[method]' => 'POST'));
Router::connect('/:candidates/deleterecord', array('controller'=> 
'candidates', 'action' => 'deleteRecord', '[method]' => 'POST'));
*
  Once you've configured the routes you can proceed to identifying 
requests. Nevertheless this is still part of the initial Router 
configuration:
*   Router::parseExtensions('xml','json','rss'); *- This will instruct the 
router to parse out file extensions from the URL for e.g.:
*http://www.example.com/articles.xml* would parse a file extension of 
"xml". What this will yield is that the parsed file extension will become 
available in the Controller's 
$params property (in *$this->params['ext']*). This property is used 
(runtime) by the RequestHandler component to automatically switch to 
alternate layouts and templates, and load helpers corresponding to the 
given content. So this will greatly help you in the development of a REST 
service, if you set all your layouts/views by the conventions.

So what about handling and serving responses to requests? Firstly you 
should add the 
RequestHandlercomponent.
 If you will be using it in all controllers (which should be the 
case :) ) you should add it the AppController's $components property:

*public $components = array(
'DebugKit.Toolbar',
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' 
=> 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' 
=> 'login')
),
'RequestHandler'
);
*
When the application receives a request for e.g. on: *
http://www.example.com/records.xml* by default this will call* 
RecordsController::**index()*.
Here's an example structure of a add method:
*
public function addRecord() {
if ($this->request->is('post')) {
//Authentication ?
//Validate incoming data
if ($this->RecorisXmld->saveAll($data)){
if($this->RequestHandler->isXml()){
//Serve a Xml responce 
}
if($this->RequestHandler->isRss()){
//Serve RSS
}
}
} 
}*

CakePHP now (since 2.1) has Json and Xml view 
classes. 
What this will do is that for e.g. "After adding 'json' to 
Router::parseExtensions() in your routes file, CakePHP will automatically 
switch view classes when a request is done with the .json extension, or the 
Accept header is application/json."

So this basically is it. By enabling Cake's core features you could be able 
to easily manage a REST service. Hope this helped.

Cheers,
   Borislav.

On Thursday, 21 June 2012 20:26:28 UTC+3, Lucas Simon Rodrigues Magalhaes 
wrote:
>
> Hello I need to build an application in Webservice to return data for a 
> particular billing.
>
> First I'm using cakePHP version 1.3, and following the cake book [1].
> According to according to the book I should map the model which give 
> permission to access REST.
> I thought about using REST to provide data json / xml instead of nusoap, 
> but I'm sure how to do it.
>
> First I'm using cakePHP version 1.3, and following the cake book [1].
> According to according to the book I should map the model which give 
> permission to access REST.
>
> The problem:
> In this case I need to perform a find in various tables and perform an 
> operation with her results in a foreach, and then send the answer to this 
> operation via JSON / XML.
>
> What I did:
> I created a controller called ws_billing_controller.php that will manage 
> the requests get to the rest.
> routes.php -> Router :: mapResources ('') / / empty do not know what to 
> put here
> routes.php -> Router 

How to do a REST WS?

2012-06-21 Thread Lucas Simon Rodrigues Magalhaes
Hello I need to build an application in Webservice to return data for a 
particular billing.
I thought about using REST to provide data json / xml instead of nusoap, 
but I'm sure how to do it.

First I'm using cakePHP version 1.3, and following the cake book [1].
According to according to the book I should map the model which give 
permission to access REST.

The problem:
In this case I need to perform a find in various tables and perform an 
operation with her results in a foreach, and then send the answer to this 
operation via JSON / XML.

What I did:
I created a controller called ws_billing_controller.php that will manage 
the requests get to the rest.
routes.php -> Router :: mapResources ('') / / empty do not know what to put 
here
routes.php -> Router :: parseExtensions ('json', 'xml');
ws_billing_controller.php -> [2]

I wonder if the path I'm following this right, or should I change?
[1] http://book.cakephp.org/1.3/pt/view/1239/Configura% C3% A7% C3% 
A3o-Simple
[2] http://dpaste.com/hold/762069/

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php