Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@rpc(NumQueriesType, _returns=Array(World))
def updates(ctx, queries):
"""Test 5: Database Updates"""
if queries is None:
queries = 1
retval = []
q = ctx.udc.session.query(World)
for id in (randint(1, 10000) for _ in xrange(queries)):
world = q.get(id)
world.randomNumber = randint(1, 10000)
retval.append(world)
ctx.udc.session.commit()
return retval
@rpc(User, _returns=Integer)
def add_user(ctx, user):
add_user(user)
return user.userid
@rpc(Integer)
def delete_user(ctx, userid):
global user_database
if not (userid in user_database):
raise ResourceNotFoundError(userid)
del user_database[userid]
@rpc(_returns=World)
def dbraw(ctx):
conn = ctx.udc.conn
wid = randint(1, 10000)
return conn.execute(
"SELECT id, randomNumber FROM world WHERE id = %s", wid) \
.fetchone()
@rpc(_returns=Array(Fortune, html_cloth=T_INDEX), _body_style='out_bare')
def fortunesraw(ctx):
# This is normally specified at the application level as it's a good
# practice to group rpc endpoints with similar return types under the
# same url fragment. eg. https://example.com/api/json
ctx.out_protocol = HtmlCloth()
ctx.outprot_ctx = ctx.out_protocol.get_context(ctx, ctx.transport)
res = ctx.udc.conn.execute("SELECT id, message FROM fortune")
fortunes = res.fetchall()
fortunes.append(Fortune(
id=0,
message=u"Additional fortune added at request time."
))
fortunes.sort(key=lambda x: x.message)
@rpc(M(UnsignedInteger32),
_in_message_name='del_%s' % T_name,
_in_variable_names={'obj_id': '%s_id' % T_name})
def del_(ctx, obj_id):
count = ctx.udc.session.query(T).filter_by(id=obj_id).count()
if count == 0:
raise ResourceNotFoundError(obj_id)
ctx.udc.session.query(T).filter_by(id=obj_id).delete()
@rpc(M(UnsignedInteger32),
_in_message_name='del_%s' % T_name,
_in_variable_names={'obj_id': '%s_id' % T_name})
def del_(ctx, obj_id):
count = ctx.udc.session.query(T).filter_by(id=obj_id).count()
if count == 0:
raise ResourceNotFoundError(obj_id)
ctx.udc.session.query(T).filter_by(id=obj_id).delete()
@rpc(Unicode, Integer, _returns=File)
def say_hello_as_binary_file(ctx, name, times):
# WARNING!: the native value for data is an iterable of bytes, not just
# bytes! If you forget this you may return data using 1-byte http chunks
# which is incredibly inefficient.
# WARNING!: don't forget to encode your data! This is the binary
# output mode! You can't just write unicode data to socket!
mime_type = HttpTransportContext.gen_header("text/plain",
charset="utf8")
return File.Value(type=mime_type,
data=['\n'.join(s.encode('utf8') for s in
_say_hello(ctx, name, times, 'txt'))]
)