|
| 1 | +"""Implementation of SystemLink Feeds Client.""" |
| 2 | + |
| 3 | +from typing import BinaryIO, List, Optional |
| 4 | + |
| 5 | +from nisystemlink.clients import core |
| 6 | +from nisystemlink.clients.core._uplink._base_client import BaseClient |
| 7 | +from nisystemlink.clients.core._uplink._methods import delete, get, post |
| 8 | +from uplink import Part, Path, Query |
| 9 | + |
| 10 | +from . import models |
| 11 | + |
| 12 | + |
| 13 | +class FeedsClient(BaseClient): |
| 14 | + def __init__(self, configuration: Optional[core.HttpConfiguration] = None): |
| 15 | + """Initialize an instance. |
| 16 | +
|
| 17 | + Args: |
| 18 | + configuration: Defines the web server to connect to and information about |
| 19 | + how to connect. If not provided, the |
| 20 | + :class:`HttpConfigurationManager <nisystemlink.clients.core.HttpConfigurationManager>` |
| 21 | + is used to obtain the configuration. |
| 22 | +
|
| 23 | + Raises: |
| 24 | + ApiException: if unable to communicate with the Feeds Service. |
| 25 | + """ |
| 26 | + if configuration is None: |
| 27 | + configuration = core.HttpConfigurationManager.get_configuration() |
| 28 | + |
| 29 | + super().__init__(configuration, base_path="/nifeed/v1/") |
| 30 | + |
| 31 | + @post("feeds") |
| 32 | + def create_feed(self, feed: models.CreateFeedRequest) -> models.Feed: |
| 33 | + """Create a new feed with the provided feed details. |
| 34 | +
|
| 35 | + Args: |
| 36 | + feeds (models.CreateFeedsRequest): Request to create the feed. |
| 37 | +
|
| 38 | + Returns: |
| 39 | + models.Feed: Details of the created feed. |
| 40 | +
|
| 41 | + Raises: |
| 42 | + ApiException: if unable to communicate with the Feeds Service. |
| 43 | + """ |
| 44 | + ... |
| 45 | + |
| 46 | + @get("feeds", args=[Query, Query]) |
| 47 | + def __query_feeds( |
| 48 | + self, |
| 49 | + platform: Optional[str] = None, |
| 50 | + workspace: Optional[str] = None, |
| 51 | + ) -> models.QueryFeedsResponse: |
| 52 | + """Lists available feeds for the Platform `platform` under the Workspace `workspace`. |
| 53 | +
|
| 54 | + Args: |
| 55 | + platform (Optional[str]): Information about system platform. Defaults to None. |
| 56 | + workspace (Optional[str]): Workspace id. Defaults to None. |
| 57 | +
|
| 58 | + Returns: |
| 59 | + models.QueryFeedsResponse: List of feeds. |
| 60 | +
|
| 61 | + Raises: |
| 62 | + ApiException: if unable to communicate with the Feeds Service. |
| 63 | + """ |
| 64 | + ... |
| 65 | + |
| 66 | + def query_feeds( |
| 67 | + self, |
| 68 | + platform: Optional[models.Platform] = None, |
| 69 | + workspace: Optional[str] = None, |
| 70 | + ) -> List[models.Feed]: |
| 71 | + """Lists available feeds for the Platform `platform` under the Workspace `workspace`. |
| 72 | +
|
| 73 | + Args: |
| 74 | + platform (Optional[models.Platform]): Information about system platform. |
| 75 | + Defaults to None. |
| 76 | + workspace (Optional[str]): Workspace id. Defaults to None. |
| 77 | +
|
| 78 | + Returns: |
| 79 | + List[models.Feed]: List of feeds. |
| 80 | +
|
| 81 | + Raises: |
| 82 | + ApiException: if unable to communicate with the Feeds Service. |
| 83 | + """ |
| 84 | + platform_by_str = platform.value if platform is not None else None |
| 85 | + response = self.__query_feeds( |
| 86 | + platform=platform_by_str, |
| 87 | + workspace=workspace, |
| 88 | + ).feeds |
| 89 | + |
| 90 | + return response |
| 91 | + |
| 92 | + @post( |
| 93 | + "feeds/{feedId}/packages", |
| 94 | + args=[Path(name="feedId"), Part(), Query(name="shouldOverwrite")], |
| 95 | + ) |
| 96 | + def __upload_package( |
| 97 | + self, |
| 98 | + feed_id: str, |
| 99 | + package: Part, |
| 100 | + overwrite: Query = False, |
| 101 | + ) -> models.Package: |
| 102 | + """Upload package to SystemLink feed. |
| 103 | +
|
| 104 | + Args: |
| 105 | + feed_id (str): ID of the feed. |
| 106 | + package (Part): Package file to be uploaded. |
| 107 | + overwrite (Query): Set to True, to overwrite the package if it already exists. |
| 108 | + Defaults to False. |
| 109 | +
|
| 110 | + Returns: |
| 111 | + models.Package: Uploaded package information. |
| 112 | +
|
| 113 | + Raises: |
| 114 | + ApiException: if unable to communicate with the Feeds Service. |
| 115 | + """ |
| 116 | + ... |
| 117 | + |
| 118 | + def upload_package( |
| 119 | + self, |
| 120 | + feed_id: str, |
| 121 | + package_file_path: str, |
| 122 | + overwrite: bool = False, |
| 123 | + ) -> models.Package: |
| 124 | + """Upload package to SystemLink feed. |
| 125 | +
|
| 126 | + Args: |
| 127 | + feed_id (str): ID of the feed. |
| 128 | + package_file_path (str): File path of the package to be uploaded. |
| 129 | + overwrite (bool): Set to True, to overwrite the package if it already exists. |
| 130 | + Defaults to False. |
| 131 | +
|
| 132 | + Returns: |
| 133 | + models.Package: Uploaded package information. |
| 134 | +
|
| 135 | + Raises: |
| 136 | + ApiException: if unable to communicate with the Feeds Service. |
| 137 | + OSError: if the file does not exist or cannot be opened. |
| 138 | + """ |
| 139 | + response = self.__upload_package( |
| 140 | + feed_id=feed_id, |
| 141 | + overwrite=overwrite, |
| 142 | + package=open(package_file_path, "rb"), |
| 143 | + ) |
| 144 | + |
| 145 | + return response |
| 146 | + |
| 147 | + def upload_package_content( |
| 148 | + self, |
| 149 | + feed_id: str, |
| 150 | + package: BinaryIO, |
| 151 | + overwrite: bool = False, |
| 152 | + ) -> models.Package: |
| 153 | + """Upload package to SystemLink feed. |
| 154 | +
|
| 155 | + Args: |
| 156 | + feed_id (str): ID of the feed. |
| 157 | + package (BinaryIO): Package file to be uploaded. |
| 158 | + overwrite (bool): Set to True, to overwrite the package if it already exists. |
| 159 | + Defaults to False. |
| 160 | +
|
| 161 | + Returns: |
| 162 | + models.Package: Uploaded package information. |
| 163 | +
|
| 164 | + Raises: |
| 165 | + ApiException: if unable to communicate with the Feeds Service. |
| 166 | + """ |
| 167 | + response = self.__upload_package( |
| 168 | + feed_id=feed_id, |
| 169 | + overwrite=overwrite, |
| 170 | + package=package, |
| 171 | + ) |
| 172 | + |
| 173 | + return response |
| 174 | + |
| 175 | + @delete( |
| 176 | + "feeds/{feedId}", |
| 177 | + args=[Path(name="feedId")], |
| 178 | + ) |
| 179 | + def delete_feed(self, id: str) -> None: |
| 180 | + """Delete feed and its packages. |
| 181 | +
|
| 182 | + Args: |
| 183 | + id (str): ID of the feed to be deleted. |
| 184 | +
|
| 185 | + Returns: |
| 186 | + None. |
| 187 | +
|
| 188 | + Raises: |
| 189 | + ApiException: if unable to communicate with the Feeds Service. |
| 190 | + """ |
| 191 | + ... |
0 commit comments