|  | 
|  | 1 | +package com.regexsolver.api; | 
|  | 2 | + | 
|  | 3 | +import com.fasterxml.jackson.databind.ObjectMapper; | 
|  | 4 | +import com.regexsolver.api.Request.GenerateStringsRequest; | 
|  | 5 | +import com.regexsolver.api.Request.MultiTermsRequest; | 
|  | 6 | +import com.regexsolver.api.Response.BooleanResponse; | 
|  | 7 | +import com.regexsolver.api.Response.StringsResponse; | 
|  | 8 | +import com.regexsolver.api.dto.Details; | 
|  | 9 | +import com.regexsolver.api.exception.ApiError; | 
|  | 10 | +import com.regexsolver.api.exception.MissingAPITokenException; | 
|  | 11 | +import okhttp3.OkHttpClient; | 
|  | 12 | +import okhttp3.Request; | 
|  | 13 | +import okhttp3.ResponseBody; | 
|  | 14 | +import retrofit2.Call; | 
|  | 15 | +import retrofit2.Response; | 
|  | 16 | +import retrofit2.Retrofit; | 
|  | 17 | +import retrofit2.converter.jackson.JacksonConverterFactory; | 
|  | 18 | +import retrofit2.http.Body; | 
|  | 19 | +import retrofit2.http.POST; | 
|  | 20 | + | 
|  | 21 | +import java.io.IOException; | 
|  | 22 | +import java.util.List; | 
|  | 23 | +import java.util.Objects; | 
|  | 24 | + | 
|  | 25 | +final class RegexSolverApiWrapper { | 
|  | 26 | + private static final RegexSolverApiWrapper INSTANCE = new RegexSolverApiWrapper(); | 
|  | 27 | + | 
|  | 28 | + private final static String DEFAULT_BASE_URL = "https://api.regexsolver.com/"; | 
|  | 29 | + | 
|  | 30 | + private final static String USER_AGENT = "RegexSolver Java / 1.0.0"; | 
|  | 31 | + | 
|  | 32 | + private RegexApi api; | 
|  | 33 | + | 
|  | 34 | + public static RegexSolverApiWrapper getInstance() { | 
|  | 35 | + return INSTANCE; | 
|  | 36 | + } | 
|  | 37 | + | 
|  | 38 | + private RegexSolverApiWrapper() { | 
|  | 39 | + initializeInternal(null, DEFAULT_BASE_URL); | 
|  | 40 | + } | 
|  | 41 | + | 
|  | 42 | + public static void initialize(String token) { | 
|  | 43 | + getInstance().initializeInternal(token, DEFAULT_BASE_URL); | 
|  | 44 | + } | 
|  | 45 | + | 
|  | 46 | + public static void initialize(String token, String baseUrl) { | 
|  | 47 | + getInstance().initializeInternal(token, baseUrl); | 
|  | 48 | + } | 
|  | 49 | + | 
|  | 50 | + private void initializeInternal(String token, String baseUrl) { | 
|  | 51 | + Retrofit retrofit = new Retrofit.Builder() | 
|  | 52 | + .client(new OkHttpClient.Builder().addInterceptor(chain -> { | 
|  | 53 | + if (token == null) { | 
|  | 54 | + throw new MissingAPITokenException(); | 
|  | 55 | + } | 
|  | 56 | + Request newRequest = chain.request().newBuilder() | 
|  | 57 | + .addHeader("User-Agent", USER_AGENT) | 
|  | 58 | + .addHeader("Authorization", "Bearer " + token) | 
|  | 59 | + .build(); | 
|  | 60 | + return chain.proceed(newRequest); | 
|  | 61 | + }).build()) | 
|  | 62 | + .baseUrl(baseUrl) | 
|  | 63 | + .addConverterFactory(JacksonConverterFactory.create()) | 
|  | 64 | + .build(); | 
|  | 65 | + | 
|  | 66 | + api = retrofit.create(RegexApi.class); | 
|  | 67 | + } | 
|  | 68 | + | 
|  | 69 | + public Term computeIntersection(MultiTermsRequest multiTermsRequest) throws ApiError, IOException { | 
|  | 70 | + Response<Term> response = api.computeIntersection(multiTermsRequest).execute(); | 
|  | 71 | + if (response.isSuccessful()) { | 
|  | 72 | + return response.body(); | 
|  | 73 | + } else { | 
|  | 74 | + throw getApiError(response); | 
|  | 75 | + } | 
|  | 76 | + } | 
|  | 77 | + | 
|  | 78 | + public Term computeUnion(MultiTermsRequest multiTermsRequest) throws ApiError, IOException { | 
|  | 79 | + Response<Term> response = api.computeUnion(multiTermsRequest).execute(); | 
|  | 80 | + if (response.isSuccessful()) { | 
|  | 81 | + return response.body(); | 
|  | 82 | + } else { | 
|  | 83 | + throw getApiError(response); | 
|  | 84 | + } | 
|  | 85 | + } | 
|  | 86 | + | 
|  | 87 | + public Term computeSubtraction(MultiTermsRequest multiTermsRequest) throws ApiError, IOException { | 
|  | 88 | + Response<Term> response = api.computeSubtraction(multiTermsRequest).execute(); | 
|  | 89 | + if (response.isSuccessful()) { | 
|  | 90 | + return response.body(); | 
|  | 91 | + } else { | 
|  | 92 | + throw getApiError(response); | 
|  | 93 | + } | 
|  | 94 | + } | 
|  | 95 | + | 
|  | 96 | + public Details getDetails(Term term) throws ApiError, IOException { | 
|  | 97 | + Response<Details> response = api.getDetails(term).execute(); | 
|  | 98 | + if (response.isSuccessful()) { | 
|  | 99 | + return response.body(); | 
|  | 100 | + } else { | 
|  | 101 | + throw getApiError(response); | 
|  | 102 | + } | 
|  | 103 | + } | 
|  | 104 | + | 
|  | 105 | + public boolean equivalence(MultiTermsRequest multiTermsRequest) throws ApiError, IOException { | 
|  | 106 | + Response<BooleanResponse> response = api.equivalence(multiTermsRequest).execute(); | 
|  | 107 | + if (response.isSuccessful()) { | 
|  | 108 | + return response.body().value(); | 
|  | 109 | + } else { | 
|  | 110 | + throw getApiError(response); | 
|  | 111 | + } | 
|  | 112 | + } | 
|  | 113 | + | 
|  | 114 | + public boolean subset(MultiTermsRequest multiTermsRequest) throws ApiError, IOException { | 
|  | 115 | + Response<BooleanResponse> response = api.subset(multiTermsRequest).execute(); | 
|  | 116 | + if (response.isSuccessful()) { | 
|  | 117 | + return response.body().value(); | 
|  | 118 | + } else { | 
|  | 119 | + throw getApiError(response); | 
|  | 120 | + } | 
|  | 121 | + } | 
|  | 122 | + | 
|  | 123 | + public List<String> generateStrings(Term term, int count) throws ApiError, IOException { | 
|  | 124 | + GenerateStringsRequest generateStringsRequest = new GenerateStringsRequest(term, count); | 
|  | 125 | + Response<StringsResponse> response = api.generateStrings(generateStringsRequest).execute(); | 
|  | 126 | + if (response.isSuccessful()) { | 
|  | 127 | + return response.body().value(); | 
|  | 128 | + } else { | 
|  | 129 | + throw getApiError(response); | 
|  | 130 | + } | 
|  | 131 | + } | 
|  | 132 | + | 
|  | 133 | + private static <T> ApiError getApiError(Response<T> response) throws IOException { | 
|  | 134 | + assert !response.isSuccessful(); | 
|  | 135 | + try (ResponseBody errorBody = response.errorBody()) { | 
|  | 136 | + String json = Objects.requireNonNull(errorBody).string(); | 
|  | 137 | + ObjectMapper mapper = new ObjectMapper(); | 
|  | 138 | + return mapper.readValue(json, ApiError.class); | 
|  | 139 | + } | 
|  | 140 | + } | 
|  | 141 | + | 
|  | 142 | + private interface RegexApi { | 
|  | 143 | + @POST("api/compute/intersection") | 
|  | 144 | + Call<Term> computeIntersection(@Body MultiTermsRequest multiTermsRequest); | 
|  | 145 | + | 
|  | 146 | + @POST("api/compute/union") | 
|  | 147 | + Call<Term> computeUnion(@Body MultiTermsRequest multiTermsRequest); | 
|  | 148 | + | 
|  | 149 | + @POST("api/compute/subtraction") | 
|  | 150 | + Call<Term> computeSubtraction(@Body MultiTermsRequest multiTermsRequest); | 
|  | 151 | + | 
|  | 152 | + @POST("api/analyze/details") | 
|  | 153 | + Call<Details> getDetails(@Body Term term); | 
|  | 154 | + | 
|  | 155 | + @POST("api/analyze/equivalence") | 
|  | 156 | + Call<BooleanResponse> equivalence(@Body MultiTermsRequest multiTermsRequest); | 
|  | 157 | + | 
|  | 158 | + @POST("api/analyze/subset") | 
|  | 159 | + Call<BooleanResponse> subset(@Body MultiTermsRequest multiTermsRequest); | 
|  | 160 | + | 
|  | 161 | + @POST("api/generate/strings") | 
|  | 162 | + Call<StringsResponse> generateStrings(@Body GenerateStringsRequest request); | 
|  | 163 | + } | 
|  | 164 | +} | 
0 commit comments