cliclient.py 9.43 KB
Newer Older
Mark Hewitt's avatar
Mark Hewitt committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
"""Useful functions."""

import os
import sys
from configparser import NoOptionError, NoSectionError
from functools import wraps
18
from typing import Union
Mark Hewitt's avatar
Mark Hewitt committed
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
from urllib.parse import urljoin

import requests
from osdu.client import OsduClient
from osdu.identity import OsduMsalInteractiveCredential, OsduTokenCredential
from requests.models import HTTPError

from osducli.config import (
    CLI_CONFIG_DIR,
    CONFIG_AUTHENTICATION_AUTHORITY,
    CONFIG_AUTHENTICATION_MODE,
    CONFIG_AUTHENTICATION_SCOPES,
    CONFIG_CLIENT_ID,
    CONFIG_CLIENT_SECRET,
    CONFIG_DATA_PARTITION_ID,
    CONFIG_REFRESH_TOKEN,
    CONFIG_SERVER,
    CONFIG_TOKEN_ENDPOINT,
    CLIConfig,
)
from osducli.log import get_logger
from osducli.util.exceptions import CliError

MSG_JSON_DECODE_ERROR = (
    "Unable to decode the response. Try running again with the --debug command line argument for"
    " more information"
)
MSG_HTTP_ERROR = (
47
    "An error occurred when accessing the api. Try running again with the --debug command line argument for"
Mark Hewitt's avatar
Mark Hewitt committed
48
49
50
    " more information"
)

51

Mark Hewitt's avatar
Mark Hewitt committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
logger = get_logger(__name__)


def handle_cli_exceptions(function):
    """Decorator to provide common cli error handling"""

    @wraps(function)
    def decorated(*args, **kwargs):
        try:
            return function(*args, **kwargs)
        except HTTPError as ex:
            logger.error(MSG_HTTP_ERROR)
            logger.error("Error (%s) - %s", ex.response.status_code, ex.response.reason)
        except CliError as ex:
            logger.error("Error %s", ex.message)
        except ValueError as ex:
            logger.error(MSG_JSON_DECODE_ERROR)
Mark Hewitt's avatar
Mark Hewitt committed
69
            logger.error(ex)
Mark Hewitt's avatar
Mark Hewitt committed
70
71
        except (NoOptionError, NoSectionError) as ex:
            logger.warning(
Mark Hewitt's avatar
Mark Hewitt committed
72
                "Configuration missing from config ('%s'). Run 'osdu config update'", ex.args[0]
Mark Hewitt's avatar
Mark Hewitt committed
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
            )
        sys.exit(1)

    return decorated


class CliOsduClient(OsduClient):
    """Specific class for use from the CLI that provides common error handling, use of configuration
    and messaging

    Args:
        OsduClient ([type]): [description]
    """

    def __init__(self, config: CLIConfig):
        """Setup the new client

        Args:
            config (CLIConfig): cli configuration
        """

        self.config = config

        try:
            # required
            server = config.get("core", CONFIG_SERVER)
            data_partition = config.get("core", CONFIG_DATA_PARTITION_ID)
            retries = config.getint("core", "retries", 0)
            authentication_mode = config.get("core", CONFIG_AUTHENTICATION_MODE)

            if authentication_mode == "refresh_token":
                client_id = config.get("core", CONFIG_CLIENT_ID)
                token_endpoint = config.get("core", CONFIG_TOKEN_ENDPOINT, None)
                refresh_token = config.get("core", CONFIG_REFRESH_TOKEN, None)
                client_secret = config.get("core", CONFIG_CLIENT_SECRET, None)
                credentials = OsduTokenCredential(
                    client_id, token_endpoint, refresh_token, client_secret
                )
            elif authentication_mode == "msal_interactive":
                client_id = config.get("core", CONFIG_CLIENT_ID)
                authority = config.get("core", CONFIG_AUTHENTICATION_AUTHORITY, None)
                scopes = config.get("core", CONFIG_AUTHENTICATION_SCOPES, None)
                cache_path = os.path.join(CLI_CONFIG_DIR, "msal_token_cache.bin")
                credentials = OsduMsalInteractiveCredential(
                    client_id, authority, scopes, cache_path
                )
            else:
                logger.error(
Mark Hewitt's avatar
Mark Hewitt committed
121
                    "Unknown type of authentication mode %s. Run 'osdu config update'.",
Mark Hewitt's avatar
Mark Hewitt committed
122
123
124
125
126
127
128
                    authentication_mode,
                )
                sys.exit(2)

            super().__init__(server, data_partition, credentials, retries)
        except (NoOptionError, NoSectionError) as ex:
            logger.warning(
Mark Hewitt's avatar
Mark Hewitt committed
129
                "Authentication information missing from config ('%s'). Run 'osdu config update'",
Mark Hewitt's avatar
Mark Hewitt committed
130
131
132
133
                ex.args[0],
            )
            sys.exit(1)

Mark Hewitt's avatar
Mark Hewitt committed
134
    def url_from_config(self, config_url_key: str, url_extra_path: str) -> str:
Mark Hewitt's avatar
Mark Hewitt committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
        """Construct a url using values from configuration"""
        unit_url = self.config.get("core", config_url_key)
        url = urljoin(self.server_url, unit_url) + url_extra_path
        return url

    def cli_get(
        self, config_url_key: str, url_extra_path: str, ok_status_codes: list = None
    ) -> requests.Response:
        """[summary]

        Args:
            config_url_key (str): key in configuration for the base path
            url_extra_path (str): extra path to add to the base path
            ok_status_codes (list, optional): Status codes for successful call. Defaults to [200].
        """
Mark Hewitt's avatar
Mark Hewitt committed
150
        url = self.url_from_config(config_url_key, url_extra_path)
Mark Hewitt's avatar
Mark Hewitt committed
151
        response = self.get(url)
Mark Hewitt's avatar
Mark Hewitt committed
152
        if ok_status_codes is not None and response.status_code not in ok_status_codes:
Mark Hewitt's avatar
Mark Hewitt committed
153
154
155
            raise HTTPError(response=response)
        return response

Mark Hewitt's avatar
Mark Hewitt committed
156
157
158
    def cli_get_returning_json(
        self, config_url_key: str, url_extra_path: str, ok_status_codes: list = None
    ) -> dict:
Mark Hewitt's avatar
Mark Hewitt committed
159
160
161
162
163
        """[summary]

        Args:
            config_url_key (str): key in configuration for the base path
            url_extra_path (str): extra path to add to the base path
Mark Hewitt's avatar
Mark Hewitt committed
164
            ok_status_codes (list, optional): accepted ok response codes. Defaults to None.
Mark Hewitt's avatar
Mark Hewitt committed
165
        """
Mark Hewitt's avatar
Mark Hewitt committed
166
        url = self.url_from_config(config_url_key, url_extra_path)
Mark Hewitt's avatar
Mark Hewitt committed
167
        return self.get_returning_json(url, ok_status_codes)
Mark Hewitt's avatar
Mark Hewitt committed
168
169
170
171
172
173
174

    def cli_post_returning_json(
        self,
        config_url_key: str,
        url_extra_path: str,
        data: Union[str, dict],
        ok_status_codes: list = None,
Mark Hewitt's avatar
Mark Hewitt committed
175
    ) -> dict:
Mark Hewitt's avatar
Mark Hewitt committed
176
177
178
179
180
181
182
183
184
        """[summary]

        Args:
            config_url_key (str): key in configuration for the base path
            url_extra_path (str): extra path to add to the base path
            data (Union[str, dict]): json data as string or dict to send as the body
            ok_status_codes (list, optional): Status codes indicating successful call. Defaults to [200].

        Returns:
Mark Hewitt's avatar
Mark Hewitt committed
185
            dict: returned json
Mark Hewitt's avatar
Mark Hewitt committed
186
        """
Mark Hewitt's avatar
Mark Hewitt committed
187
        url = self.url_from_config(config_url_key, url_extra_path)
Mark Hewitt's avatar
Mark Hewitt committed
188
        return self.post_returning_json(url, data, ok_status_codes)
Mark Hewitt's avatar
Mark Hewitt committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

    def cli_delete(
        self,
        config_url_key: str,
        url_extra_path: str,
        ok_status_codes: list = None,
    ):
        """[summary]

        Args:
            config_url_key (str): key in configuration for the base path
            url_extra_path (str): extra path to add to the base path
            data (Union[str, dict]): json data as string or dict to send as the body
            ok_status_codes (list, optional): Status codes indicating successful call. Defaults to [200].

        Returns:
            [type]: [description]
        """
        try:
Mark Hewitt's avatar
Mark Hewitt committed
208
            url = self.url_from_config(config_url_key, url_extra_path)
Mark Hewitt's avatar
Mark Hewitt committed
209
210
211
212
213
214
215
216
217
218
            self.delete(url, ok_status_codes)
            return
        except HTTPError as ex:
            logger.error(MSG_HTTP_ERROR)
            logger.error("Error (%s) - %s", ex.response.status_code, ex.response.reason)
        except ValueError as ex:
            logger.error(MSG_JSON_DECODE_ERROR)
            logger.debug(ex)
        except (NoOptionError, NoSectionError) as ex:
            logger.warning(
Mark Hewitt's avatar
Mark Hewitt committed
219
                "Configuration missing from config ('%s'). Run 'osdu config update'", ex.args[0]
Mark Hewitt's avatar
Mark Hewitt committed
220
221
222
223
224
225
            )

        sys.exit(1)

    def cli_put(
        self,
226
227
228
229
230
231
        config_url_key: str,
        url_extra_path: str,
        data: Union[str, dict],
        ok_status_codes: list = None,
    ) -> requests.Response:
        """[summary]
Mark Hewitt's avatar
Mark Hewitt committed
232
233
234
235

        Args:
            config_url_key (str): key in configuration for the base path
            url_extra_path (str): extra path to add to the base path
236
            ok_status_codes (list, optional): Status codes for successful call. Defaults to [200].
Mark Hewitt's avatar
Mark Hewitt committed
237
        """
Mark Hewitt's avatar
Mark Hewitt committed
238
        url = self.url_from_config(config_url_key, url_extra_path)
239
240
241
242
        response = self.put(url, data)
        if ok_status_codes is not None and response.status_code not in ok_status_codes:
            raise HTTPError(response=response)
        return response
Mark Hewitt's avatar
Mark Hewitt committed
243

244
245
246
247
248
249
250
251
    def cli_put_returning_json(
        self,
        config_url_key: str,
        url_extra_path: str,
        data: Union[str, dict],
        ok_status_codes: list = None,
    ) -> dict:
        """PUT the data to the given url.
Mark Hewitt's avatar
Mark Hewitt committed
252

253
254
255
256
257
258
259
260
261
        Args:
            config_url_key (str): key in configuration for the base path
            url_extra_path (str): extra path to add to the base path
            data (Union[str, dict]): data to send
            ok_status_codes (list, optional): accepted ok response codes. Defaults to None.

        Returns:
            dict: returned json
        """
Mark Hewitt's avatar
Mark Hewitt committed
262
        url = self.url_from_config(config_url_key, url_extra_path)
263
        return self.put_returning_json(url, data, ok_status_codes)