Skip to content

Commit

Permalink
patchRequest | YWebClient
Browse files Browse the repository at this point in the history
  • Loading branch information
slowcheet4h committed Jan 29, 2022
1 parent a55ac39 commit 2555ab2
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
72 changes: 72 additions & 0 deletions pisi/unitedmeows/yystal/web/YWebClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,78 @@ public String putRequest(URL url, String value, String contentType) {
return null;
}
}
public String patchRequest(String url, String value) {
return patchRequest(url, value, "application/json");
}

public String patchRequest(String url, String value, String contentType) {
try {
return patchRequest(new URL(url), value, contentType);
} catch (MalformedURLException ex) {
return null;
}
}

public String patchRequest(URL url, String value) {
return patchRequest(url, value, "application/json");
}

public String patchRequest(URL url, String value, String contentType) {
try {
URLConnection connection = url.openConnection();

/* setup http connection */
HttpURLConnection http = (HttpURLConnection) connection;
http.setRequestMethod("PATCH");
http.setDoOutput(true);

/* add headers */
headers.forEach(connection::addRequestProperty);



byte[] out = value.getBytes(StandardCharsets.UTF_8);
int length = out.length;
http.setFixedLengthStreamingMode(length);
http.setRequestProperty("Content-Type", contentType);
http.setRequestProperty("charset", "utf-8");
http.setRequestProperty("Content-Length", Integer.toString( length ));
http.setInstanceFollowRedirects( false );
http.setUseCaches( false );
http.connect();



try(OutputStream os = http.getOutputStream()) {
os.write(out);
}
/* check for redirects */
out<URL> newUrl = YYStal.out();
if (redirectCheck(http, newUrl)) {
return postRequest(newUrl.get(), value, contentType);
}

responseHeaders = connection.getHeaderFields();
StringBuilder stringBuilder = new StringBuilder();
if (http.getResponseCode() == HttpURLConnection.HTTP_OK) {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(http.getInputStream()))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
}
} else if (http.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT) {
return "";
}else {
return null;
}
return stringBuilder.toString();

} catch (Exception ex) {
return null;
}
}


public String deleteRequest(URL url) {
try {
Expand Down
22 changes: 16 additions & 6 deletions test/yystal/YTestStart.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
package test.yystal;

import pisi.unitedmeows.yystal.YYStal;
import pisi.unitedmeows.yystal.exception.impl.YexIO;
import pisi.unitedmeows.yystal.file.YFile;
import pisi.unitedmeows.yystal.utils.Capsule;
import pisi.unitedmeows.yystal.utils.CoID;
import pisi.unitedmeows.yystal.utils.YRandom;
import pisi.unitedmeows.yystal.utils.kThread;
import pisi.unitedmeows.yystal.web.YWebClient;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static pisi.unitedmeows.yystal.parallel.Async.*;
import static pisi.unitedmeows.yystal.YYStal.*;

public enum YTestStart {
gaming; /* :D */

public static void main(final String[] args) {
System.out.println(CoID.generate());
Capsule capsule = Capsule.of(pair("hello", 5), pair("var2", "test3"), pair("token", 12));



System.out.println((String)capsule.get("hello"));
capsule.ifExists("var2", o -> {
System.out.println("var2 exists");
});

kThread.sleep(1099900);
}
Expand Down

0 comments on commit 2555ab2

Please sign in to comment.