Ejemplo n.º 1
0
def test_episodes_generator_low_limit(mocked_episodes_db):
    m = MagicMock()
    mocked_episodes_db.client.get_paginator.return_value = m
    m.paginate.return_value = [
        {
            "Items": [json_util.dumps({"ep_name": "ep_1"})]
        },
        {
            "Items": [json_util.dumps({"ep_name": "ep_2"})]
        },
        {
            "Items": [json_util.dumps({"ep_name": "ep_3"})]
        },
    ]

    eps = []
    for p in mocked_episodes_db._episodes_generator(TEST_ANIME_ID, 3):
        eps += p

    assert eps == [{
        "ep_name": "ep_1"
    }, {
        "ep_name": "ep_2"
    }, {
        "ep_name": "ep_3"
    }]
Ejemplo n.º 2
0
    def _set_property(self, key: str, value: Any):
        k = key.split('.').pop(0)
        attribute_names = {
            f'#val_{i}': v
            for i, v in enumerate(key.split('.'))
        }
        attribute_names['#val_0'] = 'value'
        path = '.'.join([f'#val_{i}' for i, _ in enumerate(key.split('.'))])

        params = {
            'TableName':
            self._ddb_table,
            'Key':
            json_util.dumps({
                'pk': k,
                'sk': 'CacheItem',
            }, as_dict=True),
            'UpdateExpression':
            f'SET {path} = :val',
            'ExpressionAttributeValues':
            json_util.dumps({
                ':val': value,
            }, as_dict=True),
            'ExpressionAttributeNames':
            attribute_names,
            'ReturnValues':
            'ALL_NEW',
        }

        ret = self._ddb_client.update_item(**params)
        if 'Attributes' in ret:
            return json_util.loads(ret['Attributes'], as_dict=True)['value']

        return None
Ejemplo n.º 3
0
    def increment(self, key: str, amount: int = 1, **kwargs) -> Any:
        k = key.split('.').pop(0)
        attribute_names, path = self._process_path(key)

        params = {
            'TableName':
            self._ddb_table,
            'Key':
            json_util.dumps({
                'pk': k,
                'sk': 'CacheItem',
            }, as_dict=True),
            'UpdateExpression':
            f'SET {path} = {path} + :inc',
            'ExpressionAttributeValues':
            json_util.dumps({
                ':inc': amount,
            }, as_dict=True),
            'ExpressionAttributeNames':
            attribute_names,
            'ReturnValues':
            'ALL_NEW',
        }

        ret = self._ddb_client.update_item(**params)
        if 'Attributes' in ret:
            return json_util.loads(ret['Attributes'], as_dict=True)['value']

        return None
Ejemplo n.º 4
0
def create_event():
    event_id = request.json.get('eventId')
    evento_nome = request.json.get('eventoNome')
    evento_data = request.json.get('eventoData')
    evento_filas = request.json.get('eventoFilas')
    evento_servicos = request.json.get('eventoServicos')
    evento_visitantes = request.json.get('eventoVisitantes')

    if not event_id or not evento_nome:
        return jsonify({'error': 'Please provide eventId and eventoNome'}), 400

    response = client.put_item(TableName=EVENTS_TABLE,
                               Item={
                                   'eventId': {
                                       'S': event_id
                                   },
                                   'eventoNome': {
                                       'S': evento_nome
                                   },
                                   'eventoData': {
                                       'S': evento_data
                                   },
                                   'eventoFilas': {
                                       'M':
                                       dynamo_json.dumps(dct=evento_filas,
                                                         as_dict=True)
                                   },
                                   'eventoServicos': {
                                       'M':
                                       dynamo_json.dumps(dct=evento_filas,
                                                         as_dict=True)
                                   },
                                   'eventoVisitantes': {
                                       'M':
                                       dynamo_json.dumps(dct=evento_filas,
                                                         as_dict=True)
                                   }
                               })

    return jsonify({
        'eventId': {
            'S': event_id
        },
        'eventoNome': {
            'S': evento_nome
        },
        'eventoData': {
            'S': evento_data
        },
        'eventoFilas': {
            'M': evento_filas
        },
        'eventoServicos': {
            'M': evento_servicos
        },
        'eventoVisitantes': {
            'M': evento_visitantes
        }
    })
Ejemplo n.º 5
0
def create_medicao():
    # idUsuario = uuid.uuid4() # gera um id aleatorio
    login = request.json.get('login')
    idPlantacao = request.json.get('idPlantacao')
    idSensor = request.json.get('idSensor')
    horaMedicao = request.json.get('horaMedicao')
    medicao = dynamodb_json.dumps(dct=request.json.get('medicao'),
                                  as_dict=True)

    resp = client.update_item(
        TableName=USERS_TABLE,
        Key={
            'login': {
                'S': login
            },
        },
        UpdateExpression=
        "SET #plantacoes.#idPlantacao.#sensores.#idSensor.#medicoes.#horaMedicao = :dict",
        ExpressionAttributeNames={
            "#plantacoes": "plantacoes",
            "#idPlantacao": idPlantacao,
            "#idSensor": idSensor,
            "#sensores": "sensores",
            "#medicoes": "medicoes",
            "#horaMedicao": horaMedicao
        },
        ExpressionAttributeValues={":dict": {
            "M": medicao
        }},
        ConditionExpression=
        'attribute_not_exists(#plantacoes.#idPlantacao.#sensores.#idSensor.#medicoes.#horaMedicao)',
        ReturnValues="ALL_NEW")

    return resp
Ejemplo n.º 6
0
def lambda_handler(event, context):
    """
    Wrapper for remote (AWS) implementation of DOU articles capture. No input is required.
    This function loads the configuration from DynamoDB, runs the capture driver and 
    updates the configuration in DynamoDB. The schedule is controled from outside by cron 
    from CloudWatch.
    """
    client = boto3.client('dynamodb')

    # Load config from AWS DynamoDB:
    config = client.get_item(TableName="configs",
                             Key={'name': {
                                 'S': 'capture_DOU'
                             }})
    config = dyjson.loads(config)['Item']

    # Run DOU articles capture:
    updated_config = cd.capture_DOU_driver(config)

    # Save config to AWS DynamoDB:
    response = client.put_item(TableName="configs",
                               Item=dyjson.dumps(updated_config, as_dict=True))

    # Immediately call this function again if capturing next article batch in AWS:
    if gs.local == False and updated_config['next_batch'] == True:
        time.sleep(5)
        print('Calling next batch')
        lambd = boto3.client('lambda')
        lambd.invoke(
            FunctionName=
            'arn:aws:lambda:us-east-1:085250262607:function:capture_dou:PROD',
            #FunctionName='arn:aws:lambda:us-east-1:085250262607:function:capture_dou:DEV',
            InvocationType='Event')
def handler(event, _):
    request = unmarshall_api_gateway_event(event)
    response = None

    if not event or not 'body' in event:
        print('No event body')
        return Response(400)
    json_ = json.loads(request.body)
    ddb_payload = ddb_json.dumps(json_)
    try:
        print('Preparing to save data')
        ddb = boto3.client('dynamodb')
        result = ddb.put_item(
            TableName='data-table',
            Item=json.loads(ddb_payload)
        )
        if not result:
            print('No results found')
            response = Response(400)
        else:
            response = Response(200)
    except Exception as err:
        print(err)
        response = Response(500) 
    
    print('Sending response')
    print(response)
    return response.__dict__
Ejemplo n.º 8
0
    def remove(self, key: str, value: Any, **kwargs) -> Any:
        parts = key.split('.')
        item = self.get(parts.pop(0))
        if item is None:
            return None

        while len(parts) > 0:
            item = item[parts.pop(0)]
        index = item.index(value)

        if index is None:
            return None

        k = key.split('.').pop(0)
        attribute_names, path = self._process_path(key)

        params = {
            'TableName': self._ddb_table,
            'Key': json_util.dumps({
                'pk': k,
                'sk': 'CacheItem',
            },
                                   as_dict=True),
            'UpdateExpression': f'REMOVE {path}[{index}]',
            'ExpressionAttributeNames': attribute_names,
            'ReturnValues': 'ALL_NEW',
        }

        ret = self._ddb_client.update_item(**params)
        if 'Attributes' in ret:
            return json_util.loads(ret['Attributes'], as_dict=True)['value']

        return None
Ejemplo n.º 9
0
def entrypoint(params):
    """
    Input:   params (dict)
             Com as keywords 'dynamo_table' e 'config_key'
    Retorna: lista de dicts com url e path
    
    Atualiza a config no dynamoDB
    """

    # Load config from dynamoDB:
    if params['use_config']:
        config = load_remote_config(params['dynamo_table'],
                                    params['config_key'])
        config['update_config'] = True
    # Or use directly supplied parameters:
    else:
        config = params
        config['update_config'] = False

    # Get list of articles to download and update config:
    url_file_list, next_config = get_articles_url(config)

    # Save config to AWS DynamoDB:
    if params['use_config']:
        client = boto3.client('dynamodb')
        response = client.put_item(TableName=params['dynamo_table'],
                                   Item=dyjson.dumps(next_config,
                                                     as_dict=True))

    return url_file_list
Ejemplo n.º 10
0
def submitProfile(event, context):
    event_body = json.loads(event['body'])
    dynamo_item = dynamo_json.dumps({
        'username':
        event_body['username'],
        'selected_type':
        event_body['selected_type'],
        'requested_at':
        str(datetime.datetime.now())
    })

    try:
        res = dynamodb.put_item(
            TableName=os.environ['DYNAMO_TABLE_NAME'],
            Item=json.loads(dynamo_item),
            ConditionExpression='attribute_not_exists(username)')
    except Exception as e:  # V2: enhance cancel for different response
        print(e)
        response = {
            "statusCode": 502,
            "headers": {
                "Content-Type": "application/json",
            },
            "body": json.dumps({"message": "Something went wrong on our end"})
        }
    else:
        response = {
            "statusCode": 200,
            "headers": {
                "Content-Type": "application/json",
            },
            "body": json.dumps({"message": "Profile successfully updated"})
        }

    return response
Ejemplo n.º 11
0
    def _set_item(self,
                  key: str,
                  value: Any,
                  ttl: int = None,
                  if_not_exists: bool = False):
        item = {
            'pk': key,
            'sk': 'CacheItem',
            'value': value,
        }

        if ttl is not None:
            item['TimeToLive'] = (datetime.now() +
                                  timedelta(seconds=ttl)).timestamp()

        params = {
            'TableName': self._ddb_table,
            'Item': json_util.dumps(item, as_dict=True),
            'ReturnValues': 'ALL_OLD',
        }

        if if_not_exists is True:
            params['KeyConditionExpression'] = 'if_not_exists(pk)'

        ret = self._ddb_client.put_item(**params)
        if 'Attributes' in ret:
            return json_util.loads(ret['Attributes'], as_dict=True)

        return None
Ejemplo n.º 12
0
    def _getParamsForInsertRecord(self, included_id_data):
        params = {
            "TableName": self._table_name,
            "Item": json.loads(json_util.dumps(included_id_data)),
            "ReturnValues": "ALL_OLD"
        }

        return params
Ejemplo n.º 13
0
def test_get_episodes_with_to_small_offset(mocked_episodes_db):
    m = MagicMock()
    mocked_episodes_db.client.get_paginator.return_value = m
    m.paginate.return_value = [
        {
            "Items": [json_util.dumps({"ep_name": "ep_1"})]
        },
        {
            "Items": [json_util.dumps({"ep_name": "ep_2"})]
        },
        {
            "Items": [json_util.dumps({"ep_name": "ep_3"})]
        },
    ]

    with pytest.raises(mocked_episodes_db.InvalidStartOffset):
        mocked_episodes_db.get_episodes("123", limit=1, start=0)
Ejemplo n.º 14
0
def test_get_episodes_with_offset(mocked_episodes_db):
    m = MagicMock()
    mocked_episodes_db.client.get_paginator.return_value = m
    m.paginate.return_value = [
        {
            "Items": [json_util.dumps({"ep_name": "ep_1"})]
        },
        {
            "Items": [json_util.dumps({"ep_name": "ep_2"})]
        },
        {
            "Items": [json_util.dumps({"ep_name": "ep_3"})]
        },
    ]

    ret = mocked_episodes_db.get_episodes("123", limit=1, start=2)
    assert ret == {"items": [{"ep_name": "ep_2"}], "total_pages": 3}
Ejemplo n.º 15
0
def create_challenge(data):
    client = boto3.client('dynamodb')
    if 'id' not in data:
        data['id'] = id(int(datetime.datetime.now().timestamp()))
    if 'createDate' not in data:
        data['createDate'] = int(datetime.datetime.now().timestamp())
    client.put_item(TableName='challenges', Item=json.loads(dynamo_json.dumps(data)))
    return data
Ejemplo n.º 16
0
 def transform_json_files(self, input_path: str, output_path: str):
     for json_file in self.get_json_files(input_path):
         try:
             with open(f"{input_path}/{json_file}", "r") as file:
                 template_json = json.loads(file.read())
                 self.write_file_to_folder(json.dumps(template_json),
                                           output_path, json_file)
         except IOError as err:
             raise err
Ejemplo n.º 17
0
def store_in_cachce(object):
    """Stores an object by its id on DynamoDB"""

    client = boto3.client('dynamodb')

    response = client.put_item(TableName='serverless_crawler_index',
                               Item=json.loads(dynamo_json.dumps(object)))

    return response
Ejemplo n.º 18
0
def add_review(event, context):
    try:
        location_id = event['queryStringParameters']['location_id']
        if not location_id:
            return {
                'statusCode': 404,
                'body': json.dumps(context),
            }

        item = client.get_item(
            TableName=TABLE_NAME,
            Key={
                'location_id': {
                    'S': location_id,
                },
            },
        )

        location = json_util.loads(item)['Item']

        body_data = json.loads(event['body'])
        username = body_data['username']
        text = body_data['text']
        stars = body_data['stars']

        reviews = location['reviews']
        reviews.append({
            'username': username,
            'text': text,
            'stars': stars,
        })
        location['reviews'] = reviews

        dynamo_payload = json.loads(json_util.dumps(location))

        response = client.put_item(
            TableName=TABLE_NAME,
            Item=dynamo_payload,
        )

        return {
            'statusCode': 201,
            'headers': {
                'Access-Control-Allow-Origin': '*',
            },
            'body': json.dumps(response),
        }

    except KeyError:
        return {
            'statusCode': 400,
            'headers': {
                'Access-Control-Allow-Origin': '*',
            },
            'body': json.dumps(context),
        }
Ejemplo n.º 19
0
def createOrder(orderline):
    #write the order to db
    response = dynamodb.put_item(TableName=order_table,
                                 Item=json.loads(jsonn.dumps(orderline)))
    print(response)
    #table.put_item(orderline)
    print("inserted")
    res = moncash.payment(orderline['id'], orderline['amount'])
    print(res.get_response())
    return res.get_response()
Ejemplo n.º 20
0
def update_challenges(data):
    client = boto3.client('dynamodb')
    response = client.update_item(
        TableName='challenges',
        Key=json.loads(dynamo_json.dumps({'id': data['id']})),
        ExpressionAttributeValues=utils.get_expression_attribute_values(data),
        UpdateExpression=utils.generate_update_expression(data),
        ExpressionAttributeNames=utils.generate_expression_attribute_names(data),
        ReturnValues='ALL_NEW'
    )
    return dynamo_json.loads(response['Attributes'])
Ejemplo n.º 21
0
    def get(self, key: str, **kwargs) -> Any:
        response = self._ddb_client.get_item(
            TableName=self._ddb_table,
            Key=json_util.dumps({
                'pk': key,
                'sk': 'CacheItem',
            }, as_dict=True),
        )

        if 'Item' in response:
            return json_util.loads(response['Item'], as_dict=True)['value']

        return None
Ejemplo n.º 22
0
    def add(self, key: str, value: Any, **kwargs) -> Any:
        k = key.split('.').pop(0)
        attribute_names = {
            f'#val_{i}': v
            for i, v in enumerate(key.split('.'))
        }
        attribute_names['#val_0'] = 'value'
        path = '.'.join([f'#val_{i}' for i, _ in enumerate(key.split('.'))])

        params = {
            'TableName':
            self._ddb_table,
            'Key':
            json_util.dumps({
                'pk': k,
                'sk': 'CacheItem',
            }, as_dict=True),
            'UpdateExpression':
            f'SET {path} = list_append(if_not_exists({path}, :empty_list), :val)',
            'ExpressionAttributeValues':
            json_util.dumps({
                ':val': [value],
                ':empty_list': [],
            },
                            as_dict=True),
            'ExpressionAttributeNames':
            attribute_names,
            'ReturnValues':
            'ALL_NEW',
        }

        ret = self._ddb_client.update_item(**params)
        if 'Attributes' in ret:
            return json_util.loads(ret['Attributes'], as_dict=True)['value']

        return None
Ejemplo n.º 23
0
    def delete(self, key: str, **kwargs) -> Any:
        response = self._ddb_client.delete_item(TableName=self._ddb_table,
                                                Key=json_util.dumps(
                                                    {
                                                        'pk': key,
                                                        'sk': 'CacheItem',
                                                    },
                                                    as_dict=True),
                                                ReturnValues='ALL_OLD')

        if 'Attributes' in response:
            return json_util.loads(response['Attributes'],
                                   as_dict=True)['value']

        return None
Ejemplo n.º 24
0
def start_of_day(*args, **kwargs):
    start_of_day_data = {}
    client = get_trello_client()
    for list_id in lists:
        trello_list = client.get_list(list_id)
        card_ids = [str(card.id) for card in trello_list.list_cards()]
        start_of_day_data[list_id] = card_ids

    dynamodb_client.put_item(TableName=dynamodb_table_name,
                             Item=d_json.dumps(
                                 {
                                     'date': get_db_key(),
                                     'start_of_day': start_of_day_data,
                                 },
                                 as_dict=True))
Ejemplo n.º 25
0
    def deleteScores(self, newScores):
        pe = "hotel_id, aspect, score"

        try:
            response = self.dynamodb.scan(TableName=self.tableName,
                                          ProjectionExpression=pe)
            self.ddbScores = dynamodb_json.loads(response['Items'])
            if len(response['Items']) > 0:
                while 'LastEvaluatedKey' in response:
                    response = self.dynamodb.scan(
                        TableName=self.tableName,
                        ProjectionExpression=pe,
                        ExclusiveStartKey=response['LastEvaluatedKey'],
                        ConsistentRead=True,
                        ReturnConsumedCapacity=True)
                    for item in response['Items']:
                        self.ddbScores.extend(dynamodb_json.loads(item))
        except Exception as e:
            LOG.error("Error scanning Scores table : " + str(e))
            raise Exception(str(e))

        new_scores = [{
            'hotel_id': item['hotel_id'],
            'aspect': item['aspect']
        } for item in newScores]
        ddb_scores = [{
            'hotel_id': item['hotel_id'],
            'aspect': item['aspect']
        } for item in self.ddbScores]

        removed_scores = list(
            itertools.filterfalse(lambda x: x in new_scores, ddb_scores))

        if len(removed_scores) > 0:
            for score in removed_scores:
                score = json.loads(dynamodb_json.dumps(score))
                try:
                    self.dynamodb.delete_item(
                        TableName=self.tableName,
                        Key={
                            'hotel_id': score['hotel_id'],
                            'aspect': score['aspect'],
                        },
                    )
                except Exception as e:
                    LOG.error("Error deleting scores in Scores table : " +
                              str(e))
                    raise Exception(str(e))
Ejemplo n.º 26
0
def updateDataResource(payload):

    dynamo_req = dynamo_json.dumps(payload)

    res_dynamo = dynamodb.put_item(
        #TableName=os.environ['DYNAMO_TABLE_NAME'],
        TableName='garuda_hacks_2020_table',
        Item=json.loads(dynamo_req))

    res_es = es.index(index="garuda_hacks_2020",
                      id=payload['username'],
                      body=payload)

    _generateSiteMapXML(payload)

    return {"message": "success"}
Ejemplo n.º 27
0
    def upSertScore(self, item):
        item = json.loads(dynamodb_json.dumps(item))
        try:
            response = self.dynamodb.update_item(
                TableName=self.tableName,
                Key={
                    'hotel_id': item['hotel_id'],
                    'aspect': item['aspect']
                },
                UpdateExpression="set score = :score",
                ExpressionAttributeValues={':score': item['score']})
        except Exception as e:
            LOG.error("Error updating/inserting Scores table : " + str(e))
            raise Exception(str(e))

        return response
Ejemplo n.º 28
0
def add_location(event, context):
    try:
        body_data = json.loads(event['body'])
        location_name = body_data['location_name']
        longitude = body_data['longitude']
        latitude = body_data['latitude']
        username = body_data['username']
        description = body_data['description']

        payload = {
            'location_id': location_name,
            'name': location_name,
            'username': username,
            'description': description,
            'pos': {
                'lng': longitude,
                'lat': latitude,
            },
            'reviews': [],
        }

        dynamo_payload = json.loads(json_util.dumps(payload))

        response = client.put_item(
            TableName=TABLE_NAME,
            Item=dynamo_payload,
        )

        return {
            'statusCode': 201,
            'headers': {
                'Access-Control-Allow-Origin': '*',
            },
            'body': json.dumps(response),
        }

    except KeyError:
        return {
            'statusCode': 400,
            'headers': {
                'Access-Control-Allow-Origin': '*',
            },
            'body': json.dumps(context),
        }
Ejemplo n.º 29
0
def create_dynamodb_credentials(org_short_name, username, password,
                                permissions):
    assert isinstance(permissions, list)

    client = get_boto_client('dynamodb')
    item = {
        'organization': org_short_name,
        'credentials': [{
            'p': password,
            'u': username,
            'permissions': permissions
        }]
    }

    dynamodb_json = dynamodb_json_util.dumps(item)
    dynamodb_json = json.loads(dynamodb_json)

    client.put_item(TableName=settings.S3_USER_CREDENTIALS_TABLE,
                    Item=dynamodb_json)
Ejemplo n.º 30
0
def create_user(user):
    default_amount = 100000

    db_client.put_item(
        TableName=table_name,
        Item=json.loads(
            djson.dumps(
                {
                    "pk": user,
                    "sk": "user",
                    "buying_power": default_amount,
                    "wins": 0,
                    "losses": 0,
                    "realised_pnl": 0,
                }
            )
        ),
    )

    return default_amount