Module couchdb3.utils
Global variables
var COUCHDB_GLOBAL_CHANGES_DB_NAME : str-
Reserved CouchDB global changes database name.
var COUCHDB_REPLICATOR_DB_NAME : str-
Reserved CouchDB replicator database name.
var COUCHDB_USERS_DB_NAME : str-
Reserved CouchDB users database name.
var COUCH_DB_RESERVED_DB_NAMES : Set[str]-
Reserved CouchDB database names.
var COUCH_DB_RESERVED_DOC_FIELDS : Set[str]-
Reserved CouchDB document fields.
var DEFAULT_AUTH_METHOD : str-
The default authentication method - values to
"cookie". var DEFAULT_TIMEOUT : int-
The default timeout set in requests - values to
300. var PATTERN_DB_NAME : re.Pattern-
The pattern for valid database names.
var PATTERN_USER_ID : re.Pattern-
The pattern for valid user IDs.
var VALID_AUTH_METHODS : Set[str]-
The valid auth method arguments. Possible values are
"basic"or"cookie". var VALID_SCHEMES : Set[str]-
The valid TCP schemes. Possible values are
"http"or"https"or"socks5".
Functions
def basic_auth(user: str, password: str) ‑> str-
Expand source code
def basic_auth( user: str, password: str ) -> str: """ Create basic authentication headers value. Parameters ---------- user : str A CouchDB user name. password : str A corresponding CouchDB user password. Returns ------- str : The credentials concatenated with a colon and base64 encoded. """ return base64.b64encode(f"{user}:{password}".encode()).decode()Create basic authentication headers value.
Parameters
user:str- A CouchDB user name.
password:str- A corresponding CouchDB user password.
Returns
str : The credentials concatenated with a colon and base64 encoded.
def build_query(**kwargs) ‑> str | None-
Expand source code
def build_query( **kwargs, ) -> Optional[str]: """ Parameters ---------- kwargs Arbitrary keyword-args to be passed as query-params in a URL. Returns ------- str : A string containing the keyword-args encoded as URL query-params. """ return parse.urlencode({key: _handler(val) for key, val in kwargs.items() if val is not None})Parameters
kwargs- Arbitrary keyword-args to be passed as query-params in a URL.
Returns
str : A string containing the keyword-args encoded as URL query-params.
def build_url(*, scheme: str, host: str, path: str = None, port: int = None, **kwargs) ‑> urllib3.util.url.Url-
Expand source code
def build_url( *, scheme: str, host: str, path: str = None, port: int = None, **kwargs, ) -> Url: """ Build a URL using the provided scheme, host, path & kwargs. Parameters ---------- scheme : str The URL scheme (e.g `http`). host : str The URL host (e.g. `example.com`). path : str The URL path (e.g. `/api/data`). Default `None`. port : int The port to connect to (e.g. `5984`). Default `None`. kwargs Arbitrary keyword-args to be passed as query-params in a URL. Returns ------- Url : An instance of `Url`. """ return Url( scheme=scheme, host=host, port=port, path=path, query=build_query(**kwargs), )Build a URL using the provided scheme, host, path & kwargs.
Parameters
scheme:str- The URL scheme (e.g
http). host:str- The URL host (e.g.
example.com). path:str- The URL path (e.g.
/api/data). DefaultNone. port:int- The port to connect to (e.g.
5984). DefaultNone. kwargs- Arbitrary keyword-args to be passed as query-params in a URL.
Returns
Url : An instance of
Url. def check_response(response: requests.models.Response) ‑> None-
Expand source code
def check_response(response: requests.Response) -> None: """ Check if a request yields a successful response. Parameters ---------- response : requests.Response A `requests.Response` object. Returns ------- None Raises ------ One of the following exceptions: - couchdb3.error.CouchDBError - ConnectionError - TimeoutError - requests.exceptions.ConnectionError - requests.exceptions.HTTPError """ try: response.raise_for_status() except ( ConnectionError, TimeoutError, requests.exceptions.ConnectionError, requests.exceptions.HTTPError, ) as err: if response.status_code in exceptions.STATUS_CODE_ERROR_MAPPING: _ = exceptions.STATUS_CODE_ERROR_MAPPING[response.status_code] if _: raise _(response.text) else: return None raise errCheck if a request yields a successful response.
Parameters
response:requests.Response- A
requests.Responseobject.
Returns
None
Raises
Oneofthe following exceptions:
- couchdb3.error.CouchDBError
- ConnectionError
- TimeoutError
- requests.exceptions.ConnectionError
- requests.exceptions.HTTPError
def extract_url_data(url: str) ‑> Dict-
Expand source code
def extract_url_data(url: str) -> Dict: """ Extract scheme, credentials, host, port & path from a URL. Parameters ---------- url : str A URL string. Returns ------- Dict : A dictionary containing with the following items. - scheme - user - password - host - port - path """ if not any(url.startswith(_) for _ in VALID_SCHEMES): url = f"http://{url}" parsed = parse_url(url) return { "scheme": parsed.scheme, "user": parsed.auth.split(":")[0] if hasattr(parsed.auth, "split") else None, "password": parsed.auth.split(":")[1] if hasattr(parsed.auth, "split") else None, "host": parsed.host, "port": parsed.port, "path": parsed.path }Extract scheme, credentials, host, port & path from a URL.
Parameters
url:str- A URL string.
Returns
Dict : A dictionary containing with the following items.
- scheme
- user
- password
- host
- port
- path
def partitioned_db_resource_parser(resource: str = None, partition: str = None) ‑> str | None-
Expand source code
def partitioned_db_resource_parser( resource: str = None, partition: str = None, ) -> Optional[str]: """ Build resource path with optional partition ID. Parameters ---------- resource : str The resource to fetch (relative to the host). Default `None`. partition: str An optional partition ID. Only valid for partitioned databases. (Default `None`.) Returns ---------- The (relative) path of the resource. """ return f"_partition/{partition}/{resource}" if partition else resourceBuild resource path with optional partition ID.
Parameters
resource:str- The resource to fetch (relative to the host). Default
None. partition:str- An optional partition ID. Only valid for partitioned databases. (Default
None.)
Returns
The (relative) path of the resource. def rm_nones_from_dict(data: dict, /) ‑> dict-
Expand source code
def rm_nones_from_dict(data: dict, /) -> dict: """ Removes all `None` keys from a dictionary. Parameters ---------- data : dict A dictionary. Returns ------- dict : A dictionary without `None` keys. """ return {k: v for k, v in data.items() if v is not None}Removes all
Nonekeys from a dictionary.Parameters
data:dict- A dictionary.
Returns
dict : A dictionary without
Nonekeys. def user_name_to_id(name: str) ‑> str-
Expand source code
def user_name_to_id(name: str) -> str: """ Convert a name into a valid CouchDB user ID. Parameters ---------- name : str A user name. Returns ------- str : A valid CouchDB ID, i.e. of the form `org.couchdb.user:{name}`. """ return f"org.couchdb.user:{name}"Convert a name into a valid CouchDB user ID.
Parameters
name:str- A user name.
Returns
str : A valid CouchDB ID, i.e. of the form
org.couchdb.user:{name}. def validate_auth_method(auth_method: str) ‑> bool-
Expand source code
def validate_auth_method(auth_method: str) -> bool: """ Checks if the provided authentication method is valid. Parameters ---------- auth_method : str Returns ------- bool: `True` if `auth_method` is in `VALID_AUTH_METHODS`. """ return auth_method in VALID_AUTH_METHODSChecks if the provided authentication method is valid.
Parameters
auth_method:str
Returns
bool:
Trueifauth_methodis inVALID_AUTH_METHODS. def validate_db_name(name: str) ‑> bool-
Expand source code
def validate_db_name(name: str) -> bool: """ Checks a name for CouchDB name-compliance. Parameters ---------- name : str A prospective database name. Returns ------- bool : `True` if the provided name is CouchDB compliant. """ return name in COUCH_DB_RESERVED_DB_NAMES or bool(PATTERN_DB_NAME.fullmatch(name))Checks a name for CouchDB name-compliance.
Parameters
name:str- A prospective database name.
Returns
bool :
Trueif the provided name is CouchDB compliant. def validate_proxy(proxy: str) ‑> bool-
Expand source code
def validate_proxy(proxy: str) -> bool: """ Check a proxy scheme for CouchDB proxy-scheme-compliance Parameters ---------- proxy : str A prospective proxy. Returns ------- bool : `True` if the provided proxy is CouchDB compliant. """ return parse_url(proxy).scheme in VALID_SCHEMESCheck a proxy scheme for CouchDB proxy-scheme-compliance
Parameters
proxy:str- A prospective proxy.
Returns
bool :
Trueif the provided proxy is CouchDB compliant. def validate_user_id(user_id: str) ‑> bool-
Expand source code
def validate_user_id(user_id: str) -> bool: """ Checks a user ID for CouchDB user-id-compliance. Parameters ---------- user_id : str A prospective user ID. Returns ------- bool : `True` if the provided user ID is CouchDB compliant. """ return bool(PATTERN_USER_ID.fullmatch(user_id))Checks a user ID for CouchDB user-id-compliance.
Parameters
user_id:str- A prospective user ID.
Returns
bool :
Trueif the provided user ID is CouchDB compliant.
Classes
class MimeTypeEnum (*args, **kwds)-
Create a collection of name/value pairs.
Example enumeration:
>>> class Color(Enum): ... RED = 1 ... BLUE = 2 ... GREEN = 3Access them by:
- attribute access:
Color.RED
- value lookup:
Color(1)
- name lookup:
Color['RED']
Enumerations can be iterated over, and know how many members they have:
>>> len(Color) 3>>> list(Color) [<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]Methods can be added to enumerations, and members can have their own attributes – see the documentation for details.
Ancestors
- enum.Enum
Class variables
var mime_type_3g2var mime_type_3gpvar mime_type_3gppvar mime_type_3gpp2var mime_type_avar mime_type_aacvar mime_type_adtsvar mime_type_aivar mime_type_aifvar mime_type_aifcvar mime_type_aiffvar mime_type_assvar mime_type_auvar mime_type_avivar mime_type_avifvar mime_type_batvar mime_type_bcpiovar mime_type_binvar mime_type_bmpvar mime_type_cvar mime_type_cdfvar mime_type_cpiovar mime_type_cshvar mime_type_cssvar mime_type_csvvar mime_type_dllvar mime_type_docvar mime_type_dotvar mime_type_dvivar mime_type_emlvar mime_type_epsvar mime_type_etxvar mime_type_exevar mime_type_gifvar mime_type_gtarvar mime_type_hvar mime_type_h5var mime_type_hdfvar mime_type_heicvar mime_type_heifvar mime_type_htmvar mime_type_htmlvar mime_type_icovar mime_type_iefvar mime_type_jpevar mime_type_jpegvar mime_type_jpgvar mime_type_jsvar mime_type_jsonvar mime_type_kshvar mime_type_latexvar mime_type_loasvar mime_type_m1vvar mime_type_m3uvar mime_type_m3u8var mime_type_manvar mime_type_markdownvar mime_type_mdvar mime_type_mevar mime_type_mhtvar mime_type_mhtmlvar mime_type_mifvar mime_type_mjsvar mime_type_movvar mime_type_movievar mime_type_mp2var mime_type_mp3var mime_type_mp4var mime_type_mpavar mime_type_mpevar mime_type_mpegvar mime_type_mpgvar mime_type_msvar mime_type_n3var mime_type_ncvar mime_type_nqvar mime_type_ntvar mime_type_nwsvar mime_type_ovar mime_type_objvar mime_type_odavar mime_type_opusvar mime_type_p12var mime_type_p7cvar mime_type_pbmvar mime_type_pdfvar mime_type_pfxvar mime_type_pgmvar mime_type_plvar mime_type_pngvar mime_type_pnmvar mime_type_potvar mime_type_ppavar mime_type_ppmvar mime_type_ppsvar mime_type_pptvar mime_type_psvar mime_type_pwzvar mime_type_pyvar mime_type_pycvar mime_type_pyovar mime_type_qtvar mime_type_ravar mime_type_ramvar mime_type_rasvar mime_type_rdfvar mime_type_rgbvar mime_type_roffvar mime_type_rstvar mime_type_rtxvar mime_type_sgmvar mime_type_sgmlvar mime_type_shvar mime_type_sharvar mime_type_sndvar mime_type_sovar mime_type_srcvar mime_type_srtvar mime_type_sv4cpiovar mime_type_sv4crcvar mime_type_svgvar mime_type_swfvar mime_type_tvar mime_type_tarvar mime_type_tclvar mime_type_texvar mime_type_texivar mime_type_texinfovar mime_type_tifvar mime_type_tiffvar mime_type_trvar mime_type_trigvar mime_type_tsvvar mime_type_txtvar mime_type_ustarvar mime_type_vcfvar mime_type_vttvar mime_type_wasmvar mime_type_wavvar mime_type_webmvar mime_type_webmanifestvar mime_type_wizvar mime_type_wsdlvar mime_type_xbmvar mime_type_xlbvar mime_type_xlsvar mime_type_xmlvar mime_type_xpdlvar mime_type_xpmvar mime_type_xslvar mime_type_xwdvar mime_type_zip