Skip to content

Commit

Permalink
feat: header 处理器透传 HttpUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
yuqiufeng committed Jul 31, 2024
1 parent b775d54 commit a102bba
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import com.tuya.connector.api.context.ContextManager;
import com.tuya.connector.api.exceptions.ConnectorException;
import com.tuya.connector.api.model.HttpRequest;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.*;
import okio.Buffer;
import okio.BufferedSink;

Expand Down Expand Up @@ -44,11 +41,13 @@ public Response intercept(Chain chain) throws IOException {
if (body != null) {
body.writeTo(sink);
}
HttpUrl url = request.url();
HttpRequest httpRequest = HttpRequest.builder()
.httpMethod(request.method())
.headers(request.headers().toMultimap())
.url(request.url().url())
.url(url.url())
.body(sink.buffer().readByteArray())
.httpUrl(url)
.build();
Map<String, String> headerMap = headerProcessor.value(httpRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import okhttp3.HttpUrl;

import java.net.URL;
import java.util.List;
import java.util.Map;
Expand All @@ -22,4 +24,18 @@ public class HttpRequest {
URL url;
Map<String, List<String>> headers;
byte[] body;
HttpUrl httpUrl;

private String orginalPath() {
String p = httpUrl.scheme() + "://" + httpUrl.host();
if (httpUrl.port() != 80 && httpUrl.port() != 443) {
p += ":" + httpUrl.port();
}
List<String> pathSegments = httpUrl.pathSegments();
for (String pathSegment : pathSegments) {
p += "/" + pathSegment;
}
return p;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ public interface AnnotationAbility {
List<ResultObject> urlPost();

Object int2Double();

}
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,4 @@ void int2DoubleTest() {
Object ret = ability.int2Double();
System.out.println(JSON.toJSONString(ret));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -44,6 +43,7 @@ public Map<String, String> value(HttpRequest request) {
map.put("autoheader", "enable");
return map;
}

@Override
public String sign(String content) {
return content;
Expand Down Expand Up @@ -86,4 +86,14 @@ void enableTest() {
assertEquals(enable, "enable");
}


@Test
void chineseAndPlusTest() {
String plus = "a+b";
String chineseChar = "你好";
Map<String, String> ret = ability.chineseAndPlusTest(plus, chineseChar);
assertEquals(plus, ret.get("plus"));
assertEquals(chineseChar, ret.get("chinese_char"));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tuya.connector.api.autoheader;

import java.util.Map;

/**
* <p> TODO
*
Expand All @@ -10,4 +12,6 @@ public interface HeaderAbility {

String enable();

Map<String, String> chineseAndPlusTest(String plus, String chineseChar);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.tuya.connector.api.autoheader;

import com.tuya.connector.api.annotations.GET;
import com.tuya.connector.api.annotations.Path;

import java.util.Map;

/**
* <p> TODO
Expand All @@ -14,4 +17,8 @@ public interface HeaderConnector extends HeaderAbility{
@GET("/test/autosetheader/enable")
String enable();

@Override
@GET("/test/annotations/plus-and-chinese-char/{plus}/{chinese_char}?param=xyz")
Map<String, String> chineseAndPlusTest(@Path("plus") String plus, @Path(value="chinese_char") String chineseChar);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -127,4 +128,9 @@ public Result<IntDoubleVO> int2DoubleTest() {
return Result.<IntDoubleVO>builder().result(new IntDoubleVO(1, 2, 3L, 4L, 5.5)).build();
}

@GetMapping("/plus-and-chinese-char/{plus}/{chinese_char}")
public Result<Object> chineseAndPlusTest(@PathVariable("plus") String plus, @PathVariable ("chinese_char") String chineseChar, HttpServletRequest request) {
return Result.<Object>builder().result(Map.of("plus", plus, "chinese_char", chineseChar)).build();
}

}

0 comments on commit a102bba

Please sign in to comment.