Skip to content

Commit ce6e0d1

Browse files
Syed SethJakeWharton
authored andcommitted
URL overload for Retrofit.Builder#baseUrl
Functionally does nothing, just somewhat cleaner to be able to do `baseUrl(URL)` than `baseUrl(URL#toString());` if handling a `URL` rather than `String`. Used `HttpUrl.get(String)` as `HttpUrl.get(URL)` returns null instead of throwing an exception.
1 parent 81a6193 commit ce6e0d1

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

retrofit/src/main/java/retrofit2/Retrofit.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.lang.reflect.Modifier;
2222
import java.lang.reflect.Proxy;
2323
import java.lang.reflect.Type;
24+
import java.net.URL;
2425
import java.util.ArrayList;
2526
import java.util.List;
2627
import java.util.Map;
@@ -449,6 +450,16 @@ public Builder callFactory(okhttp3.Call.Factory factory) {
449450
return this;
450451
}
451452

453+
/**
454+
* Set the API base URL.
455+
*
456+
* @see #baseUrl(HttpUrl)
457+
*/
458+
public Builder baseUrl(URL baseUrl) {
459+
checkNotNull(baseUrl, "baseUrl == null");
460+
return baseUrl(HttpUrl.get(baseUrl.toString()));
461+
}
462+
452463
/**
453464
* Set the API base URL.
454465
*

retrofit/src/test/java/retrofit2/RetrofitTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.lang.annotation.Retention;
2121
import java.lang.reflect.ParameterizedType;
2222
import java.lang.reflect.Type;
23+
import java.net.MalformedURLException;
24+
import java.net.URL;
2325
import java.util.List;
2426
import java.util.Map;
2527
import java.util.Set;
@@ -749,6 +751,14 @@ class MyConverterFactory extends Converter.Factory {
749751
assertThat(retrofit.baseUrl()).isSameAs(url);
750752
}
751753

754+
@Test public void baseJavaUrlPropagated() throws MalformedURLException {
755+
URL url = new URL("http://example.com/");
756+
Retrofit retrofit = new Retrofit.Builder()
757+
.baseUrl(url)
758+
.build();
759+
assertThat(retrofit.baseUrl()).isEqualTo(HttpUrl.get("http://example.com/"));
760+
}
761+
752762
@Test public void clientNullThrows() {
753763
try {
754764
new Retrofit.Builder().client(null);

0 commit comments

Comments
 (0)