Skip to content

Commit 8b3d05d

Browse files
committed
1.40
1 parent 3350590 commit 8b3d05d

File tree

7 files changed

+135
-13
lines changed

7 files changed

+135
-13
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package dev.felnull.fnjl;
22

33
public class FNJLBuildIn {
4-
protected static final String VERSION = "1.39";
4+
protected static final String VERSION = "1.40";
55
}

common/src/main/java/dev/felnull/fnjl/tuple/FNPair.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package dev.felnull.fnjl.tuple;
22

33
public interface FNPair<K, E> {
4+
5+
public static <K, E> FNPair<K, E> of(K key, E entry) {
6+
return new SimpleFNPair<>(key, entry);
7+
}
8+
49
public K getKey();
510

611
public E getEntry();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package dev.felnull.fnjl.util;
2+
3+
import java.io.IOException;
4+
import java.net.URL;
5+
import java.util.concurrent.CompletableFuture;
6+
import java.util.function.Consumer;
7+
8+
/**
9+
* DiscordのWebhookを送るためのbuilder
10+
*
11+
* @author MORIMORI0317
12+
* @since 1.40
13+
*/
14+
public class DiscordWebHookBuilder {
15+
private final String url;
16+
private final String content;
17+
private String userName;
18+
private String avatarUrl;
19+
20+
public DiscordWebHookBuilder(String url, String content) {
21+
this.url = url;
22+
this.content = content;
23+
}
24+
25+
public DiscordWebHookBuilder setUsername(String userName) {
26+
this.userName = userName;
27+
return this;
28+
}
29+
30+
public DiscordWebHookBuilder setAvatarUrl(String avatarUrl) {
31+
this.avatarUrl = avatarUrl;
32+
return this;
33+
}
34+
35+
public String createContent() {
36+
StringBuilder sb = new StringBuilder();
37+
sb.append("{");
38+
if (userName != null)
39+
sb.append(String.format("\"username\": \"%s\",", userName));
40+
if (avatarUrl != null)
41+
sb.append(String.format("\"avatar_url\": \"%s\",", avatarUrl));
42+
sb.append(String.format("\"content\": \"%s\"", content));
43+
sb.append("}");
44+
return sb.toString();
45+
}
46+
47+
public int send() throws IOException {
48+
return FNURLUtil.getResponseByPOST(new URL(url), createContent(), "jp", "application/JSON").getEntry();
49+
}
50+
51+
public CompletableFuture<Void> sendAsync(Consumer<Integer> response) throws IOException {
52+
return FNURLUtil.getResponseByPOSTAsync(new URL(url), createContent(), "jp", "application/JSON", n -> {
53+
if (response != null)
54+
response.accept(n.getRight());
55+
});
56+
}
57+
}

common/src/main/java/dev/felnull/fnjl/util/FNURLUtil.java

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package dev.felnull.fnjl.util;
22

33
import dev.felnull.fnjl.FelNullJavaLibrary;
4+
import dev.felnull.fnjl.tuple.FNPair;
45

5-
import java.io.BufferedReader;
6-
import java.io.IOException;
7-
import java.io.InputStream;
8-
import java.io.InputStreamReader;
6+
import java.io.*;
97
import java.net.HttpURLConnection;
108
import java.net.URL;
119
import java.nio.charset.StandardCharsets;
@@ -113,4 +111,70 @@ public static CompletableFuture<Void> getResponseAsync(URL url, Consumer<String>
113111
stringConsumer.accept(str);
114112
});
115113
}
114+
115+
/**
116+
* POSTでテキストを送り返ってきた文字列とステータスコードを取得
117+
*
118+
* @param url URL
119+
* @param body テキスト
120+
* @param language 言語
121+
* @param contentType type
122+
* @param responseConsumer 返答とステータスコードのペア
123+
* @return 返答とステータスコードのペア
124+
*/
125+
public static CompletableFuture<Void> getResponseByPOSTAsync(URL url, String body, String language, String contentType, Consumer<FNPair<String, Integer>> responseConsumer) {
126+
return CompletableFuture.runAsync(() -> {
127+
FNPair<String, Integer> ret = null;
128+
try {
129+
ret = getResponseByPOST(url, body, language, contentType);
130+
} catch (IOException e) {
131+
e.printStackTrace();
132+
}
133+
responseConsumer.accept(ret);
134+
});
135+
}
136+
137+
/**
138+
* POSTでテキストを送り返ってきた文字列とステータスコードを取得
139+
*
140+
* @param url URL
141+
* @param body テキスト
142+
* @param language 言語
143+
* @param contentType type
144+
* @return 返答とステータスコードのペア
145+
* @throws IOException 失敗
146+
*/
147+
public static FNPair<String, Integer> getResponseByPOST(URL url, String body, String language, String contentType) throws IOException {
148+
HttpURLConnection con = getConnection(url);
149+
con.setDoOutput(true);
150+
con.setRequestMethod("POST");
151+
con.setRequestProperty("Accept-Language", language);
152+
con.setRequestProperty("Content-Type", String.format("%s; charset=utf-8", contentType));
153+
con.setRequestProperty("Content-Length", String.valueOf(body.getBytes(StandardCharsets.UTF_8).length));
154+
155+
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
156+
out.write(body);
157+
out.flush();
158+
con.connect();
159+
int sts = con.getResponseCode();
160+
StringBuilder sb = new StringBuilder();
161+
162+
InputStream in = con.getInputStream();
163+
String encoding = con.getContentEncoding();
164+
if (null == encoding) {
165+
encoding = "UTF-8";
166+
}
167+
InputStreamReader inReader = new InputStreamReader(in, encoding);
168+
BufferedReader bufReader = new BufferedReader(inReader);
169+
170+
String line;
171+
while ((line = bufReader.readLine()) != null) {
172+
sb.append(line);
173+
}
174+
bufReader.close();
175+
inReader.close();
176+
in.close();
177+
return FNPair.of(sb.toString(), sts);
178+
}
179+
116180
}
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package dev.felnull.fnjltest;
22

3-
import dev.felnull.fnjl.util.FNStringUtil;
4-
53
public class Main {
6-
public static void main(String[] args) {
7-
//System.out.println(FNMath.trans4to2CornerPlanes(new FNVec2f(0.2f, 0.1f), new FNVec2f(0.2f, 0.75f), new FNVec2f(0.8f, 0.1f), new FNVec2f(0.8f, 0.75f)));
8-
// System.out.println(FNMath.min(1919, 810, 114514, 19, 24, 19, 810, 32));
9-
System.out.println(FNStringUtil.getByteDisplay(1024 * 1024 * 8,1024));
4+
public static void main(String[] args) throws Exception {
5+
106
}
117
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
fnjl_group=dev.felnull
22
fnjl_name=felnull-java-library
3-
fnjl_version=1.39
3+
fnjl_version=1.40
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dev.felnull.fnjln;
22

33
public class FNJLNBuildIn {
4-
protected static final String VERSION = "1.39";
4+
protected static final String VERSION = "1.40";
55

66
protected static final int NATIVE_LIBRARY_VERSION = 1;
77
}

0 commit comments

Comments
 (0)