def read_by_id(endpoint, id): headers = { "Content-Type": "application/json; charset=utf8" } unwraps = request.query.getall('unwrap') resource = MongoResource(endpoint, id) if resource.document is None: return HTTPResponse(status=404, headers=headers) return HTTPResponse(status=200, headers=headers, body=dumps(resource.to_response(unwraps=unwraps)))
def update_resource(endpoint, id): new_document = request.json new_document.update({ '_id': ObjectId(id) }) mongo[endpoint].save(new_document) resource = MongoResource(endpoint, id) headers={ "Content-Type": "application/json; charset=utf8", "Location": "/users/{0}".format(id) } return HTTPResponse(status=201, headers=headers, body=dumps(resource.to_response()))
def create_resource(endpoint): endpoint_metadata = { '_id': endpoint, 'rel': endpoint, 'href': build_uri('/' + endpoint) } try: mongo[META_COLLECTION].insert(endpoint_metadata) except DuplicateKeyError: pass resource_id = mongo[endpoint].insert(request.json) resource = MongoResource(endpoint, resource_id) headers={ "Content-Type": "application/json; charset=utf8", "Location": "/users/{0}".format(resource_id) } return HTTPResponse(status=201, headers=headers, body=dumps(resource.to_response()))