|
10 | 10 |
|
11 | 11 |
|
12 | 12 | class Config(object): |
13 | | - api_url = None |
14 | | - api_key = None |
15 | | - products = [] |
| 13 | + instance = None # Global instance |
16 | 14 |
|
17 | 15 | def __init__( |
18 | 16 | self, |
19 | 17 | api_url=None, |
20 | 18 | api_key=None, |
21 | 19 | products=None, |
22 | | - file=None, |
23 | | - **kwargs |
| 20 | + file=None |
24 | 21 | ): |
25 | 22 | """ |
26 | 23 | initialization config for public api |
27 | 24 | :param api_url: Public api url |
28 | 25 | :param api_key: Service user ApiKey |
29 | 26 | :param products (optional): Id products |
30 | 27 | :param file: Config file path |
31 | | - :param kwargs: |
32 | 28 | """ |
33 | | - if not all([Config.api_key, Config.api_url]): |
34 | | - self.load(api_url, api_key, products, file, **kwargs) |
35 | | - |
36 | | - def load(self, api_url, api_key, products, file, **kwargs): |
37 | | - if not any([api_key, api_url, file]): |
38 | | - return |
39 | | - |
40 | | - if file: |
41 | | - return self.load_from_file(file) |
42 | | - |
43 | | - self.check_credentials(api_url, api_key, products) |
44 | | - self._set_attr(api_url, api_key, products) |
45 | | - |
46 | | - @staticmethod |
47 | | - def _set_attr(api_url, api_key, products=None): |
48 | | - Config.api_key = api_key |
49 | | - |
50 | | - # URL must end with a character |
51 | | - Config.api_url = api_url |
52 | | - if products: |
53 | | - Config.products = [products] if isinstance( |
54 | | - products, str) else products |
55 | | - |
56 | | - @staticmethod |
57 | | - def check_credentials(api_url, api_key, products): |
58 | | - """ |
59 | | - :param api_url: |
60 | | - :param api_key: |
61 | | - :param products: |
62 | | - :return: True or raise ValueError |
63 | | - """ |
64 | | - if not all([api_key, api_url]): |
65 | | - raise ValueError('Please provide your credentials.' |
66 | | - 'Not set value for `api_key` or `api_url`') |
67 | | - |
| 29 | + # Check arguments |
| 30 | + if not file and not any([api_key, api_url]): |
| 31 | + raise ValueError('Filename or api_key and api_url are expected' |
| 32 | + 'in Config initialization') |
68 | 33 | if products and not isinstance(products, (str, list)): |
69 | 34 | raise TypeError('Products can be string or string list. Found type ' |
70 | 35 | + type(products).__name__) |
71 | 36 |
|
72 | | - return |
73 | | - |
74 | | - def load_from_file(self, file): |
75 | | - """ |
76 | | - Format file (json): { |
77 | | - "api_key": |
78 | | - "api_url": |
79 | | - "products" (optional): |
80 | | - } |
81 | | - :param file (str): Path to the file |
82 | | - :return: Init configuration parameters. |
83 | | - Set Config.api_url/.api_key/.products |
84 | | - """ |
85 | | - if not os.path.exists(file): |
86 | | - raise IOError('Not file `{}` on directory'.format(file)) |
87 | | - |
88 | | - with open(file) as config_file: |
89 | | - configs = config_file.read() |
90 | | - |
91 | | - try: |
92 | | - configs = json.loads(configs) |
93 | | - except Exception as ex: |
94 | | - raise TypeError('Invalid config file `{}`\n' |
95 | | - 'ERROR: {}'.format(file, str(ex))) |
96 | | - |
97 | | - (api_url, api_key, products) = (configs.get('apiEndpoint', ''), |
98 | | - configs.get('apiKey', ''), |
99 | | - configs.get('products', '')) |
100 | | - |
101 | | - products = products.encode('utf-8') if not isinstance(products, (str, list)) else products |
102 | | - api_url = api_url.encode('utf-8') if not isinstance(api_url, str) else api_url |
103 | | - api_key = api_key.encode('utf-8') if not isinstance(api_key, str) else api_key |
104 | | - |
105 | | - self.check_credentials(api_url, api_key, products) |
106 | | - self._set_attr(api_url, api_key, products) |
| 37 | + # Load config from file name |
| 38 | + if file: |
| 39 | + if not os.path.exists(file): |
| 40 | + raise IOError('Not file `{}` on directory'.format(file)) |
| 41 | + |
| 42 | + with open(file) as config_file: |
| 43 | + configs = config_file.read() |
| 44 | + |
| 45 | + try: |
| 46 | + configs = json.loads(configs) |
| 47 | + except Exception as ex: |
| 48 | + raise TypeError('Invalid config file `{}`\n' |
| 49 | + 'ERROR: {}'.format(file, str(ex))) |
| 50 | + |
| 51 | + (api_url, api_key, products) = (configs.get('apiEndpoint', ''), |
| 52 | + configs.get('apiKey', ''), |
| 53 | + configs.get('products', '')) |
| 54 | + api_url = api_url.encode('utf-8') if not isinstance(api_url, str) else api_url |
| 55 | + api_key = api_key.encode('utf-8') if not isinstance(api_key, str) else api_key |
| 56 | + products = products.encode('utf-8') \ |
| 57 | + if not isinstance(products, (str, list)) \ |
| 58 | + else products |
| 59 | + |
| 60 | + # Initialize |
| 61 | + self._api_key = api_key |
| 62 | + self._api_url = api_url |
| 63 | + self._products = [products] \ |
| 64 | + if isinstance(products, str) \ |
| 65 | + else products or [] |
| 66 | + |
| 67 | + # Store first created instance |
| 68 | + if not Config.instance: |
| 69 | + Config.instance = self |
| 70 | + |
| 71 | + @property |
| 72 | + def api_url(self): |
| 73 | + return self._api_url |
| 74 | + |
| 75 | + @property |
| 76 | + def api_key(self): |
| 77 | + return self._api_key |
| 78 | + |
| 79 | + @property |
| 80 | + def products(self): |
| 81 | + return self._products |
0 commit comments