|
2 | 2 | # Use of this source code is governed by a MIT license that can be found in the LICENSE file.
|
3 | 3 | # SPDX-License-Identifier: MIT
|
4 | 4 |
|
5 |
| -from typing import List, Optional, Union, Any, Tuple |
| 5 | +from typing import List, Optional, Union, Any, Tuple, Dict |
6 | 6 | import logging
|
7 | 7 |
|
8 | 8 | from dataclasses import dataclass, field
|
@@ -77,18 +77,34 @@ class Endpoint(BaseResponse):
|
77 | 77 |
|
78 | 78 | method: Optional[str] = field(default="POST")
|
79 | 79 | url: str = field(default="")
|
80 |
| - headers: Optional[List[Header]] = field( |
| 80 | + headers: Optional[Union[Dict[str, str], List[Header]]] = field( |
81 | 81 | default=None, metadata=dataclass_config(exclude=lambda f: f is None)
|
82 | 82 | )
|
83 | 83 |
|
84 | 84 | def __getitem__(self, key):
|
85 | 85 | _dict = self.to_dict()
|
86 | 86 | if "headers" in _dict:
|
87 |
| - _dict["headers"] = [ |
88 |
| - Header.from_dict(headers) for headers in _dict["headers"] |
89 |
| - ] |
| 87 | + if isinstance(self.headers, list): |
| 88 | + _dict["headers"] = [ |
| 89 | + Header.from_dict(headers) if isinstance(headers, dict) else headers |
| 90 | + for headers in _dict["headers"] |
| 91 | + ] |
| 92 | + elif isinstance(self.headers, dict): |
| 93 | + _dict["headers"] = self.headers |
90 | 94 | return _dict[key]
|
91 | 95 |
|
| 96 | + def to_dict(self) -> dict: |
| 97 | + """ |
| 98 | + Convert the endpoint to a dictionary, properly handling headers. |
| 99 | + """ |
| 100 | + result = super().to_dict() |
| 101 | + if self.headers: |
| 102 | + if isinstance(self.headers, dict): |
| 103 | + result["headers"] = self.headers |
| 104 | + else: |
| 105 | + result["headers"] = {h.key: h.value for h in self.headers} |
| 106 | + return result |
| 107 | + |
92 | 108 |
|
93 | 109 | @dataclass
|
94 | 110 | class Function(BaseResponse):
|
|
0 commit comments