def generate_secured_api_key(parent_api_key, restrictions):
        # type: (str, dict) -> str

        query_parameters = QueryParametersSerializer.serialize(restrictions)

        secured_key = hmac.new(parent_api_key.encode('utf-8'),
                               query_parameters.encode('utf-8'),
                               hashlib.sha256).hexdigest()

        base64encoded = base64.b64encode(
            ("{}{}".format(secured_key, query_parameters)).encode('utf-8'))

        return str(base64encoded.decode('utf-8'))
    def generate_secured_api_key(parent_api_key, restrictions):
        # type: (str, dict) -> str

        query_parameters = QueryParametersSerializer.serialize(
            restrictions)

        secured_key = hmac.new(parent_api_key.encode('utf-8'),
                               query_parameters.encode('utf-8'),
                               hashlib.sha256).hexdigest()

        base64encoded = base64.b64encode(
            ("{}{}".format(secured_key, query_parameters)).encode('utf-8')
        )

        return str(base64encoded.decode('utf-8'))
    def request(self, verb, hosts, path, data, request_options, timeout):
        # type: (str, List[Host], str, Optional[Union[dict, list]], RequestOptions, int) -> dict # noqa: E501

        if isinstance(data, dict):
            data.update(request_options.data)

        query_parameters = dict(request_options.query_parameters)
        if verb == Verb.GET:
            query_parameters.update(request_options.data)

        relative_url = '{}?{}'.format(
            path, QueryParametersSerializer.serialize(query_parameters))

        request = Request(verb.upper(), request_options.headers, data,
                          self._config.connect_timeout, timeout)

        return self.retry(hosts, request, relative_url)
    def request(self, verb, hosts, path, data, request_options, timeout):
        # type: (str, List[Host], str, Optional[Union[dict, list]], RequestOptions, int) -> dict # noqa: E501

        if isinstance(data, dict):
            data.update(request_options.data)

        query_parameters = dict(request_options.query_parameters)
        if verb == Verb.GET:
            query_parameters.update(request_options.data)

        relative_url = '{}?{}'.format(path,
                                      QueryParametersSerializer.serialize(
                                          query_parameters
                                      ))

        request = Request(verb.upper(), request_options.headers, data,
                          self._config.connect_timeout, timeout)

        return self.retry(hosts, request, relative_url)
    def test_multiple_operations(self):
        index_name1 = self.index.name

        index_2 = F.index(self._testMethodName)
        index_name2 = index_2.name

        raw_response = self.client.multiple_batch([{
            "indexName": index_name1,
            "action": "addObject",
            "body": {
                "firstname": "Jimmie"
            }
        }, {
            "indexName": index_name1,
            "action": "addObject",
            "body": {
                "firstname": "Jimmie"
            }
        }, {
            "indexName": index_name2,
            "action": "addObject",
            "body": {
                "firstname": "Jimmie"
            }
        }, {
            "indexName": index_name2,
            "action": "addObject",
            "body": {
                "firstname": "Jimmie"
            }
        }]).wait().raw_response

        object_ids = list(
            map(lambda object_id: object_id, raw_response['objectIDs']))

        objects = self.client.multiple_get_objects([{
            "indexName": index_name1,
            "objectID": object_ids[0]
        }, {
            "indexName": index_name1,
            "objectID": object_ids[1]
        }, {
            "indexName": index_name2,
            "objectID": object_ids[2]
        }, {
            "indexName": index_name2,
            "objectID": object_ids[3]
        }])['results']

        self.assertEqual(objects[0]['objectID'], object_ids[0])
        self.assertEqual(objects[1]['objectID'], object_ids[1])
        self.assertEqual(objects[2]['objectID'], object_ids[2])
        self.assertEqual(objects[3]['objectID'], object_ids[3])

        results = self.client.multiple_queries([
            {
                "indexName":
                index_name1,
                "params":
                QueryParametersSerializer.serialize({
                    "query": "",
                    "hitsPerPage": 2
                })
            },
            {
                "indexName":
                index_name2,
                "params":
                QueryParametersSerializer.serialize({
                    "query": "",
                    "hitsPerPage": 2
                })
            },
        ], {'strategy': 'none'})['results']

        self.assertEqual(len(results), 2)
        self.assertEqual(len(results[0]['hits']), 2)
        self.assertEqual(results[0]['nbHits'], 4)
        self.assertEqual(len(results[1]['hits']), 2)
        self.assertEqual(results[1]['nbHits'], 4)

        results = self.client.multiple_queries(
            [{
                "indexName":
                index_name1,
                "params":
                QueryParametersSerializer.serialize({
                    "query": "",
                    "hitsPerPage": 2
                })
            }, {
                "indexName":
                index_name2,
                "params":
                QueryParametersSerializer.serialize({
                    "query": "",
                    "hitsPerPage": 2
                })
            }], {'strategy': 'stopIfEnoughMatches'})['results']

        self.assertEqual(len(results), 2)
        self.assertEqual(len(results[0]['hits']), 2)
        self.assertEqual(results[0]['nbHits'], 4)
        self.assertEqual(len(results[1]['hits']), 0)
        self.assertEqual(results[1]['nbHits'], 0)

        index_2.delete()
    def test_result(self):
        expected = "boolean=true&dict=%7B%22foo%22%3A+%22bar%22%7D"

        data = QueryParametersSerializer.serialize(self.data)
        self.assertEqual(data, expected)
    def test_multiple_operations(self):
        index_name1 = self.index.name

        index_2 = F.index(self._testMethodName)
        index_name2 = index_2.name

        raw_response = self.client.multiple_batch([
            {"indexName": index_name1, "action": "addObject",
             "body": {"firstname": "Jimmie"}},
            {"indexName": index_name1, "action": "addObject",
             "body": {"firstname": "Jimmie"}},
            {"indexName": index_name2, "action": "addObject",
             "body": {"firstname": "Jimmie"}},
            {"indexName": index_name2, "action": "addObject",
             "body": {"firstname": "Jimmie"}}
        ]).wait().raw_response

        object_ids = list(
            map(lambda object_id: object_id, raw_response['objectIDs']))

        objects = self.client.multiple_get_objects([
            {"indexName": index_name1, "objectID": object_ids[0]},
            {"indexName": index_name1, "objectID": object_ids[1]},
            {"indexName": index_name2, "objectID": object_ids[2]},
            {"indexName": index_name2, "objectID": object_ids[3]}
        ])['results']

        self.assertEqual(objects[0]['objectID'], object_ids[0])
        self.assertEqual(objects[1]['objectID'], object_ids[1])
        self.assertEqual(objects[2]['objectID'], object_ids[2])
        self.assertEqual(objects[3]['objectID'], object_ids[3])

        results = self.client.multiple_queries([
            {"indexName": index_name1,
             "params": QueryParametersSerializer.serialize(
                 {"query": "", "hitsPerPage": 2})},
            {"indexName": index_name2,
             "params": QueryParametersSerializer.serialize(
                 {"query": "", "hitsPerPage": 2})},
        ], {'strategy': 'none'})['results']

        self.assertEqual(len(results), 2)
        self.assertEqual(len(results[0]['hits']), 2)
        self.assertEqual(results[0]['nbHits'], 4)
        self.assertEqual(len(results[1]['hits']), 2)
        self.assertEqual(results[1]['nbHits'], 4)

        results = self.client.multiple_queries([
            {"indexName": index_name1,
             "params": QueryParametersSerializer.serialize(
                 {"query": "", "hitsPerPage": 2})},
            {"indexName": index_name2,
             "params": QueryParametersSerializer.serialize(
                 {"query": "", "hitsPerPage": 2})}

        ], {'strategy': 'stopIfEnoughMatches'})['results']

        self.assertEqual(len(results), 2)
        self.assertEqual(len(results[0]['hits']), 2)
        self.assertEqual(results[0]['nbHits'], 4)
        self.assertEqual(len(results[1]['hits']), 0)
        self.assertEqual(results[1]['nbHits'], 0)

        index_2.delete()
    def test_result(self):
        expected = 'boolean=true&dict=%7B%22foo%22%3A+%22bar%22%7D'

        data = QueryParametersSerializer.serialize(self.data)
        self.assertEqual(data, expected)
示例#9
0
    def test_multiple_operations(self):
        index2 = F.index(self.client, "{}2".format(self._testMethodName))
        index_name1 = self.index.name
        index_name2 = index2.name

        raw_response = (
            self.client.multiple_batch(
                [
                    {
                        "indexName": index_name1,
                        "action": "addObject",
                        "body": {"firstname": "Jimmie"},
                    },  # noqa: E501
                    {
                        "indexName": index_name1,
                        "action": "addObject",
                        "body": {"firstname": "Jimmie"},
                    },  # noqa: E501
                    {
                        "indexName": index_name2,
                        "action": "addObject",
                        "body": {"firstname": "Jimmie"},
                    },  # noqa: E501
                    {
                        "indexName": index_name2,
                        "action": "addObject",
                        "body": {"firstname": "Jimmie"},
                    },  # noqa: E501
                ]
            )
            .wait()
            .raw_response
        )

        object_ids = list(map(lambda object_id: object_id, raw_response["objectIDs"]))

        objects = self.client.multiple_get_objects(
            [
                {"indexName": index_name1, "objectID": object_ids[0]},
                {"indexName": index_name1, "objectID": object_ids[1]},
                {"indexName": index_name2, "objectID": object_ids[2]},
                {"indexName": index_name2, "objectID": object_ids[3]},
            ]
        )["results"]

        self.assertEqual(objects[0]["objectID"], object_ids[0])
        self.assertEqual(objects[1]["objectID"], object_ids[1])
        self.assertEqual(objects[2]["objectID"], object_ids[2])
        self.assertEqual(objects[3]["objectID"], object_ids[3])

        results = self.client.multiple_queries(
            [
                {
                    "indexName": index_name1,
                    "params": QueryParametersSerializer.serialize(
                        {"query": "", "hitsPerPage": 2}
                    ),  # noqa: E501
                },
                {
                    "indexName": index_name2,
                    "params": QueryParametersSerializer.serialize(
                        {"query": "", "hitsPerPage": 2}
                    ),  # noqa: E501
                },
            ],
            {"strategy": "none"},
        )["results"]

        self.assertEqual(len(results), 2)
        self.assertEqual(len(results[0]["hits"]), 2)
        self.assertEqual(results[0]["nbHits"], 2)
        self.assertEqual(len(results[1]["hits"]), 2)
        self.assertEqual(results[1]["nbHits"], 2)

        results = self.client.multiple_queries(
            [
                {
                    "indexName": index_name1,
                    "params": QueryParametersSerializer.serialize(
                        {"query": "", "hitsPerPage": 2}
                    ),  # noqa: E501
                },
                {
                    "indexName": index_name2,
                    "params": QueryParametersSerializer.serialize(
                        {"query": "", "hitsPerPage": 2}
                    ),  # noqa: E501
                },
            ],
            {"strategy": "stopIfEnoughMatches"},
        )["results"]

        self.assertEqual(len(results), 2)
        self.assertEqual(len(results[0]["hits"]), 2)
        self.assertEqual(results[0]["nbHits"], 2)
        self.assertEqual(len(results[1]["hits"]), 0)
        self.assertEqual(results[1]["nbHits"], 0)