If it's helpful to you, give me a 'star' to encouragement,This is the first time I seriously provide my open source,Thank u.
This may be currently (2023/03) Retrofit (OkHttp) supports multiple BaseUrl and changes BaseUrl at runtime to achieve the simplest library;
At the same time, it may also be the simplest and most comprehensive library that supports global Header configuration implementation;
It does not conflict with Retrofit's @url method, that is, if @url specifies another baseurl, which has nothing to do with the globally configured domain name, then the globally configured baseurl and header will not have any impact on the interface.
Add Dependencies:
implementation 'io.github.uni-cstar:oknet:0.0.6'
kotlin
OkHttpClient.Builder()
...
.addOkDomain(baseUrl)//Initialization function, baseurl is the initial main domain name address
...
.build()
java
OkHttpClient.Builder builder = new OkHttpClient.Builder();
OkDomain.useOkDomain(builder,baseUrl);
...
OkDomain.setMainDomain(newBaseUrl)
OkDomain.addMainHeader(key,value)
OkDomain.removeMainHeader(key)
The above realizes the switching of the main domain name (main BaseUrl) during dynamic operation and the configuration management of the global Header
Next, configure other domain names and corresponding global Header management. These operations are similar to the previous main domain name operations.
Multi-domain name support.
eg. add a domain name configuration named baidu
by the following method, and its baseUrl is http://www.baidu.com/
(can be any common domain name, can include port number, can include pathSegment , which differs from the RetrofitUrlManager library)
OkDomain.setDomain("baidu","http://www.baidu.com/");
public interface ApiService {
//baidu is the name of the domain name added , and its format is "Domain-Name:{domain name}"
@Headers({"Domain-Name: baidu"})
@GET("/v2/book/{id}")
Observable<ResponseBody> getBook(@Path("id") int id);
//You can also use built-in constants to splice domain names to avoid typos
@Headers({OkDomain.DOMAIN_NAME_HEADER+"baidu"})
@GET("/v2/book/{id}")
Observable<ResponseBody> getBook(@Path("id") int id);
}
By adding the header of the domain name above the interface definition, the request can use the baseurl of the corresponding domain name.
OkDomain.setDomain("baidu","your new base url for the domain");
OkDomain.addHeader("baidu",key,value)
OkDomain.removeHeader("baidu",key)
Both OkDomain.addMainHeader(key,value)
and OkDomain.addHeader(domainName,key,value)
have an overloaded method, which receives an OnConflictStrategy
parameter at the end, which is used to process the interface defined in ApiService When a header with the same key as the global configuration is included, the header's processing strategy.
- OnConflictStrategy.IGNORE If the header is included in the ApiService, the header corresponding to the key in the global configuration will not be added to the request (default policy)
- OnConflictStrategy.REPLACE The header corresponding to the key in the global configuration will replace the header added in ApiService(The header corresponding to the key in the global configuration will replace the header added in ApiService (if it exists, it will be replaced, if it does not exist, it will be added)
- OnConflictStrategy.ADD Regardless of whether there is a header corresponding to the key in ApiService, the header in the global configuration will be added to the request (the request of OkHttp is allowed to contain multiple headers with the same key)
- OnConflictStrategy.ABORT If the Header corresponding to the Key in the global configuration has a Header with the same Key in ApiService, the request will be aborted and an exception will be thrown (may cause the program to crash, and this strategy will not be used in general)
eg.
//A header with key = customKey, value = GlobalHeaderValue is configured in the main domain name. This header uses the IGONRE strategy, that is, if ApiService also contains a header with key = customKey, the globally configured header will not be added to the header of the request
OkDomain.addMainHeader("customKey", "GlobalHeaderValue", OnConflictStrategy.IGNORE)
//A header with key = abort_key, value = GlobalHeaderValue is configured for the domain name 'baidu', and the header uses the ABROT strategy, that is, if ApiService contains a header with key = abort_key, an exception will be thrown
OkDomain.addHeader("baidu","abort_key", "GlobalHeaderValue", OnConflictStrategy.ABORT)
If the ApiService is not configured with a domain name, it will use the configuration of the main domain name for processing by default; If the ApiService uses the @url annotation, the following two situations exist: The url of the request generated by @url has nothing to do with any configured domain name (including the main domain name), then the configured domain name and global header will not have any impact on this request. On the contrary, if the url address of the request generated by @url is in the configuration If the domain name is found in , it will be processed according to the configuration information.
Since this library is a java library and does not depend on the android sdk, the log uses system.out.print, and the log can be turned off by the following method:
OkDomain.debug = false
// or true,default is true
If you need to turn off all functions of this library, execute the following code to disable/enable:
OkDomain.enable = false
// or true,default is true