1515 'search' ,
1616 'hash' ,
1717)
18- PARSE_ATTRIBUTES = URL_ATTRIBUTES + ('origin' , 'host_type' )
18+ PARSE_ATTRIBUTES = URL_ATTRIBUTES + ('origin' , 'host_type' , 'scheme_type' )
1919
2020# These are the attributes that have corresponding ada_get_* functions
2121GET_ATTRIBUTES = frozenset (PARSE_ATTRIBUTES )
@@ -45,10 +45,6 @@ class HostType(IntEnum):
4545 >>> from ada_url import HostType
4646 >>> HostType.DEFAULT
4747 <HostType.DEFAULT: 0>
48- >>> HostType.IPV4
49- <HostType.IPV4: 1>
50- >>> HostType.IPV6
51- <HostType.IPV6: 2>
5248
5349 """
5450
@@ -57,6 +53,35 @@ class HostType(IntEnum):
5753 IPV6 = 2
5854
5955
56+ class SchemeType (IntEnum ):
57+ """
58+ Enum for `URL scheme types <https://url.spec.whatwg.org/#url-miscellaneous>`__.
59+
60+ * ``HTTP`` URLs like ``http://example.org`` are ``0``.
61+ * ``NOT_SPECIAL`` URLs like ``git://example.og`` are ``1``.
62+ * ``HTTPS`` URLs like ``https://example.org`` are ``2``.
63+ * ``WS`` URLs like ``ws://example.org`` are ``3``.
64+ * ``FTP`` URLs like ``ftp://example.org`` are ``4``.
65+ * ``WSS`` URLs like ``wss://example.org`` are ``5``.
66+ * ``FILE`` URLs like ``file://example`` are ``6``.
67+
68+ .. code-block:: python
69+
70+ >>> from ada_url import SchemeType
71+ >>> SchemeType.HTTPS
72+ <SchemeType.HTTPS: 2>
73+
74+ """
75+
76+ HTTP = 0
77+ NOT_SPECIAL = 1
78+ HTTPS = 2
79+ WS = 3
80+ FTP = 4
81+ WSS = 5
82+ FILE = 6
83+
84+
6085class ParseAttributes (TypedDict , total = False ):
6186 href : str
6287 username : str
@@ -70,6 +95,7 @@ class ParseAttributes(TypedDict, total=False):
7095 hash : str
7196 origin : str
7297 host_type : HostType
98+ scheme_type : SchemeType
7399
74100
75101def _get_obj (constructor , destructor , * args ):
@@ -113,8 +139,10 @@ class URL:
113139 * ``search``
114140 * ``hash``
115141
116- You can additionally read the ``origin`` and ``host_type`` attributes.
117- ``host_type`` is a :class:`HostType` enum.
142+ You can additionally read these attributes:
143+ * ``origin``, which will be a ``str``
144+ * ``host_type``, which will be a :class:`HostType` enum
145+ * ``scheme_type``, which will be a :class:`SchemeType` enum
118146
119147 The class also exposes a static method that checks whether the input
120148 *url* (and optional *base*) can be parsed:
@@ -143,6 +171,7 @@ class URL:
143171 hash : str
144172 origin : Final [str ]
145173 host_type : Final [HostType ]
174+ scheme_type : Final [SchemeType ]
146175
147176 def __init__ (self , url : str , base : Optional [str ] = None ):
148177 url_bytes = url .encode ('utf-8' )
@@ -193,15 +222,17 @@ def __delattr__(self, attr: str):
193222 def __dir__ (self ) -> List [str ]:
194223 return super ().__dir__ () + list (PARSE_ATTRIBUTES )
195224
196- def __getattr__ (self , attr : str ) -> Union [str , HostType ]:
225+ def __getattr__ (self , attr : str ) -> Union [str , HostType , SchemeType ]:
197226 if attr in GET_ATTRIBUTES :
198227 get_func = getattr (lib , f'ada_get_{ attr } ' )
199228 data = get_func (self .urlobj )
200229 if attr == 'origin' :
201230 ret = _get_str (data )
202231 lib .ada_free_owned_string (data )
203232 elif attr == 'host_type' :
204- ret = data
233+ ret = HostType (data )
234+ elif attr == 'scheme_type' :
235+ ret = SchemeType (data )
205236 else :
206237 ret = _get_str (data )
207238
@@ -342,11 +373,13 @@ def parse_url(s: str, attributes: Iterable[str] = PARSE_ATTRIBUTES) -> ParseAttr
342373 'hash': '#frag'
343374 'origin': 'https://example.org:8080',
344375 'host_type': 0
376+ 'scheme_type': 2
345377 }
346378
347379 The names of the dictionary keys correspond to the components of the "URL class"
348380 in the WHATWG URL spec.
349381 ``host_type`` is a :class:`HostType` enum.
382+ ``scheme_type`` is a :class:`SchemeType` enum.
350383
351384 Pass in a sequence of *attributes* to limit which keys are returned.
352385
@@ -378,6 +411,8 @@ def parse_url(s: str, attributes: Iterable[str] = PARSE_ATTRIBUTES) -> ParseAttr
378411 lib .ada_free_owned_string (data )
379412 elif attr == 'host_type' :
380413 ret [attr ] = HostType (data )
414+ elif attr == 'scheme_type' :
415+ ret [attr ] = SchemeType (data )
381416 else :
382417 ret [attr ] = _get_str (data )
383418
0 commit comments