Search in sources :

Example 46 with retrofit2.http

use of retrofit2.http in project resilience4j by resilience4j.

the class RetrofitRateLimiterTest method setUp.

@Before
public void setUp() {
    // ms
    final long TIMEOUT = 300;
    OkHttpClient client = new OkHttpClient.Builder().connectTimeout(TIMEOUT, TimeUnit.MILLISECONDS).readTimeout(TIMEOUT, TimeUnit.MILLISECONDS).writeTimeout(TIMEOUT, TimeUnit.MILLISECONDS).build();
    this.rateLimiter = RateLimiter.of("backendName", config);
    this.service = new Retrofit.Builder().addCallAdapterFactory(RateLimiterCallAdapter.of(rateLimiter)).addConverterFactory(ScalarsConverterFactory.create()).client(client).baseUrl("http://localhost:8080/").build().create(RetrofitService.class);
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) Before(org.junit.Before)

Example 47 with retrofit2.http

use of retrofit2.http in project elastest-torm by elastest.

the class DockerComposeService method setup.

public void setup() throws IOException, InterruptedException {
    // 1. Start docker-compose-ui container
    dockerComposeUiContainerName = dockerService.generateContainerName(dockerComposeUiPrefix);
    int dockerComposeBindPort = dockerService.findRandomOpenPort();
    Binding bindNoVncPort = bindPort(dockerComposeBindPort);
    ExposedPort exposedNoVncPort = tcp(dockerComposeUiPort);
    String dockerComposeServiceUrl = "http://" + dockerService.getDockerServerIp() + ":" + dockerComposeBindPort;
    log.debug("Starting docker-compose-ui container: {}", dockerComposeUiContainerName);
    List<PortBinding> portBindings = asList(new PortBinding(bindNoVncPort, exposedNoVncPort));
    Volume volume = new Volume(dockerDefaultSocket);
    List<Volume> volumes = asList(volume);
    List<Bind> binds = asList(new Bind(dockerDefaultSocket, volume));
    DockerBuilder dockerBuilder = dockerBuilder(dockerComposeUiImageId, dockerComposeUiContainerName).portBindings(portBindings).volumes(volumes).binds(binds);
    dockerService.startAndWaitContainer(dockerBuilder.build());
    // 2. Create Retrofit object to call docker-compose-ui REST API
    OkHttpClient okHttpClient = new OkHttpClient.Builder().readTimeout(dockerComposeTimeout, SECONDS).connectTimeout(dockerComposeTimeout, SECONDS).build();
    Retrofit retrofit = new Retrofit.Builder().client(okHttpClient).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).addConverterFactory(GsonConverterFactory.create()).baseUrl(dockerComposeServiceUrl).build();
    dockerComposeApi = retrofit.create(DockerComposeApi.class);
    log.debug("docker-compose-ui up and running on URL: {}", dockerComposeServiceUrl);
    // 3.Delete default example projects
    dockerService.waitForHostIsReachable(dockerComposeServiceUrl);
    String[] defaultProjects = { "env-demo", "hello-node", "node-redis", "volumes-demo", "volumes-relative-paths" };
    removeProjects(defaultProjects);
}
Also used : PortBinding(com.github.dockerjava.api.model.PortBinding) Binding(com.github.dockerjava.api.model.Ports.Binding) DockerBuilder(io.elastest.epm.client.DockerContainer.DockerBuilder) Bind(com.github.dockerjava.api.model.Bind) OkHttpClient(okhttp3.OkHttpClient) DockerComposeApi(io.elastest.epm.client.DockerComposeApi) ExposedPort(com.github.dockerjava.api.model.ExposedPort) Retrofit(retrofit2.Retrofit) PortBinding(com.github.dockerjava.api.model.PortBinding) Volume(com.github.dockerjava.api.model.Volume)

Example 48 with retrofit2.http

use of retrofit2.http in project bdcodehelper by boredream.

the class LcErrorConstants method parseHttpErrorInfo.

/**
 * 解析服务器错误信息
 */
public static String parseHttpErrorInfo(Throwable throwable) {
    String errorInfo = throwable.getMessage();
    if (throwable instanceof HttpException) {
        // 如果是Retrofit的Http错误,则转换类型,获取信息
        HttpException exception = (HttpException) throwable;
        ResponseBody responseBody = exception.response().errorBody();
        MediaType type = responseBody.contentType();
        // 如果是application/json类型数据,则解析返回内容
        if (type != null && type.type().equals("application") && type.subtype().equals("json")) {
            try {
                // 这里的返回内容是Bmob/AVOS/Parse等RestFul API文档中的错误代码和错误信息对象
                BaseResponse errorResponse = new Gson().fromJson(responseBody.string(), BaseResponse.class);
                errorInfo = getLocalErrorInfo(errorResponse);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } else if (throwable instanceof LcErrorResponse) {
        LcErrorResponse lce = (LcErrorResponse) throwable;
        errorInfo = getLocalErrorInfo(lce.getError());
    } else {
        if (throwable instanceof UnknownHostException) {
            errorInfo = "无法连接到服务器";
        }
    }
    return errorInfo;
}
Also used : BaseResponse(com.boredream.bdcodehelper.lean.entity.BaseResponse) UnknownHostException(java.net.UnknownHostException) MediaType(okhttp3.MediaType) Gson(com.google.gson.Gson) HttpException(retrofit2.HttpException) LcErrorResponse(com.boredream.bdcodehelper.lean.entity.LcErrorResponse) HttpException(retrofit2.HttpException) UnknownHostException(java.net.UnknownHostException) ResponseBody(okhttp3.ResponseBody)

Example 49 with retrofit2.http

use of retrofit2.http in project RxJavaSamples by rengwuxian.

the class Network method getZhuangbiApi.

public static ZhuangbiApi getZhuangbiApi() {
    if (zhuangbiApi == null) {
        Retrofit retrofit = new Retrofit.Builder().client(okHttpClient).baseUrl("http://www.zhuangbi.info/").addConverterFactory(gsonConverterFactory).addCallAdapterFactory(rxJavaCallAdapterFactory).build();
        zhuangbiApi = retrofit.create(ZhuangbiApi.class);
    }
    return zhuangbiApi;
}
Also used : Retrofit(retrofit2.Retrofit) ZhuangbiApi(com.rengwuxian.rxjavasamples.network.api.ZhuangbiApi)

Example 50 with retrofit2.http

use of retrofit2.http in project RxJavaSamples by rengwuxian.

the class Network method getGankApi.

public static GankApi getGankApi() {
    if (gankApi == null) {
        Retrofit retrofit = new Retrofit.Builder().client(okHttpClient).baseUrl("http://gank.io/api/").addConverterFactory(gsonConverterFactory).addCallAdapterFactory(rxJavaCallAdapterFactory).build();
        gankApi = retrofit.create(GankApi.class);
    }
    return gankApi;
}
Also used : Retrofit(retrofit2.Retrofit) GankApi(com.rengwuxian.rxjavasamples.network.api.GankApi)

Aggregations

Test (org.junit.Test)82 ResponseBody (okhttp3.ResponseBody)76 Request (okhttp3.Request)72 Retrofit (retrofit2.Retrofit)39 OkHttpClient (okhttp3.OkHttpClient)28 RequestBody (okhttp3.RequestBody)22 IOException (java.io.IOException)19 Query (retrofit2.http.Query)15 MultipartBody (okhttp3.MultipartBody)12 Buffer (okio.Buffer)12 Path (retrofit2.http.Path)12 Interceptor (okhttp3.Interceptor)11 HttpUrl (okhttp3.HttpUrl)10 Response (okhttp3.Response)10 HashMap (java.util.HashMap)9 LinkedHashMap (java.util.LinkedHashMap)9 Response (retrofit2.Response)9 Part (retrofit2.http.Part)9 List (java.util.List)8 QueryMap (retrofit2.http.QueryMap)8