コード例 #1
0
def buy_product_route(name):
    with get_session() as session:
        bought, data = buy_product(session, name)

    if bought is True:
        return response(status=200, data=data)
    else:
        return response(status=400, data=data)
コード例 #2
0
def insert_coin_route(name):
    with get_session() as session:
        inserted = insert_coin(session, name)

    if not inserted:
        return response(status=400, data={"msg": "Failed to insert coin."})

    return response(status=200, data=inserted)
コード例 #3
0
ファイル: api.py プロジェクト: suckerpunched/api
def get_account(event, context):
    path = event['pathParameters']

    uid = path.get('_id')

    if not uid:
        return response({'error': 'invalid parameters provided'}, 400)

    table = dynamodb.Table(os.environ['DYNAMODB_TABLE'])
    resp = table.get_item(Key={'_id': uid})

    item = resp.get('Item')
    if not item:
        return response({'error': 'account does not exist'}, 404)

    return response(item, 200)
コード例 #4
0
def register(event, context):
    try:
        sent = json.loads(event['body'])
    except:
        return response(400, 'Error parsing sent body.')

    if not 'id' in sent or not 'pw' in sent:
        return response(400, 'id and pw required.')

    # id and pw can't be empty
    if not len(sent['id']) or not len(sent['pw']):
        return response(400, 'id and pw required.')

    # User is registered only if id doesn't exist already
    if 'Item' in dynamodb.get_item(TableName='user',
                                   Key={'id': {
                                       'S': sent['id']
                                   }}):
        return response(400, 'id exists already.')

    # Hash pw with argon2
    try:
        hasher = PasswordHasher()
        hashed_pw = hasher.hash(sent['pw'])
    except:
        return response(400, 'Hashing pw failed.')

    # Save [id, hashed_pw] in user db
    dynamodb.put_item(TableName='user',
                      Item={
                          'id': {
                              'S': sent['id']
                          },
                          'pw': {
                              'S': hashed_pw
                          }
                      })

    return {'statusCode': 200}
コード例 #5
0
ファイル: api.py プロジェクト: suckerpunched/api
def post_account_section(event, context):
    path = event['pathParameters']
    keys = event['path'].split('/')[3:]

    try:
        body = json.loads(event['body'])
    except:
        return response({'error': 'invalid json provided'}, 400)

    uid = path.get('_id')

    if not uid:
        return response({'error': 'invalid parameters provided'}, 400)

    table = dynamodb.Table(os.environ['DYNAMODB_TABLE'])
    resp = table.get_item(Key={'_id': uid})

    item = resp.get('Item')
    nested_set(item, keys, body)

    table.put_item(Item=item)

    return response(item, 200)
コード例 #6
0
ファイル: api.py プロジェクト: suckerpunched/api
def post_account(event, context):
    path = event['pathParameters']

    try:
        body = json.loads(event['body'])
    except:
        return response({'error': 'invalid json provided'}, 400)

    uid = path.get('_id')
    alias = body.get('alias')
    website = body.get('website')
    company = body.get('company')

    if not uid or not alias or not website or not company:
        return response({'error': 'invalid parameters provided'}, 400)

    table = dynamodb.Table(os.environ['DYNAMODB_TABLE'])
    resp = table.get_item(Key={'_id': uid})

    new_item = {
        '_id': uid,
        'alias': alias,
        'website': website,
        'company': company
    }

    item = resp.get('Item')
    if item:
        new_item_keys = new_item.keys()
        for key in item:
            if key not in new_item_keys:
                new_item[key] = item[key]

    table.put_item(Item=new_item)

    return response(new_item, 200)
コード例 #7
0
ファイル: api.py プロジェクト: suckerpunched/api
def get_account_section(event, context):
    keys = event['path'].split('/')[3:]
    path = event['pathParameters']

    uid = path.get('_id')

    if not uid:
        return response({'error': 'invalid parameters provided'}, 400)

    table = dynamodb.Table(os.environ['DYNAMODB_TABLE'])
    resp = table.get_item(Key={'_id': uid})

    item = resp.get('Item')
    if not item:
        return response({'error': 'account does not exist'}, 404)

    section = nested_get(item, keys)
    if not section:
        return response({'error': 'section does not exist'}, 404)

    if type(section) == str:
        section = {"value": section}

    return response(section, 200)
コード例 #8
0
def delete_all_coins_route():
    with get_session() as session:
        delete_all_coins(session)

    return response(status=200, data={})
コード例 #9
0
def get_coins_route():
    with get_session() as session:
        data = {"change": get_coin_amount(session)}

    return response(status=200, data=data)
コード例 #10
0
def recebendoMsg(msg):
    id = msg['from']['id']
    print(msg)
    bot.sendMessage(id, ut.response(msg))