Compare commits

7 Commits

Author SHA1 Message Date
45294b6299 v0.0.3
All checks were successful
CI/CD / build-deploy (pull_request) Successful in 8s
2026-02-10 15:30:48 +08:00
6ddd9928fc v0.0.3
Some checks failed
CI/CD / build-deploy (pull_request) Failing after 11s
2026-02-10 15:22:04 +08:00
7b46d2c0c4 add CI workflow
All checks were successful
CI/CD / build-deploy (pull_request) Successful in 1m16s
2026-01-19 17:58:03 +08:00
acb1304650 add CommonRequest.header 2025-11-07 23:38:25 +08:00
56afd301fb add CommonRequest.timeout 2025-11-07 22:50:44 +08:00
a240c00996 add CommonRequest.execute 2025-11-07 00:16:44 +08:00
fbcf21e990 add GsonRequest.body 2025-11-07 00:15:06 +08:00
9 changed files with 351 additions and 21 deletions

162
.gitea/workflows/ci.yml Normal file
View File

@ -0,0 +1,162 @@
name: CI/CD
on:
pull_request:
branches:
- master
types:
- closed
jobs:
build-deploy:
runs-on: act_runner_java
if: ${{ github.event.pull_request.merged == true }}
env:
JAVA_HOME: /usr/lib/jvm/java-21-openjdk
steps:
- name: Checkout code
run: |
git clone ${{ github.server_url }}/${{ github.repository }}.git .
git checkout ${{ github.sha }}
- name: Set up environment
run: |
echo "PR #${{ github.event.number }} merged into master"
echo "Source branch: ${{ github.event.pull_request.head.ref }}"
echo "Target branch: ${{ github.event.pull_request.base.ref }}"
- name: Run tests
run: |
echo "Running test suite..."
- name: Build project
run: |
mvn -B -DskipTests clean package source:jar javadoc:jar
- name: Deploy to Nexus
if: success()
run: |
if [ -z "${{ secrets.NEXUS_USERNAME }}" ] || [ -z "${{ secrets.NEXUS_PASSWORD }}" ]; then
echo "Missing secrets.NEXUS_USERNAME or secrets.NEXUS_PASSWORD"
exit 1
fi
mkdir -p ~/.m2
cat > ~/.m2/settings.xml <<EOF
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>timi-nexus</id>
<username>${{ secrets.NEXUS_USERNAME }}</username>
<password>${{ secrets.NEXUS_PASSWORD }}</password>
</server>
</servers>
</settings>
EOF
version=$(mvn -q -DforceStdout help:evaluate -Dexpression=project.version)
artifact_id=$(mvn -q -DforceStdout help:evaluate -Dexpression=project.artifactId)
main_jar="target/${artifact_id}-${version}.jar"
sources_jar="target/${artifact_id}-${version}-sources.jar"
javadoc_jar="target/${artifact_id}-${version}-javadoc.jar"
if [ ! -f "$main_jar" ] || [ ! -f "$sources_jar" ] || [ ! -f "$javadoc_jar" ]; then
echo "Missing build artifacts in target"
exit 1
fi
mvn -B deploy:deploy-file \
-Dfile="$main_jar" \
-Dsources="$sources_jar" \
-Djavadoc="$javadoc_jar" \
-DpomFile="./pom.xml" \
-Durl="https://nexus.imyeyu.com/repository/maven-releases/" \
-DrepositoryId="timi-nexus" \
-Dhttps.protocols=TLSv1.2 \
-Djdk.tls.client.protocols=TLSv1.2
- name: Create release
if: ${{ success() && startsWith(github.event.pull_request.title, 'v') }}
env:
GITEA_TOKEN: ${{ secrets.RUNNER_TOKEN }}
GITEA_SERVER_URL: ${{ github.server_url }}
GITEA_REPOSITORY: ${{ github.repository }}
RELEASE_TAG: ${{ github.event.pull_request.title }}
RELEASE_TARGET: ${{ github.sha }}
run: |
if [ -z "$GITEA_TOKEN" ]; then
echo "Missing secrets.RUNNER_TOKEN"
exit 1
fi
api_url="$GITEA_SERVER_URL/api/v1/repos/$GITEA_REPOSITORY/releases"
payload=$(cat <<EOF
{
"tag_name": "$RELEASE_TAG",
"name": "$RELEASE_TAG",
"target_commitish": "$RELEASE_TARGET",
"draft": false,
"prerelease": false
}
EOF
)
echo "Creating release with tag: $RELEASE_TAG"
echo "API URL: $api_url"
echo "Target commit: $RELEASE_TARGET"
http_code=$(curl -sS -w "%{http_code}" -o /tmp/release_response.json -X POST "$api_url" \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/json" \
-d "$payload")
response=$(cat /tmp/release_response.json)
echo "HTTP Status: $http_code"
echo "Response: $response"
if [ "$http_code" -ne 201 ]; then
echo "Failed to create release (HTTP $http_code)"
if echo "$response" | grep -q "already exists"; then
echo "Release with tag $RELEASE_TAG already exists"
fi
exit 1
fi
release_id=$(echo "$response" | grep -oP '"id":\K[0-9]+' | head -n 1 || true)
if [ -z "$release_id" ]; then
echo "Failed to extract release ID from response"
exit 1
fi
echo "Release created: id=$release_id"
echo "Listing jar files in target directory:"
ls -lh target/*.jar || echo "No jar files found"
upload_count=0
for asset_path in target/*.jar; do
if [ ! -f "$asset_path" ]; then
echo "Skipping non-existent file: $asset_path"
continue
fi
asset_name=$(basename "$asset_path")
file_size=$(stat -c%s "$asset_path" 2>/dev/null || echo "unknown")
echo "Uploading asset: $asset_name (size: $file_size bytes)"
upload_url="$api_url/$release_id/assets?name=$asset_name"
echo "Upload URL: $upload_url"
set +e
http_code=$(curl -sS -w "%{http_code}" -o /tmp/asset_response.json -X POST "$upload_url" \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary @"$asset_path" 2>/dev/null)
curl_exit=$?
set -e
if [ $curl_exit -ne 0 ]; then
echo "✗ Curl failed with exit code $curl_exit for $asset_name"
cat /tmp/asset_response.json 2>/dev/null || echo "No response file"
continue
fi
if [ "$http_code" = "201" ]; then
echo "✓ Successfully uploaded: $asset_name"
upload_count=$((upload_count + 1))
else
echo "✗ Failed to upload $asset_name (HTTP $http_code)"
cat /tmp/asset_response.json 2>/dev/null || echo "No response body"
fi
done
echo "Upload complete: $upload_count file(s) uploaded"

22
pom.xml
View File

@ -6,7 +6,7 @@
<groupId>com.imyeyu.network</groupId> <groupId>com.imyeyu.network</groupId>
<artifactId>timi-network</artifactId> <artifactId>timi-network</artifactId>
<version>0.0.1</version> <version>0.0.3</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
@ -27,29 +27,11 @@
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId> <artifactId>maven-source-plugin</artifactId>
<version>3.3.1</version> <version>3.3.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId> <artifactId>maven-javadoc-plugin</artifactId>
<version>3.11.2</version> <version>3.11.2</version>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
@ -78,7 +60,7 @@
<dependency> <dependency>
<groupId>com.imyeyu.io</groupId> <groupId>com.imyeyu.io</groupId>
<artifactId>timi-io</artifactId> <artifactId>timi-io</artifactId>
<version>0.0.1</version> <version>0.0.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.httpcomponents.client5</groupId> <groupId>org.apache.httpcomponents.client5</groupId>

View File

@ -35,6 +35,36 @@ public class ArgMap<K, V> extends HashMap<K, V> {
} }
public String toURL(String url) { public String toURL(String url) {
StringBuilder sb = new StringBuilder(url);
if (url.contains("?")) {
if (!url.endsWith("?") && !url.endsWith("&")) {
sb.append('&');
}
sb.append(toURL());
return sb.toString();
} else {
return url + "?" + toURL(); return url + "?" + toURL();
} }
} }
public static <K, V> ArgMap<K, V> of(K key, V value) {
ArgMap<K, V> map = new ArgMap<>();
map.put(key, value);
return map;
}
public static <K, V> ArgMap<K, V> of(K key1, V value1, K key2, V value2) {
ArgMap<K, V> map = new ArgMap<>();
map.put(key1, value1);
map.put(key2, value2);
return map;
}
public static <K, V> ArgMap<K, V> of(K key1, V value1, K key2, V value2, K key3, V value3) {
ArgMap<K, V> map = new ArgMap<>();
map.put(key1, value1);
map.put(key2, value2);
map.put(key3, value3);
return map;
}
}

View File

@ -1,6 +1,7 @@
package com.imyeyu.network; package com.imyeyu.network;
import org.apache.hc.client5.http.fluent.Request; import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.util.Timeout;
import java.io.IOException; import java.io.IOException;
@ -28,6 +29,26 @@ public class CommonRequest {
return new CommonRequest(Request.post(url)); return new CommonRequest(Request.post(url));
} }
public CommonRequest timeout(long ms) {
request.connectTimeout(Timeout.ofMilliseconds(ms)).responseTimeout(Timeout.ofMilliseconds(ms));
return this;
}
public CommonRequest header(String key, String value) {
request.addHeader(key, value);
return this;
}
public CommonRequest token(String token) {
request.addHeader("Token", token);
return this;
}
public CommonRequest language(String langHeader) {
request.addHeader("Accept-Language", langHeader);
return this;
}
public String asString() throws IOException { public String asString() throws IOException {
return request.execute().returnContent().asString(); return request.execute().returnContent().asString();
} }
@ -39,4 +60,8 @@ public class CommonRequest {
public byte[] asBytes() throws IOException { public byte[] asBytes() throws IOException {
return request.execute().returnContent().asBytes(); return request.execute().returnContent().asBytes();
} }
public void execute() throws IOException {
request.execute();
}
} }

View File

@ -34,6 +34,27 @@ public class FileRequest extends CommonRequest {
return new FileRequest(Request.post(url)); return new FileRequest(Request.post(url));
} }
@Override
public FileRequest timeout(long ms) {
super.timeout(ms);
return this;
}
public FileRequest header(String key, String value) {
request.addHeader(key, value);
return this;
}
public FileRequest token(String token) {
request.addHeader("Token", token);
return this;
}
public FileRequest language(String langHeader) {
request.addHeader("Accept-Language", langHeader);
return this;
}
public void toFile(String path, String fileName) throws IOException, NoPermissionException { public void toFile(String path, String fileName) throws IOException, NoPermissionException {
toFile(new File(IO.fitPath(path) + fileName)); toFile(new File(IO.fitPath(path) + fileName));
} }

View File

@ -7,6 +7,7 @@ import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.imyeyu.java.TimiJava; import com.imyeyu.java.TimiJava;
import org.apache.hc.client5.http.fluent.Request; import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.ContentType;
import java.io.IOException; import java.io.IOException;
@ -45,6 +46,32 @@ public class GsonRequest extends CommonRequest {
return this; return this;
} }
@Override
public GsonRequest timeout(long ms) {
super.timeout(ms);
return this;
}
public GsonRequest header(String key, String value) {
request.addHeader(key, value);
return this;
}
public GsonRequest token(String token) {
request.addHeader("Token", token);
return this;
}
public GsonRequest language(String langHeader) {
request.addHeader("Accept-Language", langHeader);
return this;
}
public GsonRequest body(Object object) {
request.bodyString(getGson().toJson(object), ContentType.APPLICATION_JSON);
return this;
}
public <T> T resultAs(Class<T> clazz) throws IOException { public <T> T resultAs(Class<T> clazz) throws IOException {
return getGson().fromJson(super.asString(), clazz); return getGson().fromJson(super.asString(), clazz);
} }

View File

@ -42,6 +42,27 @@ public class ProgressiveRequest extends FileRequest {
return new ProgressiveRequest(Request.post(url), callback); return new ProgressiveRequest(Request.post(url), callback);
} }
@Override
public ProgressiveRequest timeout(long ms) {
super.timeout(ms);
return this;
}
public ProgressiveRequest header(String key, String value) {
request.addHeader(key, value);
return this;
}
public ProgressiveRequest token(String token) {
request.addHeader("Token", token);
return this;
}
public ProgressiveRequest language(String langHeader) {
request.addHeader("Accept-Language", langHeader);
return this;
}
@Override @Override
public void toFile(Path outputPath) throws IOException, NoPermissionException { public void toFile(Path outputPath) throws IOException, NoPermissionException {
processResponse(request.execute(), IO.getOutputStream(outputPath.toFile())); processResponse(request.execute(), IO.getOutputStream(outputPath.toFile()));

View File

@ -28,6 +28,33 @@ public class TimiRequest extends GsonRequest {
return new TimiRequest(Request.post(url)); return new TimiRequest(Request.post(url));
} }
@Override
public TimiRequest timeout(long ms) {
super.timeout(ms);
return this;
}
public TimiRequest header(String key, String value) {
request.addHeader(key, value);
return this;
}
public TimiRequest token(String token) {
request.addHeader("Token", token);
return this;
}
public TimiRequest language(String langHeader) {
request.addHeader("Accept-Language", langHeader);
return this;
}
@Override
public TimiRequest body(Object object) {
super.body(object);
return this;
}
@Override @Override
public String asString() throws IOException { public String asString() throws IOException {
return resultAs(String.class); return resultAs(String.class);
@ -53,4 +80,12 @@ public class TimiRequest extends GsonRequest {
} }
return resp.getData(); return resp.getData();
} }
@Override
public void execute() throws IOException {
TimiResponse<?> resp = getGson().fromJson(asJsonObject(), TypeToken.getParameterized(TimiResponse.class, Object.class).getType());
if (resp.isFail()) {
throw resp.toException();
}
}
} }

View File

@ -0,0 +1,27 @@
package com.imyeyu.network.test;
import com.imyeyu.network.ArgMap;
import org.junit.Test;
/**
*
*
* @author 夜雨
* @since 2026-02-10 12:45
*/
public class ArgMapTest {
@Test
public void toURLTest() {
assert ArgMap.of("key", "value").toURL("/detail").equals("/detail?key=value");
assert ArgMap.of("key", "value").toURL("/detail?").equals("/detail?key=value");
assert ArgMap.of("key", "value").toURL("/detail?id=123").equals("/detail?id=123&key=value");
assert ArgMap.of("key", "value").toURL("/detail?id=123&").equals("/detail?id=123&key=value");
assert ArgMap.of("key", "value").toURL("http://localhost/detail").equals("http://localhost/detail?key=value");
assert ArgMap.of("key", "value").toURL("http://localhost/detail?").equals("http://localhost/detail?key=value");
String uri = ArgMap.of("key", "value").toURL("/detail?id=123");
String url = ArgMap.of("newKey", "newValue").toURL("http://localhost/test" + uri);
assert url.equals("http://localhost/test/detail?id=123&key=value&newKey=newValue");
}
}