22import os
33import re
44import logging
5+ import datetime
56from concurrent .futures import ThreadPoolExecutor
67
78import xml .etree .ElementTree as ET
1617app .config ['BASIC_AUTH_USERNAME' ] = os .environ .get ("BASIC_AUTH_USERNAME" )
1718app .config ['BASIC_AUTH_PASSWORD' ] = os .environ .get ("BASIC_AUTH_PASSWORD" )
1819
20+ token = os .environ .get ("GITHUB_TOKEN" )
21+
1922basic_auth = BasicAuth (app )
2023
2124API_BASE_URL = "https://api.github.com"
@@ -104,7 +107,6 @@ def index():
104107
105108 owner = request .args .get ("owner" ) or request .form .get ('owner' )
106109 repo = request .args .get ("repo" ) or request .form .get ('repo' )
107- token = os .environ .get ("GITHUB_TOKEN" )
108110
109111 if not owner or not repo or not token :
110112 logger .warning ("Missing parameter(s) or Environment Variable" )
@@ -172,6 +174,46 @@ def health():
172174
173175 return jsonify (response )
174176
177+ @app .route ('/limit' )
178+ def limit ():
179+ """Endpoint for checking the rate limit status.
180+
181+ Returns:
182+ flask.Response: JSON response containing rate limiting information.
183+ """
184+
185+ headers = {
186+ 'Accept' : 'application/vnd.github+json' ,
187+ "Authorization" : f"Bearer { token } " ,
188+ 'X-GitHub-Api-Version' : '2022-11-28'
189+ }
190+ url = 'https://api.github.com/rate_limit'
191+ response = requests .get (url , headers = headers , timeout = TIMEOUT )
192+
193+ if response .status_code == 200 :
194+ rate = response .json ().get ('rate' , {})
195+ reset_unix_time = rate .get ('reset' , 0 )
196+ reset_datetime = datetime .datetime .fromtimestamp (reset_unix_time )
197+ reset_cest = reset_datetime .astimezone (datetime .timezone (datetime .timedelta (hours = 2 )))
198+ rate ['reset_cest' ] = reset_cest .strftime ('%Y-%m-%d %H:%M:%S %Z%z' )
199+
200+ if rate .get ('remaining' , 0 ) == 0 :
201+ response = {
202+ 'status' : 'rate_limited' ,
203+ 'rate_limit' : rate
204+ }
205+ else :
206+ response = {
207+ 'status' : 'ok' ,
208+ 'rate_limit' : rate
209+ }
210+ else :
211+ response = {
212+ 'status' : 'ok' ,
213+ 'rate_limit' : {'error' : 'Failed to retrieve rate limit information' }
214+ }
215+
216+ return jsonify (response )
175217
176218@app .errorhandler (Exception )
177219def handle_error (exception ):
@@ -184,7 +226,8 @@ def handle_error(exception):
184226 str: The error message response.
185227 """
186228 logger .error ("An error occurred: %s" , str (exception ))
187- return "An error occurred." , 500
229+ error_message = f"An error occurred: { str (exception )} "
230+ return error_message , 500
188231
189232
190233if __name__ == '__main__' :
0 commit comments