@@ -66,6 +66,7 @@ class Gitlab:
6666 user_agent: A custom user agent to use for making HTTP requests.
6767 retry_transient_errors: Whether to retry after 500, 502, 503, 504
6868 or 52x responses. Defaults to False.
69+ persist_base_url: reconstruct next url if found not same as user provided url
6970 """
7071
7172 def __init__ (
@@ -85,6 +86,7 @@ def __init__(
8586 order_by : Optional [str ] = None ,
8687 user_agent : str = gitlab .const .USER_AGENT ,
8788 retry_transient_errors : bool = False ,
89+ persist_base_url : bool = False ,
8890 ) -> None :
8991
9092 self ._api_version = str (api_version )
@@ -95,6 +97,7 @@ def __init__(
9597 #: Timeout to use for requests to gitlab server
9698 self .timeout = timeout
9799 self .retry_transient_errors = retry_transient_errors
100+ self .persist_base_url = persist_base_url
98101 #: Headers that will be used in request to GitLab
99102 self .headers = {"User-Agent" : user_agent }
100103
@@ -1133,8 +1136,29 @@ def _query(
11331136 next_url = requests .utils .parse_header_links (result .headers ["links" ])[
11341137 0
11351138 ]["url" ]
1136- if not next_url .startswith (self ._gl ._base_url ):
1137- next_url = re .sub (r"^.*?/api" , f"{ self ._gl ._base_url } /api" , next_url )
1139+ # if the next url is different with user provided server URL
1140+ # then give a warning it may because of misconfiguration
1141+ # but if the option to fix provided then just reconstruct it
1142+ if not next_url .startswith (self ._gl .url ):
1143+ search_api_url = re .search (r"(^.*?/api)" , next_url )
1144+ if search_api_url :
1145+ next_api_url = search_api_url .group (1 )
1146+ if self ._gl .persist_base_url :
1147+ next_url = next_url .replace (
1148+ next_api_url , f"{ self ._gl ._base_url } /api"
1149+ )
1150+ else :
1151+ utils .warn (
1152+ message = (
1153+ f"The base url of the returned next page got "
1154+ f"different with the user provided "
1155+ f"{ self ._gl .url } /api* ~> { next_api_url } *, "
1156+ f"since this may can lead to unexpected behaviour. "
1157+ f"set argument persist_base_url to True for "
1158+ f"resonctruct it with the origin one."
1159+ ),
1160+ category = UserWarning ,
1161+ )
11381162 self ._next_url = next_url
11391163 except KeyError :
11401164 self ._next_url = None
0 commit comments