3838import base64
3939import json
4040import sys
41- from typing import Any , Optional
41+ from typing import Any , Dict , Optional , Tuple
4242from urllib import parse as urlparse
4343
4444import requests
4747import srvlookup
4848import yaml
4949from requests .adapters import HTTPAdapter
50+ from urllib3 import HTTPConnectionPool
5051
5152# uncomment the following to enable debug logging of http requests
5253# from httplib import HTTPConnection
5354# HTTPConnection.debuglevel = 1
5455
5556
56- def encode_base64 (input_bytes ) :
57+ def encode_base64 (input_bytes : bytes ) -> str :
5758 """Encode bytes as a base64 string without any padding."""
5859
5960 input_len = len (input_bytes )
@@ -63,7 +64,7 @@ def encode_base64(input_bytes):
6364 return output_string
6465
6566
66- def encode_canonical_json (value ) :
67+ def encode_canonical_json (value : object ) -> bytes :
6768 return json .dumps (
6869 value ,
6970 # Encode code-points outside of ASCII as UTF-8 rather than \u escapes
@@ -130,7 +131,7 @@ def request(
130131 sig ,
131132 destination ,
132133 )
133- authorization_headers .append (header . encode ( "ascii" ) )
134+ authorization_headers .append (header )
134135 print ("Authorization: %s" % header , file = sys .stderr )
135136
136137 dest = "matrix://%s%s" % (destination , path )
@@ -139,7 +140,10 @@ def request(
139140 s = requests .Session ()
140141 s .mount ("matrix://" , MatrixConnectionAdapter ())
141142
142- headers = {"Host" : destination , "Authorization" : authorization_headers [0 ]}
143+ headers : Dict [str , str ] = {
144+ "Host" : destination ,
145+ "Authorization" : authorization_headers [0 ],
146+ }
143147
144148 if method == "POST" :
145149 headers ["Content-Type" ] = "application/json"
@@ -154,7 +158,7 @@ def request(
154158 )
155159
156160
157- def main ():
161+ def main () -> None :
158162 parser = argparse .ArgumentParser (
159163 description = "Signs and sends a federation request to a matrix homeserver"
160164 )
@@ -212,6 +216,7 @@ def main():
212216 if not args .server_name or not args .signing_key :
213217 read_args_from_config (args )
214218
219+ assert isinstance (args .signing_key , str )
215220 algorithm , version , key_base64 = args .signing_key .split ()
216221 key = signedjson .key .decode_signing_key_base64 (algorithm , version , key_base64 )
217222
@@ -233,7 +238,7 @@ def main():
233238 print ("" )
234239
235240
236- def read_args_from_config (args ) :
241+ def read_args_from_config (args : argparse . Namespace ) -> None :
237242 with open (args .config , "r" ) as fh :
238243 config = yaml .safe_load (fh )
239244
@@ -250,7 +255,7 @@ def read_args_from_config(args):
250255
251256class MatrixConnectionAdapter (HTTPAdapter ):
252257 @staticmethod
253- def lookup (s , skip_well_known = False ):
258+ def lookup (s : str , skip_well_known : bool = False ) -> Tuple [ str , int ] :
254259 if s [- 1 ] == "]" :
255260 # ipv6 literal (with no port)
256261 return s , 8448
@@ -276,7 +281,7 @@ def lookup(s, skip_well_known=False):
276281 return s , 8448
277282
278283 @staticmethod
279- def get_well_known (server_name ) :
284+ def get_well_known (server_name : str ) -> Optional [ str ] :
280285 uri = "https://%s/.well-known/matrix/server" % (server_name ,)
281286 print ("fetching %s" % (uri ,), file = sys .stderr )
282287
@@ -299,7 +304,9 @@ def get_well_known(server_name):
299304 print ("Invalid response from %s: %s" % (uri , e ), file = sys .stderr )
300305 return None
301306
302- def get_connection (self , url , proxies = None ):
307+ def get_connection (
308+ self , url : str , proxies : Optional [Dict [str , str ]] = None
309+ ) -> HTTPConnectionPool :
303310 parsed = urlparse .urlparse (url )
304311
305312 (host , port ) = self .lookup (parsed .netloc )
0 commit comments