Given an endpoint (e.g. '/sapi/v1/userInfo'), determine which _request_*_api function is appropriate in the client, remove the recognized prefix plus any version segments (e.g. /v1/), parse out any version (v1, v2, etc.), and return (request_function, stripped_path, version). E
(
endpoint: str,
)
| 133 | |
| 134 | |
| 135 | def get_request_function_and_path( |
| 136 | endpoint: str, |
| 137 | ) -> tuple[str | None, str | None, int | None]: |
| 138 | """ |
| 139 | Given an endpoint (e.g. '/sapi/v1/userInfo'), determine which _request_*_api |
| 140 | function is appropriate in the client, remove the recognized prefix plus any |
| 141 | version segments (e.g. /v1/), parse out any version (v1, v2, etc.), |
| 142 | and return (request_function, stripped_path, version). |
| 143 | |
| 144 | Example: |
| 145 | endpoint = '/sapi/v1/exchangeInfo' |
| 146 | -> returns ('_request_margin_api', 'exchangeInfo', 1) |
| 147 | |
| 148 | If no recognized prefix is found, return (None, None, None). |
| 149 | If no version is found, version will be None. |
| 150 | """ |
| 151 | # Sort prefixes by length descending to match the longest prefix first |
| 152 | sorted_prefixes = sorted(PREFIX_MAP.keys(), key=len, reverse=True) |
| 153 | request_func = None |
| 154 | stripped = None |
| 155 | |
| 156 | # Identify which prefix is present, if any |
| 157 | matched_prefix = None |
| 158 | for prefix in sorted_prefixes: |
| 159 | if endpoint.startswith(prefix): |
| 160 | request_func = PREFIX_MAP[prefix] |
| 161 | matched_prefix = prefix |
| 162 | break |
| 163 | |
| 164 | # If no recognized prefix, return null |
| 165 | if not request_func or matched_prefix is None: |
| 166 | return None, None, None |
| 167 | |
| 168 | stripped = endpoint[len(matched_prefix) :] |
| 169 | |
| 170 | # Attempt to parse out the version, e.g. '/v1/', '/v2/' |
| 171 | version_match = re.search(r"/v(\d+)/", stripped) |
| 172 | version = None |
| 173 | if version_match: |
| 174 | # Convert the matched text into an integer |
| 175 | version = int(version_match.group(1)) |
| 176 | |
| 177 | # Remove version segments like /v1/, /v2/ |
| 178 | if stripped: |
| 179 | stripped = re.sub(r"/v\d+/", "/", stripped) |
| 180 | # Strip leading/trailing slashes |
| 181 | stripped = stripped.strip("/") |
| 182 | |
| 183 | return request_func, stripped, version |
| 184 | |
| 185 | |
| 186 | def check_method_in_file(method, endpoint, file_name): |
no outgoing calls
no test coverage detected
searching dependent graphs…