| 122 | |
| 123 | |
| 124 | class Session(BaseConfigDict): |
| 125 | helpurl = 'https://httpie.io/docs#sessions' |
| 126 | about = 'HTTPie session file' |
| 127 | |
| 128 | def __init__( |
| 129 | self, |
| 130 | path: Union[str, Path], |
| 131 | env: Environment, |
| 132 | bound_host: str, |
| 133 | session_id: str, |
| 134 | suppress_legacy_warnings: bool = False, |
| 135 | ): |
| 136 | super().__init__(path=Path(path)) |
| 137 | |
| 138 | # Default values for the session files |
| 139 | self['headers'] = [] |
| 140 | self['cookies'] = [] |
| 141 | self['auth'] = { |
| 142 | 'type': None, |
| 143 | 'username': None, |
| 144 | 'password': None |
| 145 | } |
| 146 | |
| 147 | # Runtime state of the Session objects. |
| 148 | self.env = env |
| 149 | self._headers = HTTPHeadersDict() |
| 150 | self.cookie_jar = RequestsCookieJar( |
| 151 | # See also a temporary workaround for a Requests bug in `compat.py`. |
| 152 | policy=HTTPieCookiePolicy(), |
| 153 | ) |
| 154 | self.session_id = session_id |
| 155 | self.bound_host = bound_host |
| 156 | self.suppress_legacy_warnings = suppress_legacy_warnings |
| 157 | |
| 158 | def _add_cookies(self, cookies: List[Dict[str, Any]]) -> None: |
| 159 | for cookie in cookies: |
| 160 | domain = cookie.get('domain', '') |
| 161 | if domain is None: |
| 162 | # domain = None means explicitly lack of cookie, though |
| 163 | # requests requires domain to be a string so we'll cast it |
| 164 | # manually. |
| 165 | cookie['domain'] = '' |
| 166 | cookie['rest'] = {'is_explicit_none': True} |
| 167 | |
| 168 | self.cookie_jar.set(**cookie) |
| 169 | |
| 170 | def pre_process_data(self, data: Dict[str, Any]) -> Dict[str, Any]: |
| 171 | for key, deserializer, importer in [ |
| 172 | ('cookies', legacy_cookies.pre_process, self._add_cookies), |
| 173 | ('headers', legacy_headers.pre_process, self._headers.update), |
| 174 | ]: |
| 175 | values = data.get(key) |
| 176 | if values: |
| 177 | normalized_values = deserializer(self, values) |
| 178 | else: |
| 179 | normalized_values = [] |
| 180 | |
| 181 | importer(normalized_values) |
no outgoing calls