Class OkHttpClientHelper

  • All Implemented Interfaces:

    
    public class OkHttpClientHelper
    
                        

    Who uses it, who should close it, in a try-close pattern.

    Since:

    2020-06-02

    Author:

    trydofor

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
      public final static RequestBody EMPTY
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      static OkHttpClient staticClient() global static client
      static OkHttpClient springClient() inject Spring Configured client
      static MultipartBody.Builder postFile(@NotNull() String key, @NotNull() File file)
      static MultipartBody.Builder postFile(@NotNull() String key, Array<byte> file, @NotNull() String fileName)
      static MultipartBody.Builder postFile(@NotNull() String key, @NotNull() InputStream file, @NotNull() String fileName)
      static String postFile(@NotNull() OkHttpClient client, @NotNull() String url, @NotNull() String key, @NotNull() File file)
      static String postFile(@NotNull() Call.Factory callFactory, @NotNull() String url, @NotNull() String key, @NotNull() File file)
      static String postFile(@NotNull() OkHttpClient client, @NotNull() String url, @NotNull() String key, Array<byte> file, @NotNull() String fileName)
      static String postFile(@NotNull() Call.Factory callFactory, @NotNull() String url, @NotNull() String key, Array<byte> file, @NotNull() String fileName)
      static String postFile(@NotNull() OkHttpClient client, @NotNull() String url, @NotNull() String key, @NotNull() InputStream file, @NotNull() String fileName)
      static String postFile(@NotNull() Call.Factory callFactory, @NotNull() String url, @NotNull() String key, @NotNull() InputStream file, @NotNull() String fileName)
      static String postFile(@NotNull() OkHttpClient client, @NotNull() String url, @NotNull() MultipartBody body)
      static String postFile(@NotNull() Call.Factory callFactory, @NotNull() String url, @NotNull() MultipartBody body)
      static String postJson(@NotNull() OkHttpClient client, @NotNull() String url, @Nullable() CharSequence json)
      static String postJson(@NotNull() Call.Factory callFactory, @NotNull() String url, @Nullable() CharSequence json)
      static ResponseBody extract(@Nullable() Response response)
      static String extractString(@Nullable() Response response)
      static String extractString(@Nullable() Response response, boolean nullWhenThrow)
      static String extractString(@Nullable() ResponseBody body)
      static String extractString(@Nullable() ResponseBody body, boolean nullWhenThrow)
      static Array<byte> download(@NotNull() OkHttpClient client, @NotNull() String url)
      static Array<byte> download(@NotNull() Call.Factory callFactory, @NotNull() String url)
      static Array<byte> download(@NotNull() OkHttpClient client, @NotNull() String url, @NotNull() String method)
      static RequestBody emptyBody(@NotNull() String method) return empty body or null according to method
      static Array<byte> download(@NotNull() Call.Factory callFactory, @NotNull() String url, @NotNull() String method)
      static String getText(@NotNull() OkHttpClient client, @NotNull() String url)
      static String getText(@NotNull() Call.Factory callFactory, @NotNull() String url)
      static String executeString(@NotNull() OkHttpClient client, @NotNull() Request request, boolean nullWhenThrow)
      static String executeString(@NotNull() Call.Factory callFactory, @NotNull() Request request, boolean nullWhenThrow)
      static String executeJson(@NotNull() OkHttpClient client, @NotNull() String url, @Nullable() CharSequence json, @NotNull() String method)
      static String executeJson(@NotNull() Call.Factory callFactory, @NotNull() String url, @Nullable() CharSequence json, @NotNull() String method)
      static Response execute(@NotNull() OkHttpClient client, @NotNull() Request request)
      static Response execute(@NotNull() Call.Factory callFactory, @NotNull() Request request)
      static Response execute(@NotNull() OkHttpClient client, @NotNull() Request request, boolean nullWhenThrow)
      static Response execute(@NotNull() Call.Factory callFactory, @NotNull() Request request, boolean nullWhenThrow)
      static <T> T execute(@NotNull() OkHttpClient client, @NotNull() Request request, @NotNull() BiFunction<Response, IOException, T> fun)
      static <T> T execute(@NotNull() Call.Factory callFactory, @NotNull() Request request, @NotNull() BiFunction<Response, IOException, T> fun)
      static Response execute(@NotNull() OkHttpClient client, @NotNull() Request.Builder builder)
      static Response execute(@NotNull() Call.Factory callFactory, @NotNull() Request.Builder builder)
      static Response execute(@NotNull() OkHttpClient client, @NotNull() Request.Builder builder, boolean nullWhenThrow)
      static Response execute(@NotNull() Call.Factory callFactory, @NotNull() Request.Builder builder, boolean nullWhenThrow)
      static <T> T execute(@NotNull() OkHttpClient client, @NotNull() Request.Builder builder, @NotNull() BiFunction<Response, IOException, T> fun)
      static <T> T execute(@NotNull() Call.Factory callFactory, @NotNull() Request.Builder builder, @NotNull() BiFunction<Response, IOException, T> fun)
      static void clearCookie(@NotNull() OkHttpClient client, @NotNull() HttpUrl url)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • OkHttpClientHelper

        OkHttpClientHelper()
    • Method Detail

      • staticClient

        @NotNull() static OkHttpClient staticClient()

        global static client

      • springClient

        @NotNull() static OkHttpClient springClient()

        inject Spring Configured client

      • postFile

        @NotNull() static MultipartBody.Builder postFile(@NotNull() String key, @NotNull() File file)
      • postFile

        @NotNull() static MultipartBody.Builder postFile(@NotNull() String key, Array<byte> file, @NotNull() String fileName)
      • postFile

        @NotNull() static String postFile(@NotNull() OkHttpClient client, @NotNull() String url, @NotNull() String key, @NotNull() File file)
      • postFile

        @NotNull() static String postFile(@NotNull() Call.Factory callFactory, @NotNull() String url, @NotNull() String key, @NotNull() File file)
      • postFile

        @NotNull() static String postFile(@NotNull() OkHttpClient client, @NotNull() String url, @NotNull() MultipartBody body)
      • postFile

        @NotNull() static String postFile(@NotNull() Call.Factory callFactory, @NotNull() String url, @NotNull() MultipartBody body)
      • extract

        @Nullable() static ResponseBody extract(@Nullable() Response response)
      • extractString

        @Nullable()@Contract(value = "_,false->!null") static String extractString(@Nullable() Response response, boolean nullWhenThrow)
      • extractString

        @Nullable()@Contract(value = "_,false->!null") static String extractString(@Nullable() ResponseBody body, boolean nullWhenThrow)
      • download

         static Array<byte> download(@NotNull() OkHttpClient client, @NotNull() String url)
      • download

         static Array<byte> download(@NotNull() Call.Factory callFactory, @NotNull() String url)
      • download

         static Array<byte> download(@NotNull() OkHttpClient client, @NotNull() String url, @NotNull() String method)
      • emptyBody

        @Nullable() static RequestBody emptyBody(@NotNull() String method)

        return empty body or null according to method

      • download

         static Array<byte> download(@NotNull() Call.Factory callFactory, @NotNull() String url, @NotNull() String method)
      • getText

        @NotNull() static String getText(@NotNull() OkHttpClient client, @NotNull() String url)
      • getText

        @NotNull() static String getText(@NotNull() Call.Factory callFactory, @NotNull() String url)
      • executeString

        @Contract(value = "_,_,false->!null") static String executeString(@NotNull() OkHttpClient client, @NotNull() Request request, boolean nullWhenThrow)
      • executeString

        @Contract(value = "_,_,false->!null") static String executeString(@NotNull() Call.Factory callFactory, @NotNull() Request request, boolean nullWhenThrow)
      • execute

        @NotNull() static Response execute(@NotNull() OkHttpClient client, @NotNull() Request request)
      • execute

        @NotNull() static Response execute(@NotNull() Call.Factory callFactory, @NotNull() Request request)
      • execute

        @Contract(value = "_,_,false->!null") static Response execute(@NotNull() OkHttpClient client, @NotNull() Request request, boolean nullWhenThrow)
      • execute

        @Contract(value = "_,_,false->!null") static Response execute(@NotNull() Call.Factory callFactory, @NotNull() Request request, boolean nullWhenThrow)
      • execute

        @Nullable() static <T> T execute(@NotNull() OkHttpClient client, @NotNull() Request request, @NotNull() BiFunction<Response, IOException, T> fun)
      • execute

        @Nullable() static <T> T execute(@NotNull() Call.Factory callFactory, @NotNull() Request request, @NotNull() BiFunction<Response, IOException, T> fun)
      • execute

        @NotNull() static Response execute(@NotNull() OkHttpClient client, @NotNull() Request.Builder builder)
      • execute

        @NotNull() static Response execute(@NotNull() Call.Factory callFactory, @NotNull() Request.Builder builder)
      • execute

        @Contract(value = "_,_,false->!null") static Response execute(@NotNull() OkHttpClient client, @NotNull() Request.Builder builder, boolean nullWhenThrow)
      • execute

        @Contract(value = "_,_,false->!null") static Response execute(@NotNull() Call.Factory callFactory, @NotNull() Request.Builder builder, boolean nullWhenThrow)
      • execute

        @Nullable() static <T> T execute(@NotNull() OkHttpClient client, @NotNull() Request.Builder builder, @NotNull() BiFunction<Response, IOException, T> fun)
      • execute

         static <T> T execute(@NotNull() Call.Factory callFactory, @NotNull() Request.Builder builder, @NotNull() BiFunction<Response, IOException, T> fun)
      • clearCookie

         static void clearCookie(@NotNull() OkHttpClient client, @NotNull() HttpUrl url)