From 2555ab2d0f2c24b5666b301114af2fc75712f198 Mon Sep 17 00:00:00 2001 From: slowcheet4h <47327665+slowcheet4h@users.noreply.github.com> Date: Sat, 29 Jan 2022 23:26:15 +0300 Subject: [PATCH] patchRequest | YWebClient --- pisi/unitedmeows/yystal/web/YWebClient.java | 72 +++++++++++++++++++++ test/yystal/YTestStart.java | 22 +++++-- 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/pisi/unitedmeows/yystal/web/YWebClient.java b/pisi/unitedmeows/yystal/web/YWebClient.java index 7453aa5..fd0cc25 100644 --- a/pisi/unitedmeows/yystal/web/YWebClient.java +++ b/pisi/unitedmeows/yystal/web/YWebClient.java @@ -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 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 { diff --git a/test/yystal/YTestStart.java b/test/yystal/YTestStart.java index f9eae5e..9c5d465 100644 --- a/test/yystal/YTestStart.java +++ b/test/yystal/YTestStart.java @@ -1,10 +1,24 @@ 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.*; @@ -12,13 +26,9 @@ 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); }