Skip to content

Powertrain File Service API Client

Bases: BasePowertrainAPIClient

Powertrain FileService API Client.

Swagger Documentation

Source code in reportconnectors/api_client/powertrain/file_service/__init__.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class PowertrainFileServiceAPIClient(BasePowertrainAPIClient):
    """
    Powertrain FileService API Client.

    [Swagger Documentation](https://motion-pt-dev-we-fileservice-api.azurewebsites.net/swagger/index.html)
    """

    _status_prefix = "/fileservice"

    def get_raw_data_files_attributes(
        self, asset_id: Union[int, List[int]], start_date: DateType, end_date: DateType
    ) -> RawDataFilesAttributesResponse:
        """
        Gets the list of raw data file attributes that describe the raw data files available for a given asset(s)
        in provided the time range.

        It uses the `/Report/Asset/RawData` endpoint for the requested asset_id and time range.
        The returned raw data file objects don't contain the file content, but only the download URL.

        If you want to download the file content you have two options:

        1. Run the `.get_file()` method on the `RawDataFileAttributes` response element.
        2. Use the client method `.download_raw_data_file(file_id)` to get the file content and name.

        Args:
            asset_id: Asset identifier or list of asset identifiers
            start_date: Start Date
            end_date: End Date

        Returns:
            Response object that list the attributes of found raw data files.
        """
        endpoint = "api/report/FileService/Report/Asset/RawData"

        asset_ids = asset_id if isinstance(asset_id, list) else [asset_id]

        data = {
            "assetIds": asset_ids,
            "startTime": start_date.strftime(self._datetime_format),
            "endTime": end_date.strftime(self._datetime_format),
        }
        response = self._make_request(method="POST", endpoint=endpoint, json_data=data)
        response_model = self._decode_response_to_model(response, RawDataFilesAttributesResponse)
        return response_model

    def download_raw_data_file(self, file_id: int) -> CsvRawDataFile:
        """
        Downloads the raw data file content for the given file_id.

        It uses the `/Files/{file_id}/Download` endpoint to download the file content.

        Downloaded data is a base64 encoded string that is decoded to the original file content (CSV) and name.

        Args:
            file_id: Raw data file identifier

        Returns:
            CsvFile object with raw data file content and name.
        """
        endpoint = f"/fileservice/Files/{file_id}/Download"
        response = self._make_request(method="GET", endpoint=endpoint)
        response_model = self._decode_response_to_model(response, Base64RawDataFile)
        csv_file = response_model.decode_to_csv(encoding="utf-8")
        return csv_file

    def download_raw_data_from_url(self, url: str, file_name: Optional[str] = None) -> CsvRawDataFile:
        """
        Downloads the raw data file content directly from provided URL.

        The URL is a part of response from the `get_raw_data_files_attributes` method.

        Args:
            url: The URL to download the raw data file content.
            file_name: Optional file name to use for the downloaded file.
                If not provided, the file name from the URL is used.

        Returns:
            CsvFile object with raw data file content and name.
        """
        response = self._make_request(method="GET", url=url, add_authorization=False)
        decoded_response = response.text if response else ""

        if file_name is None:
            file_name = extract_file_name_from_url(url, default="csv_file.csv")
        csv_file = CsvRawDataFile(content=decoded_response, name=file_name)
        return csv_file

download_raw_data_file(file_id)

Downloads the raw data file content for the given file_id.

It uses the /Files/{file_id}/Download endpoint to download the file content.

Downloaded data is a base64 encoded string that is decoded to the original file content (CSV) and name.

Parameters:

Name Type Description Default
file_id int

Raw data file identifier

required

Returns:

Type Description
CsvRawDataFile

CsvFile object with raw data file content and name.

Source code in reportconnectors/api_client/powertrain/file_service/__init__.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def download_raw_data_file(self, file_id: int) -> CsvRawDataFile:
    """
    Downloads the raw data file content for the given file_id.

    It uses the `/Files/{file_id}/Download` endpoint to download the file content.

    Downloaded data is a base64 encoded string that is decoded to the original file content (CSV) and name.

    Args:
        file_id: Raw data file identifier

    Returns:
        CsvFile object with raw data file content and name.
    """
    endpoint = f"/fileservice/Files/{file_id}/Download"
    response = self._make_request(method="GET", endpoint=endpoint)
    response_model = self._decode_response_to_model(response, Base64RawDataFile)
    csv_file = response_model.decode_to_csv(encoding="utf-8")
    return csv_file

download_raw_data_from_url(url, file_name=None)

Downloads the raw data file content directly from provided URL.

The URL is a part of response from the get_raw_data_files_attributes method.

Parameters:

Name Type Description Default
url str

The URL to download the raw data file content.

required
file_name Optional[str]

Optional file name to use for the downloaded file. If not provided, the file name from the URL is used.

None

Returns:

Type Description
CsvRawDataFile

CsvFile object with raw data file content and name.

Source code in reportconnectors/api_client/powertrain/file_service/__init__.py
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def download_raw_data_from_url(self, url: str, file_name: Optional[str] = None) -> CsvRawDataFile:
    """
    Downloads the raw data file content directly from provided URL.

    The URL is a part of response from the `get_raw_data_files_attributes` method.

    Args:
        url: The URL to download the raw data file content.
        file_name: Optional file name to use for the downloaded file.
            If not provided, the file name from the URL is used.

    Returns:
        CsvFile object with raw data file content and name.
    """
    response = self._make_request(method="GET", url=url, add_authorization=False)
    decoded_response = response.text if response else ""

    if file_name is None:
        file_name = extract_file_name_from_url(url, default="csv_file.csv")
    csv_file = CsvRawDataFile(content=decoded_response, name=file_name)
    return csv_file

get_raw_data_files_attributes(asset_id, start_date, end_date)

Gets the list of raw data file attributes that describe the raw data files available for a given asset(s) in provided the time range.

It uses the /Report/Asset/RawData endpoint for the requested asset_id and time range. The returned raw data file objects don't contain the file content, but only the download URL.

If you want to download the file content you have two options:

  1. Run the .get_file() method on the RawDataFileAttributes response element.
  2. Use the client method .download_raw_data_file(file_id) to get the file content and name.

Parameters:

Name Type Description Default
asset_id Union[int, List[int]]

Asset identifier or list of asset identifiers

required
start_date DateType

Start Date

required
end_date DateType

End Date

required

Returns:

Type Description
RawDataFilesAttributesResponse

Response object that list the attributes of found raw data files.

Source code in reportconnectors/api_client/powertrain/file_service/__init__.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def get_raw_data_files_attributes(
    self, asset_id: Union[int, List[int]], start_date: DateType, end_date: DateType
) -> RawDataFilesAttributesResponse:
    """
    Gets the list of raw data file attributes that describe the raw data files available for a given asset(s)
    in provided the time range.

    It uses the `/Report/Asset/RawData` endpoint for the requested asset_id and time range.
    The returned raw data file objects don't contain the file content, but only the download URL.

    If you want to download the file content you have two options:

    1. Run the `.get_file()` method on the `RawDataFileAttributes` response element.
    2. Use the client method `.download_raw_data_file(file_id)` to get the file content and name.

    Args:
        asset_id: Asset identifier or list of asset identifiers
        start_date: Start Date
        end_date: End Date

    Returns:
        Response object that list the attributes of found raw data files.
    """
    endpoint = "api/report/FileService/Report/Asset/RawData"

    asset_ids = asset_id if isinstance(asset_id, list) else [asset_id]

    data = {
        "assetIds": asset_ids,
        "startTime": start_date.strftime(self._datetime_format),
        "endTime": end_date.strftime(self._datetime_format),
    }
    response = self._make_request(method="POST", endpoint=endpoint, json_data=data)
    response_model = self._decode_response_to_model(response, RawDataFilesAttributesResponse)
    return response_model