Skip to content

Commit

Permalink
feat(内核): 支持 Post 处理器
Browse files Browse the repository at this point in the history
  • Loading branch information
qianmoQ committed Jan 18, 2025
1 parent 04b3752 commit d19fe52
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 16 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -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.
SOFTWARE.
7 changes: 6 additions & 1 deletion src/main/java/org/devlive/lightcall/RequestContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/devlive/lightcall/annotation/Post.java
Original file line number Diff line number Diff line change
@@ -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 "";
}
6 changes: 0 additions & 6 deletions src/main/java/org/devlive/lightcall/executor/HttpMethod.java

This file was deleted.

50 changes: 50 additions & 0 deletions src/main/java/org/devlive/lightcall/processor/PostProcessor.java
Original file line number Diff line number Diff line change
@@ -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<Post>
{
private PostProcessor(OkHttpClient client, ObjectMapper objectMapper, List<Interceptor> interceptors, List<ErrorHandler> errorHandlers)
{
super(client, objectMapper, interceptors, errorHandlers);
}

public static PostProcessor create(OkHttpClient client, ObjectMapper objectMapper, List<Interceptor> interceptors, List<ErrorHandler> errorHandlers)
{
return new PostProcessor(client, objectMapper, interceptors, errorHandlers);
}

@Override
public Class<Post> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <A extends Annotation> void registerProcessor(MethodProcessor<A> processor)
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/org/devlive/lightcall/example/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Post> getPosts();
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

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;

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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit d19fe52

Please sign in to comment.