[web2py] Re: json-rpc

2018-04-24 Thread 黄祥
*json rpc (1.1 and 2) *
*for curl should be*
curl -X POST -d '{"method": "add", "params": [42, 23], "id": 1}' -i 
http://127.0.0.1:8000/test/default/call/jsonrpc
curl -X POST -d '{"method": "add", "params": {"a": 42, "b": 23}, "id": 1}' 
-i http://127.0.0.1:8000/test/default/call/jsonrpc
curl -X POST -d '{"jsonrpc": "2.0", "method": "add", "params": [42, 23], 
"id": 3 }' -i http://127.0.0.1:8000/test/default/call/jsonrpc2
curl -X POST -d '{"jsonrpc": "2.0", "method": "add", "params": {"a": 42, 
"b": 23}, "id": 3 }' -i http://127.0.0.1:8000/test/default/call/jsonrpc2

*for postman*
just change the method into POST and the body raw type (application/json) 
with the params from example above (curl -d)

xmlrpc seems must create the *.xml file first, not sure, never tried

best regards,
stifan

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: json-rpc

2018-04-23 Thread 黄祥
trying to learn rpc
*controllers/default.py*
from gluon.tools import Service
service = Service()

def call():
return service()

@service.xmlrpc
@service.jsonrpc
@service.jsonrpc2
def add(a, b):
return a + b

*using curl*
curl -X GET -d '{"jsonrpc":"2.0","method":"add","params":["1","2"] }' -i 
http://127.0.0.1:8000/test/default/call/jsonrpc2

*result :*
HTTP/1.1 200 OK
X-Powered-By: web2py
Set-Cookie:  
session_id_test=127.0.0.1-e4f82a2b-19ab-4331-9744-711988008e68; httponly; 
Path=/
Expires: Tue, 24 Apr 2018 06:43:49 GMT
Pragma: no-cache
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, 
pre-check=0
Content-Type: application/json; charset=utf-8
Date: Tue, 24 Apr 2018 06:43:49 GMT
Server: Rocket 1.2.6 Python/2.7.10
Content-Length: 0
Connection: keep-alive

*using postman with get method*
http://127.0.0.1:8000/test/default/call/jsonrpc2

*result :*
{"jsonrpc": "2.0", "id": null, "error": {"message": "Parse error. Invalid 
JSON was received by the server.", "code": -32700, "data": "An error 
occurred on the server while parsing the JSON text."}}

*using postman** with get method*
http://127.0.0.1:8000/test/default/call/jsonrpc

*result :*
Traceback (most recent call last):
  File "/Users/MacBookPro/project/python/web2py/gluon/restricted.py", line 
219, in restricted
exec(ccode, environment)
  File 
"/Users/MacBookPro/project/python/web2py/applications/test/controllers/default.py",
 
line 73, in 
  File "/Users/MacBookPro/project/python/web2py/gluon/globals.py", line 
419, in 
self._caller = lambda f: f()
  File 
"/Users/MacBookPro/project/python/web2py/applications/test/controllers/default.py",
 
line 64, in call
return service()
  File "/Users/MacBookPro/project/python/web2py/gluon/tools.py", line 5483, 
in __call__
return self.serve_jsonrpc()
  File "/Users/MacBookPro/project/python/web2py/gluon/tools.py", line 5207, 
in serve_jsonrpc
data = json.loads(request.body.read())
  File 
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py",
 
line 338, in loads
return _default_decoder.decode(s)
  File 
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py",
 
line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File 
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py",
 
line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

any idea?

*n.b.*
web2y latest version scaffolding welcome didn't declare service module in 
db.py

thx and best regards,
stifan

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: JSON-RPC calls within controller functions

2014-07-10 Thread lyn2py
Thanks for your response Matheus!

no authentication… is there somewhere I can read up on this? I tried 
searching around google for answers, and ran into using "id", but no idea 
how that works, it appears that the id is automatically included. It's 
working on command line, the exact same commands, but not inside the 
controller function.

Had me wondering if I am using JSON-RPC correctly?

I guess what I need is an example of JSON-RPC working within a controller's 
function… 



On Wednesday, July 9, 2014 9:58:23 PM UTC+8, Matheus Cardoso wrote:
>
> There is any authentication on that? If yes, you should try something like 
> this:
>
> server = ServerProxy('http://'+ ws_user + ':' + ws_pass + '
> @127.0.0.1:8000/app22/default/call/jsonrpc 
> ')
>
> And If you created by yourself the WS through web2py, make sure that you 
> put the decorator on your service function "load". Besides, try (this time 
> is a guess, ok?) return your "result" variable instead of locals().
>
> On Tuesday, July 8, 2014 7:41:44 AM UTC-3, lyn2py wrote:
>>
>> I'm wanting to get JSON RPC to work within web2py controller functions, 
>> without using pyjamas (which is the example provided in the book)
>>
>> This is my code for the JSON RPC call:
>> def test_call():
>> from gluon.contrib.simplejsonrpc import ServerProxy
>> service = ServerProxy('
>> http://127.0.0.1:8000/app22/default/call/jsonrpc')
>> result = service.load(1000)
>> return locals()
>>
>> (1) Running it within web2py controller functions = It returns an error: 
>> ValueError: No JSON object could be decoded
>>
>> (2) Running it in the browser =  
>> http://127.0.0.1:8000/app22/default/call/jsonrpc/load/1000 it runs into 
>> the same error as (1).
>>
>> (3) Running it in command prompt = it works like a charm.
>>
>> How do I get it to work within web2py controller functions? Thanks!
>>
>>
>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: JSON-RPC calls within controller functions

2014-07-09 Thread Matheus Cardoso
There is any authentication on that? If yes, you should try something like 
this:

server = ServerProxy('http://'+ ws_user + ':' + ws_pass + '
@127.0.0.1:8000/app22/default/call/jsonrpc 
')

And If you created by yourself the WS through web2py, make sure that you 
put the decorator on your service function "load". Besides, try (this time 
is a guess, ok?) return your "result" variable instead of locals().

On Tuesday, July 8, 2014 7:41:44 AM UTC-3, lyn2py wrote:
>
> I'm wanting to get JSON RPC to work within web2py controller functions, 
> without using pyjamas (which is the example provided in the book)
>
> This is my code for the JSON RPC call:
> def test_call():
> from gluon.contrib.simplejsonrpc import ServerProxy
> service = ServerProxy('
> http://127.0.0.1:8000/app22/default/call/jsonrpc')
> result = service.load(1000)
> return locals()
>
> (1) Running it within web2py controller functions = It returns an error: 
> ValueError: No JSON object could be decoded
>
> (2) Running it in the browser =  
> http://127.0.0.1:8000/app22/default/call/jsonrpc/load/1000 it runs into 
> the same error as (1).
>
> (3) Running it in command prompt = it works like a charm.
>
> How do I get it to work within web2py controller functions? Thanks!
>
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: JSON-RPC best practices

2010-10-22 Thread cjrh
On Oct 22, 11:28 am, Omri  wrote:
> Should I write a very general function that basically exposes the
> database to the client side?

Yes

> Should a write many small functions, each
> wrapping a database call (perhaps sometimes also doing some
> calculations in the background)?

No

> Should I opt to transfer large amounts of data at each JSON-RPC call
> (to minimize server calls),

Yes

> or only ask for the data when needed?

Yes.  Ask only for data when needed, but do as much work in a single
request as you need to do.  Don't use multiple requests when a single
one will do.

> This web-application is intended to be internal to my company, with no
> more than 30 users, and probably no more than 10 simultaneously.

There will be no performance impact at all.  Therefore, you main
consideration should be writing the code in such a way that your own
work is minimized.   Exploit dictionary unpacking to dump JSON data
dictionaries directly to the DB, for example, e.g. (untested)

def insert_json_data(table, jsondata):
table.insert(**jsondata)

jsondata1 = simplejson.decode(incomingstuff1)
mytable = db.person
insert_json_data(mytable, jsondata1)

jsondata2 = simplejson.decode(incomingstuff1)
mytable = db.account
insert_json_data(mytable, jsondata2)

If you are careful with your field namings between DB and json
packets, you can get a log of mileage out of this. If you put your
table name inside your json data, you can remove even the table arg.









>
> Any thoughts/opinions/discussions are welcome :)
>
> Cheers,
> Omri