1 | from __future__ import annotations
|
---|
2 | from rfc3986.api import uri_reference
|
---|
3 |
|
---|
4 | class URI:
|
---|
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 | '''
|
---|
11 | def __init__(self, uri:str):
|
---|
12 | '''
|
---|
13 | Constructor checks that the provided URI meets the specs.
|
---|
14 | '''
|
---|
15 | self._uri=uri
|
---|
16 | self._parse= uri_reference(uri)
|
---|
17 | # 41 do not check URI presence, similar to java
|
---|
18 | # assert self._parse.scheme , "missing scheme"
|
---|
19 | self._normal=self._parse.normalize()
|
---|
20 |
|
---|
21 | def getUri(self):
|
---|
22 | '''
|
---|
23 | @return the original URI provided in the constructor
|
---|
24 | '''
|
---|
25 | return self._uri
|
---|
26 |
|
---|
27 | def getScheme(self):
|
---|
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 | '''
|
---|
32 | return self._parse.scheme
|
---|
33 |
|
---|
34 |
|
---|
35 | def getHost(self):
|
---|
36 | '''
|
---|
37 | @return the Name or IP address of the machine containing the resource
|
---|
38 | '''
|
---|
39 | return self._parse.host
|
---|
40 |
|
---|
41 | def getPath(self):
|
---|
42 | '''
|
---|
43 | @return the Path part of the URI. The pat to the data on the machine.
|
---|
44 | '''
|
---|
45 | return self._parse.path
|
---|
46 |
|
---|
47 | def getQuery(self):
|
---|
48 | '''
|
---|
49 | @return the query part of the URI
|
---|
50 | '''
|
---|
51 | return self._parse.query
|
---|
52 |
|
---|
53 | def getFragment(self):
|
---|
54 | '''
|
---|
55 | @return the fragment contained in the URI.
|
---|
56 | '''
|
---|
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):
|
---|
67 | return hash(self._normal)
|
---|
68 |
|
---|