1
- from bs4 import BeautifulSoup
2
- import grequests
3
- import markdown
1
+ import asyncio
4
2
import os
5
3
import unittest
4
+ from typing import Union
5
+
6
+ import httpx
7
+ import markdown
8
+ from bs4 import BeautifulSoup
9
+
6
10
from . import (
11
+ DOC_NAMES ,
12
+ DOCS_DIR ,
7
13
SCHEMA_NAMES ,
8
14
all_properties ,
9
- DOCS_DIR ,
10
- DOC_NAMES ,
11
15
property_doc_name ,
12
16
schema_enum_registry ,
13
17
)
@@ -117,14 +121,12 @@ def error_msg(schema_name, value, enum):
117
121
schema_name , v , enum
118
122
) # noqa
119
123
120
- def test_urls_in_docs (s ):
121
- def exception (request , exception ):
122
- return f"{ request } - { exception } "
124
+ def test_urls_in_docs (self ):
125
+ async def async_requests (urls ) -> list [Union [httpx .Response , httpx .HTTPError ]]:
126
+ async with httpx .AsyncClient (timeout = 60 ) as client :
127
+ responses = (client .get (url ) for url in urls )
128
+ results = await asyncio .gather (* responses , return_exceptions = True )
123
129
124
- def async_requests (urls ):
125
- results = grequests .map (
126
- (grequests .get (u ) for u in urls ), exception_handler = exception , size = 100
127
- )
128
130
return results
129
131
130
132
urls = []
@@ -142,18 +144,21 @@ def async_requests(urls):
142
144
143
145
urls .append (url )
144
146
145
- results = async_requests (urls )
147
+ results = asyncio . run ( async_requests (urls ) )
146
148
147
149
warns = []
148
150
not_founds = []
149
- for resp in results :
150
- if not resp .ok :
151
- warns .append (f"failed { resp .status_code } : { resp .url } " )
152
- if resp .status_code in [404 ]:
153
- not_founds .append (resp .url )
154
-
155
- if not_founds :
156
- raise ValueError (f"URLs not found: \n { not_founds } " )
151
+ for response in results :
152
+ if isinstance (response , httpx .HTTPError ):
153
+ warns .append (f"failed { response !s} : { response .request .url !s} " )
154
+ else :
155
+ if not response .is_success :
156
+ warns .append (f"failed { response .status_code } : { response .url !s} " )
157
+
158
+ if response .status_code in (404 ,):
159
+ not_founds .append (str (response .url ))
160
+
161
+ assert not not_founds , f"URLs not found: \n { not_founds } "
157
162
158
163
print ("\n === Minor URL link warnings ===\n " )
159
164
for w in warns :
0 commit comments