我正在使用Pyramid和Cornice为Web服务开发REST API ; 服务器端的数据使用SQLAlchemy和MySQL处理.Web服务器是使用uwsgi的nginx,它被配置为运行多个Python进程:
[uwsgi]
socket = localhost:6542
plugins = python34
...
processes = 2 # spawn the specified number of workers/processes
threads = 2 # run each worker in prethreaded mode with the specified number of threads
Run Code Online (Sandbox Code Playgroud)
问题
假设customers
服务器端有一个表.使用API可以读取客户数据,修改或删除客户数据.除此之外,还有其他API函数可以读取客户数据.
我可以同时发出多个API调用,然后竞争相同的客户资源:
# Write/modify the customer {id} data
curl --request POST ... https://some.host/api/customer/{id}
# Delete customer {id} and all of its associated data
curl --request DELETE https://some.host/api/customer/{id}
# …
Run Code Online (Sandbox Code Playgroud) 我已经多次阅读了文档,并搜索了这个问题的答案,但是很简单.具体来说,我已经研究了为服务定义服务和Cornice API以及为资源定义资源.
我目前正在构建一个REST API,它具有与此类似的结构:
GET /clients # Gets a list of clients
GET /clients/{id} # Gets a specific client
GET /clients/{id}/users # Gets a specific clients users
Run Code Online (Sandbox Code Playgroud)
最好的方法是什么?我应该使用服务或资源还是两者兼而有之?而且,如果两者都是,怎么样?
$http({method: 'POST', url: 'http://localhost:5001/products', data: {token: $scope.product.token}}).success(
function () {
alert('success');
}
);
Run Code Online (Sandbox Code Playgroud)
在金字塔方面,request.POST显示NOVars:不是表单请求.不是HTML表单提交(Content-Type:application/json)
我使用檐口提供我的api(/产品),我认为这是金字塔的问题.
有没有人有办法解决吗?
我使用Cornice制作RESTful网址.
我安装了pyramid-beaker,并__init__.py
在doc中指定了我的include .
而已.然后我在我的观点中这样做了:
p_desc = """Service to post session. """
p = Service(name='p',\
path=root+'/1',\
description=register_desc)
g_desc = """Service to get session. """
g = Service(name='g',\
path=root+'/2',\
description=g_desc)
@g.get()
def view2(request):
print request.session
return Response()
@p.post()
def view1(request):
print 'here'
request.session['lol'] = 'what'
request.session.save()
print request.session
return Response()
Run Code Online (Sandbox Code Playgroud)
这是我的结果
>>> requests.post('http://localhost/proj/1')
<Response [200]>
>>> requests.get('http://localhost/proj/2')
<Response [200]>
Starting HTTP server on http://0.0.0.0:6543
here
{'_accessed_time': 1346107789.2590189, 'lol': 'what', '_creation_time': 1346107789.2590189}
localhost - - [27/Aug/2012 18:49:49] "POST /proj/1 HTTP/1.0" 200 …
Run Code Online (Sandbox Code Playgroud) APIURL ='http://localhost:6543/api/patches/alice_8b84090712bce46e15a8107839cefe/e5678'
data = {
'patch_id' : 'e5678',
'queue_id' : 'alice_8b84090712bce46e15a8107839cefe',
}
response = requests.get(APIURL, data=data)
Run Code Online (Sandbox Code Playgroud)
我有上面的代码来测试一个檐口金字塔REST api.
但是,我无法读取通过data=data
参数输入的数据.
这是此端点的服务器功能:
@resource(collection_path='/api/patches', path="/api/patches/{queue_id}/{patch_id}")
class Patch(object):
def __init__(self, request):
self.request = request
def get(self):
"""
"""
queue_id = self.request.matchdict.get('queue_id', '')
patch_id = self.request.matchdict.get('patch_id', '')
data = {
'queue_id': 'e12525e1f90ad5e7395a965',
'patch_id': 'a9651a8259a666c0032f343',
'node_id': 'bef3a2adc76415b2be0f6942b5111f6c5e5b7002',
'message': 'This is a patch on feature2.',
'datetime': '.....',
}
#TODO call the patch method to get the public attributes
return {'error':{}, 'data': data}
Run Code Online (Sandbox Code Playgroud)