diff --git a/LICENSE b/LICENSE index 7aab491..f176e7e 100644 --- a/LICENSE +++ b/LICENSE @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file +SOFTWARE. diff --git a/src/main/java/org/devlive/lightcall/RequestContext.java b/src/main/java/org/devlive/lightcall/RequestContext.java index df1506d..61ee86f 100644 --- a/src/main/java/org/devlive/lightcall/RequestContext.java +++ b/src/main/java/org/devlive/lightcall/RequestContext.java @@ -18,7 +18,12 @@ private RequestContext(HttpUrl.Builder urlBuilder, Request.Builder requestBuilde public static RequestContext create(String baseUrl) { - HttpUrl.Builder urlBuilder = HttpUrl.parse(baseUrl).newBuilder(); + HttpUrl url = HttpUrl.parse(baseUrl); + if (url == null) { + throw new IllegalArgumentException("Base URL cannot be null"); + } + + HttpUrl.Builder urlBuilder = url.newBuilder(); Request.Builder requestBuilder = new Request.Builder(); return new RequestContext(urlBuilder, requestBuilder); } diff --git a/src/main/java/org/devlive/lightcall/annotation/Post.java b/src/main/java/org/devlive/lightcall/annotation/Post.java new file mode 100644 index 0000000..29bbd3f --- /dev/null +++ b/src/main/java/org/devlive/lightcall/annotation/Post.java @@ -0,0 +1,15 @@ +package org.devlive.lightcall.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Documented +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface Post +{ + String value() default ""; +} diff --git a/src/main/java/org/devlive/lightcall/executor/HttpMethod.java b/src/main/java/org/devlive/lightcall/executor/HttpMethod.java deleted file mode 100644 index a028ef1..0000000 --- a/src/main/java/org/devlive/lightcall/executor/HttpMethod.java +++ /dev/null @@ -1,6 +0,0 @@ -package org.devlive.lightcall.executor; - -public enum HttpMethod -{ - GET -} diff --git a/src/main/java/org/devlive/lightcall/processor/PostProcessor.java b/src/main/java/org/devlive/lightcall/processor/PostProcessor.java new file mode 100644 index 0000000..2da43b3 --- /dev/null +++ b/src/main/java/org/devlive/lightcall/processor/PostProcessor.java @@ -0,0 +1,50 @@ +package org.devlive.lightcall.processor; + +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import org.devlive.lightcall.RequestContext; +import org.devlive.lightcall.annotation.Post; +import org.devlive.lightcall.error.ErrorHandler; +import org.devlive.lightcall.interceptor.Interceptor; + +import java.util.List; + +@Slf4j +public class PostProcessor + extends AbstractMethodProcessor +{ + private PostProcessor(OkHttpClient client, ObjectMapper objectMapper, List interceptors, List errorHandlers) + { + super(client, objectMapper, interceptors, errorHandlers); + } + + public static PostProcessor create(OkHttpClient client, ObjectMapper objectMapper, List interceptors, List errorHandlers) + { + return new PostProcessor(client, objectMapper, interceptors, errorHandlers); + } + + @Override + public Class getAnnotationType() + { + return Post.class; + } + + @Override + protected String getPath(Post annotation) + { + return annotation.value(); + } + + @Override + protected Request buildRequest(HttpUrl url, RequestContext context) + { + return context.getRequestBuilder() + .url(url) + .post(RequestBody.create(null, "")) + .build(); + } +} diff --git a/src/main/java/org/devlive/lightcall/proxy/LightCallProxy.java b/src/main/java/org/devlive/lightcall/proxy/LightCallProxy.java index ea0a4b4..95fcc29 100644 --- a/src/main/java/org/devlive/lightcall/proxy/LightCallProxy.java +++ b/src/main/java/org/devlive/lightcall/proxy/LightCallProxy.java @@ -7,6 +7,7 @@ import org.devlive.lightcall.config.LightCallConfig; import org.devlive.lightcall.processor.GetProcessor; import org.devlive.lightcall.processor.MethodProcessor; +import org.devlive.lightcall.processor.PostProcessor; import java.lang.annotation.Annotation; import java.lang.reflect.InvocationHandler; @@ -38,8 +39,9 @@ public LightCallProxy(LightCallConfig config) this.objectMapper = new ObjectMapper(); this.processors = new HashMap<>(); - // 注册默认的 GET 处理器 + // 注册默认的处理器 registerProcessor(GetProcessor.create(client, objectMapper, config.getInterceptors(), config.getErrorHandlers())); + registerProcessor(PostProcessor.create(client, objectMapper, config.getInterceptors(), config.getErrorHandlers())); } public void registerProcessor(MethodProcessor processor) diff --git a/src/test/java/org/devlive/lightcall/example/Post.java b/src/test/java/org/devlive/lightcall/example/Post.java index 5588909..f238d65 100644 --- a/src/test/java/org/devlive/lightcall/example/Post.java +++ b/src/test/java/org/devlive/lightcall/example/Post.java @@ -9,4 +9,35 @@ public class Post private String title; private Long id; private Long userId; + + private Post() {} + + public static Post create() + { + return new Post(); + } + + public Post body(String body) + { + this.body = body; + return this; + } + + public Post title(String title) + { + this.title = title; + return this; + } + + public Post id(Long id) + { + this.id = id; + return this; + } + + public Post userId(Long userId) + { + this.userId = userId; + return this; + } } diff --git a/src/test/java/org/devlive/lightcall/example/PostService.java b/src/test/java/org/devlive/lightcall/example/get/GetService.java similarity index 90% rename from src/test/java/org/devlive/lightcall/example/PostService.java rename to src/test/java/org/devlive/lightcall/example/get/GetService.java index 3e6ad7f..c3c7c49 100644 --- a/src/test/java/org/devlive/lightcall/example/PostService.java +++ b/src/test/java/org/devlive/lightcall/example/get/GetService.java @@ -1,14 +1,15 @@ -package org.devlive.lightcall.example; +package org.devlive.lightcall.example.get; import org.devlive.lightcall.annotation.Get; import org.devlive.lightcall.annotation.Header; import org.devlive.lightcall.annotation.Headers; import org.devlive.lightcall.annotation.PathVariable; import org.devlive.lightcall.annotation.RequestParam; +import org.devlive.lightcall.example.Post; import java.util.List; -public interface PostService +public interface GetService { @Get("/posts") List getPosts(); diff --git a/src/test/java/org/devlive/lightcall/example/LightCallGetTest.java b/src/test/java/org/devlive/lightcall/example/get/GetServiceTest.java similarity index 87% rename from src/test/java/org/devlive/lightcall/example/LightCallGetTest.java rename to src/test/java/org/devlive/lightcall/example/get/GetServiceTest.java index 18c4d46..fd7818e 100644 --- a/src/test/java/org/devlive/lightcall/example/LightCallGetTest.java +++ b/src/test/java/org/devlive/lightcall/example/get/GetServiceTest.java @@ -1,14 +1,14 @@ -package org.devlive.lightcall.example; +package org.devlive.lightcall.example.get; import org.devlive.lightcall.LightCall; import org.devlive.lightcall.config.LightCallConfig; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -class LightCallGetTest +class GetServiceTest { private final LightCallConfig config = LightCallConfig.create("https://jsonplaceholder.typicode.com"); - private final PostService service = LightCall.create(PostService.class, config); + private final GetService service = LightCall.create(GetService.class, config); @Test public void testGetPosts() diff --git a/src/test/java/org/devlive/lightcall/example/interceptor/LightCallInterceptorTest.java b/src/test/java/org/devlive/lightcall/example/interceptor/LightCallInterceptorTest.java index 42f8ea0..3a17a57 100644 --- a/src/test/java/org/devlive/lightcall/example/interceptor/LightCallInterceptorTest.java +++ b/src/test/java/org/devlive/lightcall/example/interceptor/LightCallInterceptorTest.java @@ -2,7 +2,7 @@ import org.devlive.lightcall.LightCall; import org.devlive.lightcall.config.LightCallConfig; -import org.devlive.lightcall.example.PostService; +import org.devlive.lightcall.example.get.GetService; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -10,7 +10,7 @@ class LightCallInterceptorTest { private final LightCallConfig config = LightCallConfig.create("https://jsonplaceholder.typicode.com") .addInterceptor(new LoggingInterceptor()); - private final PostService service = LightCall.create(PostService.class, config); + private final GetService service = LightCall.create(GetService.class, config); @Test public void testGetPosts() diff --git a/src/test/java/org/devlive/lightcall/example/post/PostService.java b/src/test/java/org/devlive/lightcall/example/post/PostService.java new file mode 100644 index 0000000..8a56f74 --- /dev/null +++ b/src/test/java/org/devlive/lightcall/example/post/PostService.java @@ -0,0 +1,9 @@ +package org.devlive.lightcall.example.post; + +import org.devlive.lightcall.annotation.Post; + +public interface PostService +{ + @Post("/posts") + org.devlive.lightcall.example.Post createPost(org.devlive.lightcall.example.Post post); +} diff --git a/src/test/java/org/devlive/lightcall/example/post/PostServiceTest.java b/src/test/java/org/devlive/lightcall/example/post/PostServiceTest.java new file mode 100644 index 0000000..b92bd36 --- /dev/null +++ b/src/test/java/org/devlive/lightcall/example/post/PostServiceTest.java @@ -0,0 +1,22 @@ +package org.devlive.lightcall.example.post; + +import org.devlive.lightcall.LightCall; +import org.devlive.lightcall.config.LightCallConfig; +import org.devlive.lightcall.example.Post; +import org.junit.jupiter.api.Test; + +class PostServiceTest +{ + private final LightCallConfig config = LightCallConfig.create("https://jsonplaceholder.typicode.com"); + private final PostService service = LightCall.create(PostService.class, config); + + @Test + void createPost() + { + Post post = Post.create() + .title("测试创建新的数据") + .userId(1L) + .body("这是测试数据"); + System.out.println(service.createPost(post)); + } +} \ No newline at end of file