You've already forked httpie-cli
mirror of
https://github.com/httpie/cli.git
synced 2025-08-10 22:42:05 +02:00
Python 3 annotations, super(), pathlib, etc.
This commit is contained in:
@@ -3,16 +3,20 @@
|
||||
"""
|
||||
import re
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
from requests.auth import AuthBase
|
||||
from requests.cookies import RequestsCookieJar, create_cookie
|
||||
import requests
|
||||
|
||||
from httpie.config import BaseConfigDict, DEFAULT_CONFIG_DIR
|
||||
from httpie.plugins import plugin_manager
|
||||
|
||||
|
||||
SESSIONS_DIR_NAME = 'sessions'
|
||||
DEFAULT_SESSIONS_DIR = os.path.join(DEFAULT_CONFIG_DIR, SESSIONS_DIR_NAME)
|
||||
DEFAULT_SESSIONS_DIR = DEFAULT_CONFIG_DIR / SESSIONS_DIR_NAME
|
||||
VALID_SESSION_NAME_PATTERN = re.compile('^[a-zA-Z0-9_.-]+$')
|
||||
# Request headers starting with these prefixes won't be stored in sessions.
|
||||
# They are specific to each request.
|
||||
@@ -20,8 +24,13 @@ VALID_SESSION_NAME_PATTERN = re.compile('^[a-zA-Z0-9_.-]+$')
|
||||
SESSION_IGNORED_HEADER_PREFIXES = ['Content-', 'If-']
|
||||
|
||||
|
||||
def get_response(requests_session, session_name,
|
||||
config_dir, args, read_only=False):
|
||||
def get_response(
|
||||
requests_session: requests.Session,
|
||||
session_name: str,
|
||||
config_dir: Path,
|
||||
args,
|
||||
read_only=False,
|
||||
) -> requests.Response:
|
||||
"""Like `client.get_responses`, but applies permanent
|
||||
aspects of the session to the request.
|
||||
|
||||
@@ -38,10 +47,10 @@ def get_response(requests_session, session_name,
|
||||
|
||||
# host:port => host_port
|
||||
hostname = hostname.replace(':', '_')
|
||||
path = os.path.join(config_dir,
|
||||
SESSIONS_DIR_NAME,
|
||||
hostname,
|
||||
session_name + '.json')
|
||||
path = (
|
||||
config_dir / SESSIONS_DIR_NAME / hostname /
|
||||
(session_name + '.json')
|
||||
)
|
||||
|
||||
session = Session(path)
|
||||
session.load()
|
||||
@@ -77,9 +86,9 @@ class Session(BaseConfigDict):
|
||||
helpurl = 'https://httpie.org/doc#sessions'
|
||||
about = 'HTTPie session file'
|
||||
|
||||
def __init__(self, path, *args, **kwargs):
|
||||
super(Session, self).__init__(*args, **kwargs)
|
||||
self._path = path
|
||||
def __init__(self, path: Union[str, Path]):
|
||||
super().__init__()
|
||||
self._path = Path(path)
|
||||
self['headers'] = {}
|
||||
self['cookies'] = {}
|
||||
self['auth'] = {
|
||||
@@ -88,10 +97,10 @@ class Session(BaseConfigDict):
|
||||
'password': None
|
||||
}
|
||||
|
||||
def _get_path(self):
|
||||
def _get_path(self) -> Path:
|
||||
return self._path
|
||||
|
||||
def update_headers(self, request_headers):
|
||||
def update_headers(self, request_headers: dict):
|
||||
"""
|
||||
Update the session headers with the request ones while ignoring
|
||||
certain name prefixes.
|
||||
@@ -102,7 +111,7 @@ class Session(BaseConfigDict):
|
||||
for name, value in request_headers.items():
|
||||
|
||||
if value is None:
|
||||
continue # Ignore explicitely unset headers
|
||||
continue # Ignore explicitly unset headers
|
||||
|
||||
value = value.decode('utf8')
|
||||
if name == 'User-Agent' and value.startswith('HTTPie/'):
|
||||
@@ -115,11 +124,11 @@ class Session(BaseConfigDict):
|
||||
self['headers'][name] = value
|
||||
|
||||
@property
|
||||
def headers(self):
|
||||
def headers(self) -> dict:
|
||||
return self['headers']
|
||||
|
||||
@property
|
||||
def cookies(self):
|
||||
def cookies(self) -> RequestsCookieJar:
|
||||
jar = RequestsCookieJar()
|
||||
for name, cookie_dict in self['cookies'].items():
|
||||
jar.set_cookie(create_cookie(
|
||||
@@ -128,10 +137,7 @@ class Session(BaseConfigDict):
|
||||
return jar
|
||||
|
||||
@cookies.setter
|
||||
def cookies(self, jar):
|
||||
"""
|
||||
:type jar: CookieJar
|
||||
"""
|
||||
def cookies(self, jar: RequestsCookieJar):
|
||||
# https://docs.python.org/2/library/cookielib.html#cookie-objects
|
||||
stored_attrs = ['value', 'path', 'secure', 'expires']
|
||||
self['cookies'] = {}
|
||||
@@ -142,7 +148,7 @@ class Session(BaseConfigDict):
|
||||
}
|
||||
|
||||
@property
|
||||
def auth(self):
|
||||
def auth(self) -> Optional[AuthBase]:
|
||||
auth = self.get('auth', None)
|
||||
if not auth or not auth['type']:
|
||||
return
|
||||
@@ -171,6 +177,6 @@ class Session(BaseConfigDict):
|
||||
return plugin.get_auth(**credentials)
|
||||
|
||||
@auth.setter
|
||||
def auth(self, auth):
|
||||
def auth(self, auth: dict):
|
||||
assert {'type', 'raw_auth'} == auth.keys()
|
||||
self['auth'] = auth
|
||||
|
Reference in New Issue
Block a user