ReplaceBaseURL replaces the base URL (scheme + host) of the given URL with the provided baseURL. It returns the updated URL as a string or an error if the operation fails.
(currentURL string, baseURL string)
| 121 | // ReplaceBaseURL replaces the base URL (scheme + host) of the given URL with the provided baseURL. |
| 122 | // It returns the updated URL as a string or an error if the operation fails. |
| 123 | func ReplaceBaseURL(currentURL string, baseURL string) (string, error) { |
| 124 | // Parse the current URL |
| 125 | parsedURL, err := url.Parse(currentURL) |
| 126 | if err != nil { |
| 127 | return currentURL, err |
| 128 | } |
| 129 | |
| 130 | // Check if baseURL is valid |
| 131 | if baseURL == "" { |
| 132 | return currentURL, fmt.Errorf("failed to replace baseURL: baseURL is empty") |
| 133 | } |
| 134 | |
| 135 | // Parse the new baseURL |
| 136 | newBaseURL, err := url.Parse(baseURL) |
| 137 | if err != nil { |
| 138 | return currentURL, fmt.Errorf("invalid baseURL: %w", err) |
| 139 | } |
| 140 | |
| 141 | // Replace the scheme and host |
| 142 | parsedURL.Scheme = newBaseURL.Scheme |
| 143 | parsedURL.Host = newBaseURL.Host |
| 144 | |
| 145 | // Return the updated URL as a string |
| 146 | return parsedURL.String(), nil |
| 147 | } |
| 148 | |
| 149 | func ReplacePort(currentURL string, port string) (string, error) { |
| 150 | if port == "" { |