|
14 | 14 | from arangoasync.connection import Connection
|
15 | 15 | from arangoasync.errno import HTTP_FORBIDDEN, HTTP_NOT_FOUND
|
16 | 16 | from arangoasync.exceptions import (
|
| 17 | + AnalyzerCreateError, |
| 18 | + AnalyzerDeleteError, |
| 19 | + AnalyzerGetError, |
| 20 | + AnalyzerListError, |
17 | 21 | AsyncJobClearError,
|
18 | 22 | AsyncJobListError,
|
19 | 23 | CollectionCreateError,
|
@@ -1461,6 +1465,133 @@ def response_handler(resp: Response) -> bool:
|
1461 | 1465 |
|
1462 | 1466 | return await self._executor.execute(request, response_handler)
|
1463 | 1467 |
|
| 1468 | + async def analyzers(self) -> Result[Jsons]: |
| 1469 | + """List all analyzers in the database. |
| 1470 | +
|
| 1471 | + Returns: |
| 1472 | + list: List of analyzers with their properties. |
| 1473 | +
|
| 1474 | + Raises: |
| 1475 | + AnalyzerListError: If the operation fails. |
| 1476 | +
|
| 1477 | + References: |
| 1478 | + - `list-all-analyzers <https://docs.arangodb.com/stable/develop/http-api/analyzers/#list-all-analyzers>`__ |
| 1479 | + """ # noqa: E501 |
| 1480 | + request = Request(method=Method.GET, endpoint="/_api/analyzer") |
| 1481 | + |
| 1482 | + def response_handler(resp: Response) -> Jsons: |
| 1483 | + if resp.is_success: |
| 1484 | + result: Jsons = self.deserializer.loads(resp.raw_body)["result"] |
| 1485 | + return result |
| 1486 | + raise AnalyzerListError(resp, request) |
| 1487 | + |
| 1488 | + return await self._executor.execute(request, response_handler) |
| 1489 | + |
| 1490 | + async def analyzer(self, name: str) -> Result[Json]: |
| 1491 | + """Return analyzer details. |
| 1492 | +
|
| 1493 | + Args: |
| 1494 | + name (str): Analyzer name. |
| 1495 | +
|
| 1496 | + Returns: |
| 1497 | + dict: Analyzer properties. |
| 1498 | +
|
| 1499 | + References: |
| 1500 | + - `get-an-analyzer-definition <https://docs.arangodb.com/stable/develop/http-api/analyzers/#get-an-analyzer-definition>`__ |
| 1501 | + """ # noqa: E501 |
| 1502 | + request = Request(method=Method.GET, endpoint=f"/_api/analyzer/{name}") |
| 1503 | + |
| 1504 | + def response_handler(resp: Response) -> Json: |
| 1505 | + if not resp.is_success: |
| 1506 | + raise AnalyzerGetError(resp, request) |
| 1507 | + return Response.format_body(self.deserializer.loads(resp.raw_body)) |
| 1508 | + |
| 1509 | + return await self._executor.execute(request, response_handler) |
| 1510 | + |
| 1511 | + async def create_analyzer( |
| 1512 | + self, |
| 1513 | + name: str, |
| 1514 | + analyzer_type: str, |
| 1515 | + properties: Optional[Json] = None, |
| 1516 | + features: Optional[Sequence[str]] = None, |
| 1517 | + ) -> Result[Json]: |
| 1518 | + """Create an analyzer. |
| 1519 | +
|
| 1520 | + Args: |
| 1521 | + name (str): Analyzer name. |
| 1522 | + analyzer_type (str): Type of the analyzer (e.g., "text", "identity"). |
| 1523 | + properties (dict | None): Properties of the analyzer. |
| 1524 | + features (list | None): The set of features to set on the Analyzer |
| 1525 | + generated fields. The default value is an empty array. Possible values: |
| 1526 | + "frequency", "norm", "position", "offset". |
| 1527 | +
|
| 1528 | + Returns: |
| 1529 | + dict: Analyzer properties. |
| 1530 | +
|
| 1531 | + Raises: |
| 1532 | + AnalyzerCreateError: If the operation fails. |
| 1533 | +
|
| 1534 | + References: |
| 1535 | + - `create-an-analyzer <https://docs.arangodb.com/stable/develop/http-api/analyzers/#create-an-analyzer>`__ |
| 1536 | + """ # noqa: E501 |
| 1537 | + data: Json = {"name": name, "type": analyzer_type} |
| 1538 | + if properties is not None: |
| 1539 | + data["properties"] = properties |
| 1540 | + if features is not None: |
| 1541 | + data["features"] = features |
| 1542 | + |
| 1543 | + request = Request( |
| 1544 | + method=Method.POST, |
| 1545 | + endpoint="/_api/analyzer", |
| 1546 | + data=self.serializer.dumps(data), |
| 1547 | + ) |
| 1548 | + |
| 1549 | + def response_handler(resp: Response) -> Json: |
| 1550 | + if not resp.is_success: |
| 1551 | + raise AnalyzerCreateError(resp, request) |
| 1552 | + return self.deserializer.loads(resp.raw_body) |
| 1553 | + |
| 1554 | + return await self._executor.execute(request, response_handler) |
| 1555 | + |
| 1556 | + async def delete_analyzer( |
| 1557 | + self, name: str, force: Optional[bool] = None, ignore_missing: bool = False |
| 1558 | + ) -> Result[bool]: |
| 1559 | + """Delete an analyzer. |
| 1560 | +
|
| 1561 | + Args: |
| 1562 | + name (str): Analyzer name. |
| 1563 | + force (bool | None): Remove the analyzer configuration even if in use. |
| 1564 | + ignore_missing (bool): Do not raise an exception on missing analyzer. |
| 1565 | +
|
| 1566 | + Returns: |
| 1567 | + bool: `True` if the analyzer was deleted successfully, `False` if the |
| 1568 | + analyzer was not found and **ignore_missing** was set to `True`. |
| 1569 | +
|
| 1570 | + Raises: |
| 1571 | + AnalyzerDeleteError: If the operation fails. |
| 1572 | +
|
| 1573 | + References: |
| 1574 | + - `remove-an-analyzer <https://docs.arangodb.com/stable/develop/http-api/analyzers/#remove-an-analyzer>`__ |
| 1575 | + """ # noqa: E501 |
| 1576 | + params: Params = {} |
| 1577 | + if force is not None: |
| 1578 | + params["force"] = force |
| 1579 | + |
| 1580 | + request = Request( |
| 1581 | + method=Method.DELETE, |
| 1582 | + endpoint=f"/_api/analyzer/{name}", |
| 1583 | + params=params, |
| 1584 | + ) |
| 1585 | + |
| 1586 | + def response_handler(resp: Response) -> bool: |
| 1587 | + if resp.is_success: |
| 1588 | + return True |
| 1589 | + if resp.status_code == HTTP_NOT_FOUND and ignore_missing: |
| 1590 | + return False |
| 1591 | + raise AnalyzerDeleteError(resp, request) |
| 1592 | + |
| 1593 | + return await self._executor.execute(request, response_handler) |
| 1594 | + |
1464 | 1595 | async def has_user(self, username: str) -> Result[bool]:
|
1465 | 1596 | """Check if a user exists.
|
1466 | 1597 |
|
|
0 commit comments