diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..dd84ea7 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,38 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: '' +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Desktop (please complete the following information):** + - OS: [e.g. iOS] + - Browser [e.g. chrome, safari] + - Version [e.g. 22] + +**Smartphone (please complete the following information):** + - Device: [e.g. iPhone6] + - OS: [e.g. iOS8.1] + - Browser [e.g. stock browser, safari] + - Version [e.g. 22] + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..bbcbbe7 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: '' +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/.github/workflows/maven-publish-starter.yml b/.github/workflows/maven-publish-starter.yml new file mode 100644 index 0000000..36c41af --- /dev/null +++ b/.github/workflows/maven-publish-starter.yml @@ -0,0 +1,46 @@ +# This workflow will build a package using Maven and then publish it to GitHub packages when a release is created +# For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#apache-maven-with-a-settings-path + +name: http starter + +on: + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - uses: actions/checkout@v4 + - name: Set up JDK 8 + uses: actions/setup-java@v4 + with: + java-version: 8 + distribution: temurin + server-id: ossrh + server-username: MAVEN_USERNAME + server-password: MAVEN_PASSWORD + gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }} + gpg-passphrase: MAVEN_GPG_PASSPHRASE + cache: 'maven' + + - name: Build with Maven + run: mvn -B clean package -Dmaven.test.skip=true --file admin4j-common-http-starter/pom.xml + + - name: Set up Apache Maven Central + run: mvn deploy -Dmaven.test.skip=true -Possrh --file admin4j-common-http-starter/pom.xml + env: + MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PASSWORD }} + MAVEN_USERNAME: ${{ secrets.OSSRH_USER }} + MAVEN_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} + GITHUB_TOKEN: ${{ github.token }} +# GPG_PRIVATE_KEY: + +# -----BEGIN PGP PRIVATE KEY BLOCK----- +# *** +# -----END PGP PRIVATE KEY BLOCK----- + +# GPG_PASSWORD GPG_PRIVATE_KEY 的密码 diff --git a/.github/workflows/maven-publish.yml b/.github/workflows/maven-publish.yml index 077a012..c7516eb 100644 --- a/.github/workflows/maven-publish.yml +++ b/.github/workflows/maven-publish.yml @@ -1,7 +1,7 @@ # This workflow will build a package using Maven and then publish it to GitHub packages when a release is created # For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#apache-maven-with-a-settings-path -name: Maven Package +name: deployment all on: release: @@ -16,9 +16,9 @@ jobs: packages: write steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up JDK 8 - uses: actions/setup-java@v3 + uses: actions/setup-java@v4 with: java-version: 8 distribution: temurin diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..18c9147 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,128 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, religion, or sexual identity +and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the + overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at +. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series +of actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or +permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within +the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.0, available at +https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct +enforcement ladder](https://github.com/mozilla/diversity). + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see the FAQ at +https://www.contributor-covenant.org/faq. Translations are available at +https://www.contributor-covenant.org/translations. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..8ad3726 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1 @@ +CONTRIBUTING HTTP diff --git a/README.md b/README.md index 446b78e..31b605a 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ maven引入 io.github.admin4j http - 0.7.4 + 0.9.5 ``` @@ -150,7 +150,7 @@ maven引入 io.github.admin4j common-http-starter - 0.7.4 + 0.9.5 ``` @@ -329,7 +329,7 @@ public class EbayOrderClient extends EbayClient { io.github.admin4j http - 0.7.4 + 0.9.5 com.admin4j.json @@ -364,4 +364,7 @@ java.lang.NoClassDefFoundError: okio.Buffer ``` 由于 okio 版本号问题 -具体参考 [记录Maven 依赖包版本号奇奇怪怪的问题 - okhttp3、okio 版本指定无效](https://blog.csdn.net/agonie201218/article/details/131552134) \ No newline at end of file +具体参考 [记录Maven 依赖包版本号奇奇怪怪的问题 - okhttp3、okio 版本指定无效](https://blog.csdn.net/agonie201218/article/details/131552134) + +# 鸣谢 +![IntelliJ IDEA logo](https://resources.jetbrains.com/storage/products/company/brand/logos/IntelliJ_IDEA.png) diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..034e848 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,21 @@ +# Security Policy + +## Supported Versions + +Use this section to tell people about which versions of your project are +currently being supported with security updates. + +| Version | Supported | +| ------- | ------------------ | +| 5.1.x | :white_check_mark: | +| 5.0.x | :x: | +| 4.0.x | :white_check_mark: | +| < 4.0 | :x: | + +## Reporting a Vulnerability + +Use this section to tell people how to report a vulnerability. + +Tell them where to go, how often they can expect to get an update on a +reported vulnerability, what to expect if the vulnerability is accepted or +declined, etc. diff --git a/admin4j-common-http-starter/pom.xml b/admin4j-common-http-starter/pom.xml index 1b352eb..95ac9dd 100644 --- a/admin4j-common-http-starter/pom.xml +++ b/admin4j-common-http-starter/pom.xml @@ -124,125 +124,6 @@ - - - - ${repository.releases.id} - nexus-releases - ${repository.releases.url} - - - ${repository.releases.id} - nexus-snapshot - ${repository.snapshots.url} - - - - - ossrh - - false - - - ossrh - https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ - - https://s01.oss.sonatype.org/content/repositories/snapshots - - - - - - - org.apache.maven.plugins - maven-source-plugin - 2.2.1 - - - attach-sources - - jar-no-fork - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - private - true - UTF-8 - UTF-8 - UTF-8 - - -Xdoclint:none - - - - package - - jar - - - - - - org.springframework.boot - spring-boot-maven-plugin - 2.7.12 - - - - org.projectlombok - lombok - - - org.springframework.boot - spring-boot-autoconfigure-processor - - - org.springframework.boot - spring-boot-autoconfigure - - - org.springframework.boot - spring-boot-configuration-processor - - - - - - org.sonatype.plugins - nexus-staging-maven-plugin - 1.6.13 - true - - ossrh - https://s01.oss.sonatype.org/ - true - true - - - - org.apache.maven.plugins - maven-gpg-plugin - 3.1.0 - - - sign-artifacts - verify - - sign - - - - - - - - \ No newline at end of file diff --git a/admin4j-common-http/pom.xml b/admin4j-common-http/pom.xml index 4700e26..271060e 100644 --- a/admin4j-common-http/pom.xml +++ b/admin4j-common-http/pom.xml @@ -100,7 +100,7 @@ com.admin4j.json admin4j-json-fastjson2 - 0.7.1 + 0.10.0 com.squareup.okio @@ -165,125 +165,5 @@ +8 - - - - ${repository.releases.id} - nexus-releases - ${repository.releases.url} - - - ${repository.releases.id} - nexus-snapshot - ${repository.snapshots.url} - - - - - - ossrh - - false - - - ossrh - https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ - - https://s01.oss.sonatype.org/content/repositories/snapshots - - - - - - - org.apache.maven.plugins - maven-source-plugin - 2.2.1 - - - attach-sources - - jar-no-fork - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - private - true - UTF-8 - UTF-8 - UTF-8 - - -Xdoclint:none - - - - package - - jar - - - - - - org.springframework.boot - spring-boot-maven-plugin - 2.7.12 - - - - org.projectlombok - lombok - - - org.springframework.boot - spring-boot-autoconfigure-processor - - - org.springframework.boot - spring-boot-autoconfigure - - - org.springframework.boot - spring-boot-configuration-processor - - - - - - org.sonatype.plugins - nexus-staging-maven-plugin - 1.6.13 - true - - ossrh - https://s01.oss.sonatype.org/ - true - true - - - - org.apache.maven.plugins - maven-gpg-plugin - 3.1.0 - - - sign-artifacts - verify - - sign - - - - - - - - diff --git a/admin4j-common-http/src/main/java/io/github/admin4j/http/ApiJsonClient.java b/admin4j-common-http/src/main/java/io/github/admin4j/http/ApiJsonClient.java index a23b86f..583f4cf 100644 --- a/admin4j-common-http/src/main/java/io/github/admin4j/http/ApiJsonClient.java +++ b/admin4j-common-http/src/main/java/io/github/admin4j/http/ApiJsonClient.java @@ -51,12 +51,20 @@ protected T deserializeJSON(InputStream in, Class tClass, boolean isList) return JSONUtil.parseObject(in, charset, tClass); } + /** + * 执行请求,并返回 json反序列化之后的实体数据 + * @param call 请求数据 + * @param tClass 实体模型 + * @return + * @param + * @throws HttpException + */ public T execute(Call call, Class tClass) throws HttpException { try { Response response = call.execute(); return handleResponse(response, tClass); } catch (IOException e) { - throw new HttpException(e); + throw new HttpException(e.getMessage(),e); } } @@ -121,7 +129,7 @@ protected T handleResponse(Response response, Class tClass, boolean isLis return handleSuccessResponse(response, tClass, isList); } } catch (IOException e) { - throw new HttpException(response.message(), e, response.code(), response.headers().toMultimap()); + throw new HttpException(e.getMessage(), e, response.code(), response.headers().toMultimap()); } } else { @@ -135,10 +143,10 @@ protected T handleResponse(Response response, Class tClass, boolean isLis respBody = response.body().string(); } catch (IOException e) { - throw new HttpException(response.message(), e, response.code(), response.headers().toMultimap()); + throw new HttpException(e.getMessage(), e, response.code(), response.headers().toMultimap()); } } - throw new HttpException(response.message(), response.code(), response.headers().toMultimap(), respBody); + throw new HttpException(respBody, response.code(), response.headers().toMultimap(), respBody); } } @@ -168,7 +176,7 @@ protected T handleFailResponse(Response response, Class tClass) { return null; } - //protected JSONObject serialize(Response response) { + // protected JSONObject serialize(Response response) { // ResponseBody body = response.body(); // if (body == null) { // throw new HttpException("response body is null"); @@ -181,7 +189,7 @@ protected T handleFailResponse(Response response, Class tClass) { // } //} - //protected JSONArray serializeList(Response response) { + // protected JSONArray serializeList(Response response) { // ResponseBody body = response.body(); // if (body == null) { // throw new HttpException("response body is null"); @@ -205,28 +213,14 @@ public T get(String path, Map queryMap, Class tClass) { return execute(call, tClass); } - - public T postForm(String url, Map formParams, Class tClass) { - - Response response = post(url, MediaTypeEnum.FORM, null, formParams, null); - return handleResponse(response, tClass); - } - - public T postFormData(String url, Map formParams, Class tClass) { - - Response response = post(url, MediaTypeEnum.FORM_DATA, null, formParams, null); - return handleResponse(response, tClass); - } - - public T post(String url, Object body, Class tClass) { - - Response response = post(url, MediaTypeEnum.JSON, body, null, null); - return handleResponse(response, tClass); + public T get(String path, Map queryMap,Map headerMap, Class tClass) { + Call call = buildGet(path, queryMap,headerMap, (Pair) null); + return execute(call, tClass); } public JSONMapper get(String path, Pair... queryParams) { - Response response = get(path, (Map) null, queryParams); + Response response = get(path, (Map) null,null, queryParams); return handleResponse(response, JSONMapper.class); } @@ -235,6 +229,11 @@ public JSONMapper get(String path, Map queryMap) { return handleResponse(response, JSONMapper.class); } + public JSONMapper get(String path, Map queryMap, Map headerMap) { + Response response = get(path, queryMap, headerMap, (Pair[]) null); + return handleResponse(response, JSONMapper.class); + } + public List getList(String path, Class tClass, Pair... queryParams) { Response response = get(path, (Map) null, queryParams); @@ -246,6 +245,47 @@ public List getList(String path, Map queryMap, Class t return (List) handleResponse(response, tClass, true); } + public List getList(String path, Map queryMap, Map headerMap, Class tClass) { + Response response = get(path, queryMap, headerMap, (Pair[]) null); + return (List) handleResponse(response, tClass, true); + } + + public T postForm(String url, Map formParams, Class tClass) { + + Response response = post(url, MediaTypeEnum.FORM, null, formParams, null); + return handleResponse(response, tClass); + } + + public T postForm(String url, Map formParams, Map headerParams, Class tClass) { + + Response response = post(url, MediaTypeEnum.FORM, null, formParams, headerParams); + return handleResponse(response, tClass); + } + + public T postFormData(String url, Map formParams, Class tClass) { + + Response response = post(url, MediaTypeEnum.FORM_DATA, null, formParams, null); + return handleResponse(response, tClass); + } + + public T postFormData(String url, Map formParams, Map headerParams, Class tClass) { + + Response response = post(url, MediaTypeEnum.FORM_DATA, null, formParams, headerParams); + return handleResponse(response, tClass); + } + + public T post(String url, Object body, Class tClass) { + + Response response = post(url, MediaTypeEnum.JSON, body, null, null); + return handleResponse(response, tClass); + } + + public T post(String url, Object body, Map headerParams, Class tClass) { + + Response response = post(url, MediaTypeEnum.JSON, body, null, headerParams); + return handleResponse(response, tClass); + } + public JSONMapper postForm(String url, Map formParams) { @@ -253,48 +293,96 @@ public JSONMapper postForm(String url, Map formParams) { return handleResponse(response, JSONMapper.class); } + public JSONMapper postForm(String url, Map formParams, Map headerParams) { + + Response response = post(url, MediaTypeEnum.FORM, null, formParams, headerParams); + return handleResponse(response, JSONMapper.class); + } + public JSONMapper postFormData(String url, Map formParams) { Response response = post(url, MediaTypeEnum.FORM_DATA, null, formParams, null); return handleResponse(response, JSONMapper.class); } + public JSONMapper postFormData(String url, Map formParams, Map headerParams) { + + Response response = post(url, MediaTypeEnum.FORM_DATA, null, formParams, headerParams); + return handleResponse(response, JSONMapper.class); + } + public JSONMapper post(String url, Object body) { Response response = post(url, MediaTypeEnum.JSON, body, null, null); return handleResponse(response, JSONMapper.class); } + public JSONMapper post(String url, Object body, Map headerParams) { + + Response response = post(url, MediaTypeEnum.JSON, body, null, headerParams); + return handleResponse(response, JSONMapper.class); + } + public JSONMapper put(String url, Object body) { Response response = put(url, MediaTypeEnum.JSON, body, null, null); return handleResponse(response, JSONMapper.class); } + public JSONMapper put(String url, Object body, Map headerParams) { + + Response response = put(url, MediaTypeEnum.JSON, body, null, headerParams); + return handleResponse(response, JSONMapper.class); + } + public T put(String url, Object body, Class tClass) { Response response = put(url, MediaTypeEnum.JSON, body, null, null); return handleResponse(response, tClass); } + public T put(String url, Object body, Map headerParams, Class tClass) { + + Response response = put(url, MediaTypeEnum.JSON, body, null, headerParams); + return handleResponse(response, tClass); + } + public JSONMapper putForm(String url, Map formParams) { Response response = put(url, MediaTypeEnum.FORM, null, formParams, null); return handleResponse(response, JSONMapper.class); } + public JSONMapper putForm(String url, Map formParams, Map headerParams) { + + Response response = put(url, MediaTypeEnum.FORM, null, formParams, headerParams); + return handleResponse(response, JSONMapper.class); + } + public T putForm(String url, Map formParams, Class tClass) { Response response = put(url, MediaTypeEnum.FORM, null, formParams, null); return handleResponse(response, tClass); } + public T putForm(String url, Map formParams, Map headerParams, Class tClass) { + + Response response = put(url, MediaTypeEnum.FORM, null, formParams, headerParams); + return handleResponse(response, tClass); + } + public JSONMapper putFormData(String url, Map formParams) { Response response = put(url, MediaTypeEnum.FORM_DATA, null, formParams, null); return handleResponse(response, JSONMapper.class); } + public JSONMapper putFormData(String url, Map formParams, Map headerParams) { + + Response response = put(url, MediaTypeEnum.FORM_DATA, null, formParams, headerParams); + return handleResponse(response, JSONMapper.class); + } + public T delete(String url, Object body, Map formParams, @@ -304,6 +392,16 @@ public T delete(String url, return handleResponse(response, tClass); } + public T delete(String url, + Object body, + Map formParams, + Map headerParams, + Class tClass) { + + Response response = delete(url, body == null ? MediaTypeEnum.FORM : MediaTypeEnum.JSON, body, formParams, headerParams); + return handleResponse(response, tClass); + } + public JSONMapper delete(String url, Object body, Map formParams) { @@ -311,4 +409,13 @@ public JSONMapper delete(String url, Response response = delete(url, body == null ? MediaTypeEnum.FORM : MediaTypeEnum.JSON, body, formParams, (Map) null); return handleResponse(response, JSONMapper.class); } + + public JSONMapper delete(String url, + Object body, + Map formParams + , Map headerParams) { + + Response response = delete(url, body == null ? MediaTypeEnum.FORM : MediaTypeEnum.JSON, body, formParams, headerParams); + return handleResponse(response, JSONMapper.class); + } } diff --git a/admin4j-common-http/src/main/java/io/github/admin4j/http/HttpRequest.java b/admin4j-common-http/src/main/java/io/github/admin4j/http/HttpRequest.java index c3d61d9..79f1bad 100644 --- a/admin4j-common-http/src/main/java/io/github/admin4j/http/HttpRequest.java +++ b/admin4j-common-http/src/main/java/io/github/admin4j/http/HttpRequest.java @@ -7,13 +7,13 @@ import io.github.admin4j.http.core.Pair; import io.github.admin4j.http.util.HttpUtil; import lombok.Cleanup; +import lombok.Getter; import lombok.Setter; import lombok.experimental.Accessors; import okhttp3.Call; import okhttp3.Callback; import okhttp3.Response; -import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; @@ -23,6 +23,7 @@ * @author andanyang * @since 2022/10/14 15:43 */ +@Getter public class HttpRequest { private Method method; @@ -39,6 +40,14 @@ public class HttpRequest { @Accessors private Object body; + public static HttpRequest get(String url) { + + HttpRequest httpRequest = new HttpRequest(); + httpRequest.url = url; + httpRequest.method = Method.GET; + return httpRequest; + } + public static HttpRequest post(String url) { HttpRequest httpRequest = new HttpRequest(); @@ -47,14 +56,46 @@ public static HttpRequest post(String url) { return httpRequest; } - public static HttpRequest get(String url) { + public static HttpRequest put(String url) { HttpRequest httpRequest = new HttpRequest(); httpRequest.url = url; - httpRequest.method = Method.GET; + httpRequest.method = Method.PUT; return httpRequest; } + public static HttpRequest delete(String url) { + + HttpRequest httpRequest = new HttpRequest(); + httpRequest.url = url; + httpRequest.method = Method.DELETE; + return httpRequest; + } + + public static HttpRequest patch(String url) { + + HttpRequest httpRequest = new HttpRequest(); + httpRequest.url = url; + httpRequest.method = Method.PATCH; + return httpRequest; + } + + /** + * 设置请求方法 + * + * @param method + * @return + */ + public HttpRequest method(Method method) { + this.method = method; + return this; + } + + public HttpRequest mediaType(MediaTypeEnum mediaType) { + this.mediaType = mediaType; + return this; + } + public HttpRequest userAgent(String userAgent) { if (headers == null) { headers = new HashMap<>(8); @@ -121,6 +162,8 @@ public HttpRequest body(Object body) { return this; } + //============= 发送http 请求 ================= + public Response execute() { ApiClient apiClient = HttpUtil.getClient(); @@ -135,7 +178,7 @@ public void asyncExecute(Callback callback) { apiClient.executeAsync(call, callback); } - public T execute(Class clas) throws IOException { + public T execute(Class clas) { @Cleanup InputStream is = Objects.requireNonNull(execute().body()).byteStream(); return JSONUtil.parseObject(is, clas); diff --git a/admin4j-common-http/src/main/java/io/github/admin4j/http/core/AbstractHttpBuildCall.java b/admin4j-common-http/src/main/java/io/github/admin4j/http/core/AbstractHttpBuildCall.java index 98369da..667a2d7 100644 --- a/admin4j-common-http/src/main/java/io/github/admin4j/http/core/AbstractHttpBuildCall.java +++ b/admin4j-common-http/src/main/java/io/github/admin4j/http/core/AbstractHttpBuildCall.java @@ -399,6 +399,11 @@ public Call buildGet(String path, Map queryMap, Pair... query return buildCall(path, Method.GET, null, queryParams, queryMap, null, null, null); } + public Call buildGet(String path, Map queryMap,Map headerMap, Pair... queryParams) { + + return buildCall(path, Method.GET, null, queryParams, queryMap, null, null, headerMap); + } + // ======================= build GET POST =============== protected Call buildPost(String url, diff --git a/admin4j-common-http/src/main/java/io/github/admin4j/http/core/AbstractHttpExecute.java b/admin4j-common-http/src/main/java/io/github/admin4j/http/core/AbstractHttpExecute.java index fdd0719..18e4984 100644 --- a/admin4j-common-http/src/main/java/io/github/admin4j/http/core/AbstractHttpExecute.java +++ b/admin4j-common-http/src/main/java/io/github/admin4j/http/core/AbstractHttpExecute.java @@ -32,9 +32,8 @@ public Response execute(Call call, UnaryOperator function) throws Http response = function.apply(response); return response; } - //catch (SocketTimeoutException e) catch (IOException e) { - throw new HttpException(e); + throw new HttpException(e.getMessage(),e); } } diff --git a/admin4j-common-http/src/main/java/io/github/admin4j/http/core/AbstractHttpRequest.java b/admin4j-common-http/src/main/java/io/github/admin4j/http/core/AbstractHttpRequest.java index 2d05274..43f0d49 100644 --- a/admin4j-common-http/src/main/java/io/github/admin4j/http/core/AbstractHttpRequest.java +++ b/admin4j-common-http/src/main/java/io/github/admin4j/http/core/AbstractHttpRequest.java @@ -45,8 +45,12 @@ public OkHttpClient getHttpClient() { // ======================= GET POST =============== public Response get(String path, Map queryMap, Pair... queryParams) { + return get(path, queryMap,null, queryParams); + } + + public Response get(String path, Map queryMap, Map headerMap, Pair... queryParams) { - Call call = buildGet(path, queryMap, queryParams); + Call call = buildGet(path, queryMap,headerMap, queryParams); return execute(call); } diff --git a/admin4j-common-http/src/main/java/io/github/admin4j/http/core/HttpConfig.java b/admin4j-common-http/src/main/java/io/github/admin4j/http/core/HttpConfig.java index 1c2bcaf..89c25a7 100644 --- a/admin4j-common-http/src/main/java/io/github/admin4j/http/core/HttpConfig.java +++ b/admin4j-common-http/src/main/java/io/github/admin4j/http/core/HttpConfig.java @@ -1,9 +1,11 @@ package io.github.admin4j.http.core; import lombok.Data; +import okhttp3.Protocol; import okhttp3.logging.HttpLoggingInterceptor; import java.net.Proxy; +import java.util.List; /** * @author andanyang @@ -53,9 +55,27 @@ public class HttpConfig { * 是否共用连接池 */ private boolean commonConnectionPool = true; + + /** + * 代理配置 + */ private ProxyConfig proxy; + /** + * 请求协议 + * - HTTP_1_0: HTTP/1.0 + * - HTTP_1_1: HTTP/1.1 + * - HTTP_2: HTTP/2 + *

+ * 按优先顺序排列。如果列表包含 Protocol.H2_PRIOR_KNOWLEDGE ,则该协议必须是唯一的协议,并且不支持 HTTPS URL。 + * 否则,列表必须包含 Protocol. HTTP_1_1。该列表不得包含 null 或 Protocol. HTTP_1_0 + */ + private List protocols; + + /** + * 代理配置 + */ @Data public static class ProxyConfig { diff --git a/admin4j-common-http/src/main/java/io/github/admin4j/http/exception/HttpException.java b/admin4j-common-http/src/main/java/io/github/admin4j/http/exception/HttpException.java index d5b7e3d..5361185 100644 --- a/admin4j-common-http/src/main/java/io/github/admin4j/http/exception/HttpException.java +++ b/admin4j-common-http/src/main/java/io/github/admin4j/http/exception/HttpException.java @@ -23,6 +23,10 @@ public HttpException(String message) { super(message); } + public HttpException(String message,Throwable throwable) { + super(message,throwable); + } + public HttpException(String message, int code) { super(message); this.code = code; diff --git a/admin4j-common-http/src/main/java/io/github/admin4j/http/factory/HttpClientFactory.java b/admin4j-common-http/src/main/java/io/github/admin4j/http/factory/HttpClientFactory.java index 247fcfa..dd3cda6 100644 --- a/admin4j-common-http/src/main/java/io/github/admin4j/http/factory/HttpClientFactory.java +++ b/admin4j-common-http/src/main/java/io/github/admin4j/http/factory/HttpClientFactory.java @@ -83,6 +83,12 @@ public static OkHttpClient okHttpClient(HttpConfig httpConfig, boolean commonCon }); } } + + // http protocols + if (httpConfig.getProtocols() != null && !httpConfig.getProtocols().isEmpty()) { + builder.protocols(httpConfig.getProtocols()); + } + return builder.build(); } } diff --git a/admin4j-common-http/src/main/java/io/github/admin4j/http/util/HttpJsonUtil.java b/admin4j-common-http/src/main/java/io/github/admin4j/http/util/HttpJsonUtil.java index a95e561..9de95a9 100644 --- a/admin4j-common-http/src/main/java/io/github/admin4j/http/util/HttpJsonUtil.java +++ b/admin4j-common-http/src/main/java/io/github/admin4j/http/util/HttpJsonUtil.java @@ -54,50 +54,105 @@ public static JSONMapper get(String url, Map queryParams) { return getHttpRequest().get(url, queryParams); } + public static JSONMapper get(String url, Map queryParams, Map headerMap) { + + return getHttpRequest().get(url, queryParams,headerMap); + } + public static T get(String url, Class tClass, Map queryParams) { return getHttpRequest().get(url, queryParams, tClass); } + /** + * 发送GET 请求 + * @param url 地址 + * @param tClass 需要JSON解析的model + * @param queryParams 查询参数 + * @param headerMap header 参数 + * @return 返回结果 model + * @param + */ + public static T get(String url, Class tClass, Map queryParams, Map headerMap) { + + return getHttpRequest().get(url, queryParams,headerMap, tClass); + } + public static JSONMapper post(String url, Object body) { return getHttpRequest().post(url, body); } + public static JSONMapper post(String url, Object body, Map headerParams) { + return getHttpRequest().post(url, body, headerParams); + } + public static T post(String url, Object body, Class tClass) { return getHttpRequest().post(url, body, tClass); } + public static T post(String url, Object body, Map headerParams, Class tClass) { + return getHttpRequest().post(url, body, headerParams, tClass); + } + public static JSONMapper postForm(String url, Map formParams) { return getHttpRequest().postForm(url, formParams); } + public static JSONMapper postForm(String url, Map formParams, Map headerParams) { + return getHttpRequest().postForm(url, formParams, headerParams); + } + public static T postForm(String url, Map formParams, Class tClass) { return getHttpRequest().postForm(url, formParams, tClass); } + public static T postForm(String url, Map formParams, Map headerParams, Class tClass) { + return getHttpRequest().postForm(url, formParams, headerParams, tClass); + } + //=================== Put ================= public static JSONMapper put(String url, Object body) { return getHttpRequest().put(url, body); } + public static JSONMapper put(String url, Object body, Map headerParams) { + return getHttpRequest().put(url, body, headerParams); + } + public static T put(String url, Object body, Class tClass) { return getHttpRequest().put(url, body, tClass); } + public static T put(String url, Object body, Map headerParams, Class tClass) { + return getHttpRequest().put(url, body, headerParams, tClass); + } + public static JSONMapper putForm(String url, Map formParams) { return getHttpRequest().putForm(url, formParams); } + public static JSONMapper putForm(String url, Map formParams, Map headerParams) { + return getHttpRequest().putForm(url, formParams, headerParams); + } + public static T putForm(String url, Map formParams, Class tClass) { return getHttpRequest().putForm(url, formParams, tClass); } + public static T putForm(String url, Map formParams, Map headerParams, Class tClass) { + return getHttpRequest().putForm(url, formParams, headerParams, tClass); + } + public static JSONMapper upload(String url, Map formParams) { return getHttpRequest().postFormData(url, formParams); } + public static JSONMapper upload(String url, Map formParams, Map headerParams) { + return getHttpRequest().postFormData(url, formParams, headerParams); + } + /** * 使用 put 方法上传 * @@ -109,6 +164,10 @@ public static JSONMapper uploadPut(String url, Map formParams) { return getHttpRequest().putFormData(url, formParams); } + public static JSONMapper uploadPut(String url, Map formParams, Map headerParams) { + return getHttpRequest().putFormData(url, formParams, headerParams); + } + // ============= delete ======= /** @@ -125,6 +184,12 @@ public static JSONMapper delete(String url, return getHttpRequest().delete(url, body, formParams); } + public static JSONMapper delete(String url, + Object body, + Map formParams, Map headerParams) { + return getHttpRequest().delete(url, body, formParams, headerParams); + } + /** * 删除 * @@ -139,4 +204,10 @@ public static T delete(String url, return getHttpRequest().delete(url, body, formParams, tClass); } + public static T delete(String url, + Object body, + Map formParams, Map headerParams, Class tClass) { + return getHttpRequest().delete(url, body, formParams, headerParams, tClass); + } + } diff --git a/admin4j-common-http/src/main/java/io/github/admin4j/http/util/HttpUtil.java b/admin4j-common-http/src/main/java/io/github/admin4j/http/util/HttpUtil.java index 2dc0e53..fb1bb8c 100644 --- a/admin4j-common-http/src/main/java/io/github/admin4j/http/util/HttpUtil.java +++ b/admin4j-common-http/src/main/java/io/github/admin4j/http/util/HttpUtil.java @@ -1,10 +1,13 @@ package io.github.admin4j.http.util; import io.github.admin4j.http.ApiClient; +import io.github.admin4j.http.HttpRequest; import io.github.admin4j.http.core.HttpDefaultConfig; import io.github.admin4j.http.core.MediaTypeEnum; import io.github.admin4j.http.core.Pair; import lombok.Cleanup; +import okhttp3.Call; +import okhttp3.Callback; import okhttp3.Response; import org.apache.commons.lang3.StringUtils; @@ -43,46 +46,198 @@ public static void setClient(ApiClient apiJsonClient) { SINGLETON_REQUEST = apiJsonClient; } + /** + * get 请求 + * + * @param url + * @param queryParams 查询参数 + * @return + */ public static Response get(String url, Pair... queryParams) { return getClient().get(url, (Map) null, queryParams); } + /** + * get 请求 + * + * @param url + * @param queryParams 查询参数 + * @return + */ public static Response get(String url, Map queryParams) { return getClient().get(url, queryParams, (Pair[]) null); } + /** + * post 请求 + * + * @param url + * @param body post body 体 + * @return + */ public static Response post(String url, Object body) { return getClient().post(url, MediaTypeEnum.JSON, body, (Map) null, (Map) null); } + /** + * post(application/json;) 请求 + * + * @param url + * @param body post body 体 + * @param header header 头 + * @return + */ + public static Response post(String url, Object body, Map header) { + return getClient().post(url, MediaTypeEnum.JSON, body, (Map) null, header); + } + + /** + * form(x-www-form-urlencoded) 格式的 post 请求 + * + * @param url + * @param formParams + * @return + */ public static Response postForm(String url, Map formParams) { return getClient().post(url, MediaTypeEnum.FORM, null, formParams, (Map) null); } + /** + * form(x-www-form-urlencoded) 格式的 post 请求 + * + * @param url + * @param formParams + * @param header + * @return + */ + public static Response postForm(String url, Map formParams, Map header) { + return getClient().post(url, MediaTypeEnum.FORM, null, formParams, header); + } + + /** + * put 请求 + * + * @param url + * @param body + * @return + */ public static Response put(String url, Object body) { return getClient().put(url, MediaTypeEnum.JSON, body, (Map) null, (Map) null); } + /** + * put 请求 + * + * @param url + * @param body + * @param header + * @return + */ + public static Response put(String url, Object body, Map header) { + return getClient().put(url, MediaTypeEnum.JSON, body, (Map) null, header); + } + + /** + * form(x-www-form-urlencoded) 格式的 put 请求 + * + * @param url + * @param formParams + * @return + */ public static Response putForm(String url, Map formParams) { return getClient().put(url, MediaTypeEnum.FORM, null, formParams, (Map) null); } + /** + * form(x-www-form-urlencoded) 格式的 put 请求 + * + * @param url + * @param formParams + * @return + */ + public static Response putForm(String url, Map formParams, Map header) { + return getClient().put(url, MediaTypeEnum.FORM, null, formParams, header); + } + + /** + * delete 请求 + * + * @param url + * @param body + * @return + */ public static Response delete(String url, Object body) { return getClient().delete(url, MediaTypeEnum.JSON, body, (Map) null, (Map) null); } + /** + * delete 请求 + * + * @param url + * @param body + * @return + */ + public static Response delete(String url, Object body, Map header) { + return getClient().delete(url, MediaTypeEnum.JSON, body, (Map) null, header); + } + + /** + * form(x-www-form-urlencoded) 格式的 delete 请求 + * + * @param url + * @param formParams + * @return + */ public static Response deleteForm(String url, Map formParams) { return getClient().delete(url, MediaTypeEnum.FORM, null, formParams, (Map) null); } + /** + * form(x-www-form-urlencoded) 格式的 delete 请求 + * + * @param url + * @param formParams + * @return + */ + public static Response deleteForm(String url, Map formParams, Map header) { + return getClient().delete(url, MediaTypeEnum.FORM, null, formParams, header); + } + + /** + * form-data(multipart/form-data) 格式的 post 请求 + * + * @param url + * @param formParams + * @return + */ + public static Response postFormData(String url, Map formParams) { + return getClient().post(url, MediaTypeEnum.FORM_DATA, null, formParams, (Map) null); + } + + public static Response postFormData(String url, Map formParams, Map header) { + return getClient().post(url, MediaTypeEnum.FORM_DATA, null, formParams, header); + } + + + /** + * form-data(multipart/form-data) 格式的 post 请求 + * + * @param url + * @param formParams + * @return + */ public static Response upload(String url, Map formParams) { return getClient().post(url, MediaTypeEnum.FORM_DATA, null, formParams, (Map) null); } + public static Response upload(String url, Map formParams, Map header) { + return getClient().post(url, MediaTypeEnum.FORM_DATA, null, formParams, header); + } + public static InputStream down(String url) { return getClient().down(url); @@ -114,4 +269,29 @@ public static void down(String url, String savePath) throws IOException { fileOutputStream.write(b); } } + + //============= 发送http 请求 ================= + public static Response send(HttpRequest httpRequest) { + ApiClient apiClient = HttpUtil.getClient(); + Call call = apiClient.buildCall(httpRequest.getUrl(), httpRequest.getMethod(), httpRequest.getMediaType(), + httpRequest.getQueryParams(), httpRequest.getQueryMap(), httpRequest.getBody(), httpRequest.getForm(), + httpRequest.getHeaders()); + return apiClient.execute(call); + } + + + /** + * 异步执行 + * + * @param httpRequest + * @param callback + */ + public static void asyncExecute(HttpRequest httpRequest, Callback callback) { + + ApiClient apiClient = HttpUtil.getClient(); + Call call = apiClient.buildCall(httpRequest.getUrl(), httpRequest.getMethod(), httpRequest.getMediaType(), + httpRequest.getQueryParams(), httpRequest.getQueryMap(), httpRequest.getBody(), httpRequest.getForm(), + httpRequest.getHeaders()); + apiClient.executeAsync(call, callback); + } } diff --git a/admin4j-common-http/src/test/java/http/ApiClientTest.java b/admin4j-common-http/src/test/java/http/ApiClientTest.java index 0412d3c..8419fe0 100644 --- a/admin4j-common-http/src/test/java/http/ApiClientTest.java +++ b/admin4j-common-http/src/test/java/http/ApiClientTest.java @@ -6,6 +6,7 @@ import io.github.admin4j.http.core.HttpConfig; import io.github.admin4j.http.core.Method; import io.github.admin4j.http.core.Pair; +import io.github.admin4j.http.exception.HttpException; import io.github.admin4j.http.util.HttpJsonUtil; import io.github.admin4j.http.util.HttpUtil; import lombok.SneakyThrows; @@ -117,5 +118,21 @@ void upload() throws IOException { System.out.println(xx); } - + @Test + void postFormError() { + + try { + Map formParams = new HashMap<>(16); + formParams.put("username", "admin"); + formParams.put("password", "admin123"); + httpRequest.postForm("https://uatservice-etax.one.th/etaxbatchweb/etaxsignbatc", + formParams + ); + //System.out.println("post = " + object); + }catch (HttpException e){ + System.out.println("e = " + e.getMessage()); + e.printStackTrace(); + } + + } } \ No newline at end of file diff --git a/admin4j-common-http/src/test/java/http/ApiJsonClientTest.java b/admin4j-common-http/src/test/java/http/ApiJsonClientTest.java index 6a01918..98ee377 100644 --- a/admin4j-common-http/src/test/java/http/ApiJsonClientTest.java +++ b/admin4j-common-http/src/test/java/http/ApiJsonClientTest.java @@ -4,11 +4,15 @@ import http.entity.R; import io.github.admin4j.http.ApiJsonClient; import io.github.admin4j.http.core.HttpConfig; +import io.github.admin4j.http.exception.HttpException; +import io.github.admin4j.http.util.HttpJsonUtil; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.IOException; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @author andanyang @@ -62,4 +66,22 @@ void getListObject() throws IOException { } + @Test + void postFormError() { + + try { + Map formParams = new HashMap<>(16); + formParams.put("username", "admin"); + formParams.put("password", "admin123"); + JSONMapper object = httpRequest.postForm("https://uatservice-etax.one.th/etaxbatchweb/etaxsignbatc", + formParams + ); + System.out.println("post = " + object); + }catch (HttpException e){ + System.out.println("e = " + e); + e.printStackTrace(); + } + + } + } \ No newline at end of file diff --git a/admin4j-common-http/src/test/java/http/TestProxy.java b/admin4j-common-http/src/test/java/http/TestProxy.java index 7bf1bce..a06bc2f 100644 --- a/admin4j-common-http/src/test/java/http/TestProxy.java +++ b/admin4j-common-http/src/test/java/http/TestProxy.java @@ -23,22 +23,22 @@ void getStr() throws IOException { HttpConfig httpConfig = new HttpConfig(); HttpConfig.ProxyConfig proxyConfig = new HttpConfig.ProxyConfig(); - proxyConfig.setHost("119.8.115.201"); - proxyConfig.setPort(9892); - proxyConfig.setUserName("ces"); - proxyConfig.setPassword("iIl1o0O"); + proxyConfig.setHost("192.168.0.1"); + proxyConfig.setPort(9898); + proxyConfig.setUserName("ces_order"); + proxyConfig.setPassword("23451Order"); httpConfig.setProxy(proxyConfig); OkHttpClient okHttpClient = HttpClientFactory.okHttpClient(httpConfig); ApiJsonClient apiClient = new ApiJsonClient(httpConfig); // apiClient.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"); - //JSONObject jsonObject = apiClient.get("https://2022.ip138.com/"); + // JSONObject jsonObject = apiClient.get("https://2022.ip138.com/"); Map formParams = new HashMap<>(16); formParams.put("username", "admin"); formParams.put("password", "admin123"); - JSONMapper object = apiClient.postForm("https://www.baidu.com/?/auth/login", + JSONMapper object = apiClient.get("https://api.onbuy.com/v2/auth/request-token", formParams ); System.out.println("post = " + object); diff --git a/admin4j-common-http/src/test/java/io/github/admin4j/http/HttpRequestTest.java b/admin4j-common-http/src/test/java/io/github/admin4j/http/HttpRequestTest.java index c427074..6b7dd26 100644 --- a/admin4j-common-http/src/test/java/io/github/admin4j/http/HttpRequestTest.java +++ b/admin4j-common-http/src/test/java/io/github/admin4j/http/HttpRequestTest.java @@ -1,6 +1,7 @@ package io.github.admin4j.http; import io.github.admin4j.http.core.HttpHeaderKey; +import io.github.admin4j.http.exception.HttpException; import okhttp3.Response; import org.junit.jupiter.api.Test; @@ -21,7 +22,7 @@ void get() { @Test void post() { - Response response = HttpRequest.get("http://192.168.1.13:9100/auth/login") + Response response = HttpRequest.get("https://uatservice-etax.one.th/etaxbatchweb/etaxsignbatch") .queryMap("q", "admin4j") .header(HttpHeaderKey.USER_AGENT, "admin4j") .form("username", "admin") @@ -29,4 +30,22 @@ void post() { .execute(); System.out.println("response = " + response); } + + @Test + void postError() { + + //模拟http 404 异常 + try{ + Response response = HttpRequest.get("https://uatservice-etax.one.th/etaxbatchweb/etaxsignbatch") + .queryMap("q", "admin4j") + .header(HttpHeaderKey.USER_AGENT, "admin4j") + .form("username", "admin") + .form("password", "admin123") + .execute(); + System.out.println("response = " + response); + }catch(HttpException e){ + System.out.println("e = " + e); + } + + } } \ No newline at end of file diff --git a/pom.xml b/pom.xml index 26d7ef1..2de3248 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ UTF-8 1.8 - 0.8.0 + 0.9.5 @@ -103,18 +103,18 @@ - - - ${repository.releases.id} - nexus-releases - ${repository.releases.url} - - - ${repository.releases.id} - nexus-snapshot - ${repository.snapshots.url} - - + + + + + + + + + + + + @@ -122,13 +122,6 @@ false - - ossrh - https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ - - https://s01.oss.sonatype.org/content/repositories/snapshots - - @@ -193,17 +186,17 @@ - org.sonatype.plugins - nexus-staging-maven-plugin - 1.6.13 + org.sonatype.central + central-publishing-maven-plugin + 0.7.0 true - ossrh - https://s01.oss.sonatype.org/ - true - true + ossrh + true + published + org.apache.maven.plugins maven-gpg-plugin