buildFullUrl method

String buildFullUrl()

Builds the full URL by combining baseUrl, path, and pathParams.

RESTful parameters in path (enclosed in curly braces) are replaced with corresponding values from pathParams.

Example:

final request = AdapterRequest(
  baseUrl: 'https://api.example.com',
  path: '/users/{id}/posts/{postId}',
  method: HttpMethod.get,
  pathParams: {'id': '123', 'postId': '456'},
);
print(request.buildFullUrl());
// Output: https://api.example.com/users/123/posts/456

Note: Query parameters are not included in the returned URL. They are handled separately by the adapter.

Implementation

String buildFullUrl() {
  String url = path;

  // 处理 RESTful 参数替换
  pathParams.forEach((key, value) {
    url = url.replaceAll('{$key}', value.toString());
  });

  // 如果 path 已经是完整 URL(以 http:// 或 https:// 开头),直接返回
  if (url.startsWith('http://') || url.startsWith('https://')) {
    return url;
  }

  // 智能拼接 baseUrl 和 path,处理斜杠
  // 确保 baseUrl 和 path 之间有且仅有一个斜杠
  String fullUrl = baseUrl;

  // 移除 baseUrl 末尾的斜杠
  if (fullUrl.endsWith('/')) {
    fullUrl = fullUrl.substring(0, fullUrl.length - 1);
  }

  // 确保 path 以斜杠开头
  if (!url.startsWith('/')) {
    url = '/$url';
  }

  return fullUrl + url;
}