HttpResourceGroup.java 1.8 KB
Newer Older
A
Allen Wang 已提交
1 2 3
package com.netflix.ribbonclientextensions.http;

import io.netty.buffer.ByteBuf;
4 5
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpHeaders;
A
Allen Wang 已提交
6 7 8 9 10
import io.reactivex.netty.protocol.http.client.HttpClient;

import com.netflix.client.config.ClientConfigBuilder;
import com.netflix.client.config.IClientConfig;
import com.netflix.client.netty.RibbonTransport;
11
import com.netflix.ribbonclientextensions.ClientOptions;
A
Allen Wang 已提交
12 13
import com.netflix.ribbonclientextensions.ResourceGroup;

14 15
public class HttpResourceGroup extends ResourceGroup<HttpRequestTemplate<?>> {
    private final HttpClient<ByteBuf, ByteBuf> client;
16
    private final HttpHeaders headers;
A
Allen Wang 已提交
17
    
18 19
    public HttpResourceGroup(String groupName) {
        this(groupName, null);
A
Allen Wang 已提交
20 21
    }
    
22 23 24
    public HttpResourceGroup(String groupName, ClientOptions options) {
        super(groupName, options);
        client = RibbonTransport.newHttpClient(getClientConfig());
25
        headers = new DefaultHttpHeaders();
A
Allen Wang 已提交
26 27
    }
    
28
    protected IClientConfig loadDefaultConfig(String groupName) {
A
Allen Wang 已提交
29 30 31 32
        return ClientConfigBuilder.newBuilderWithArchaiusProperties(groupName).build();
    }
    
    public HttpResourceGroup withCommonHeader(String name, String value) {
33
        headers.add(name, value);
A
Allen Wang 已提交
34 35
        return this;
    }
36

37
    @Override
38 39
    public <T> HttpRequestTemplate<T> newRequestTemplate(String name,
            Class<? extends T> classType) {
40
        return new HttpRequestTemplate<T>(name, this, classType);
A
Allen Wang 已提交
41 42
    }
    
43 44
    public HttpRequestTemplate<ByteBuf> newRequestTemplate(String name) {
        return newRequestTemplate(name, ByteBuf.class);
A
Allen Wang 已提交
45 46
    }
    
47 48 49 50
    HttpHeaders getHeaders() {
        return headers;
    }
    
51 52 53
    HttpClient<ByteBuf, ByteBuf> getClient() {
        return client;
    }
A
Allen Wang 已提交
54
}