[231] | 1 | from __future__ import annotations
|
---|
| 2 | from rfc3986.api import uri_reference
|
---|
| 3 |
|
---|
| 4 | class URI:
|
---|
[262] | 5 | '''
|
---|
| 6 | Immutable object containing an Uniform Resource Identifier (URI).
|
---|
| 7 | An URI is a string of characters identifying a resource on the web.
|
---|
| 8 | It generally looks like scheme://user@host:port/bla/bla?query#fragment.
|
---|
| 9 | Many of these parts are optional, check the specs of rfc3986
|
---|
| 10 | '''
|
---|
[231] | 11 | def __init__(self, uri:str):
|
---|
[262] | 12 | '''
|
---|
| 13 | Constructor checks that the provided URI meets the specs.
|
---|
| 14 | '''
|
---|
[231] | 15 | self._uri=uri
|
---|
| 16 | self._parse= uri_reference(uri)
|
---|
[266] | 17 | # 41 do not check URI presence, similar to java
|
---|
| 18 | # assert self._parse.scheme , "missing scheme"
|
---|
[231] | 19 | self._normal=self._parse.normalize()
|
---|
| 20 |
|
---|
| 21 | def getUri(self):
|
---|
[262] | 22 | '''
|
---|
| 23 | @return the original URI provided in the constructor
|
---|
| 24 | '''
|
---|
[231] | 25 | return self._uri
|
---|
| 26 |
|
---|
| 27 | def getScheme(self):
|
---|
[262] | 28 | '''
|
---|
| 29 | The scheme of the URI. Scheme specifies the format of the data
|
---|
| 30 | and the communication protocols needed. Eg "https" or "ws"
|
---|
| 31 | '''
|
---|
[231] | 32 | return self._parse.scheme
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | def getHost(self):
|
---|
[262] | 36 | '''
|
---|
| 37 | @return the Name or IP address of the machine containing the resource
|
---|
| 38 | '''
|
---|
[231] | 39 | return self._parse.host
|
---|
| 40 |
|
---|
| 41 | def getPath(self):
|
---|
[262] | 42 | '''
|
---|
| 43 | @return the Path part of the URI. The pat to the data on the machine.
|
---|
| 44 | '''
|
---|
[231] | 45 | return self._parse.path
|
---|
| 46 |
|
---|
| 47 | def getQuery(self):
|
---|
[262] | 48 | '''
|
---|
| 49 | @return the query part of the URI
|
---|
| 50 | '''
|
---|
[231] | 51 | return self._parse.query
|
---|
| 52 |
|
---|
| 53 | def getFragment(self):
|
---|
[262] | 54 | '''
|
---|
| 55 | @return the fragment contained in the URI.
|
---|
| 56 | '''
|
---|
[231] | 57 | return self._parse.fragment
|
---|
| 58 |
|
---|
| 59 | def __repr__(self)->str:
|
---|
| 60 | return self._uri
|
---|
| 61 |
|
---|
| 62 | def __eq__(self, other):
|
---|
| 63 | return isinstance(other, self.__class__) and \
|
---|
| 64 | self._normal==other._normal
|
---|
| 65 |
|
---|
| 66 | def __hash__(self):
|
---|
[232] | 67 | return hash(self._normal)
|
---|
[231] | 68 |
|
---|