|
7 | 7 | import com.deepl.api.http.HttpResponse; |
8 | 8 | import com.deepl.api.http.HttpResponseStream; |
9 | 9 | import com.deepl.api.utils.*; |
| 10 | +import com.google.gson.JsonElement; |
| 11 | +import com.google.gson.JsonObject; |
10 | 12 | import java.io.*; |
11 | 13 | import java.net.URLEncoder; |
12 | 14 | import java.nio.charset.StandardCharsets; |
@@ -1055,6 +1057,166 @@ public List<StyleRuleInfo> getAllStyleRules() throws DeepLException, Interrupted |
1055 | 1057 | return getAllStyleRules(null, null, null); |
1056 | 1058 | } |
1057 | 1059 |
|
| 1060 | + /** |
| 1061 | + * Retrieves one page of the style profiles in your account. |
| 1062 | + * |
| 1063 | + * @param page Page number to retrieve (starting from 0), or <code>null</code> for the API default |
| 1064 | + * (0). |
| 1065 | + * @param pageSize Number of items per page, or <code>null</code> for the API default (10). The |
| 1066 | + * API reduces values above 100 to 100. |
| 1067 | + * @return List of {@link StyleProfile} objects on the requested page. |
| 1068 | + * @throws InterruptedException If the thread is interrupted during execution of this function. |
| 1069 | + * @throws DeepLException If any error occurs while communicating with the DeepL API. |
| 1070 | + */ |
| 1071 | + public List<StyleProfile> listStyleProfiles(@Nullable Integer page, @Nullable Integer pageSize) |
| 1072 | + throws DeepLException, InterruptedException { |
| 1073 | + ArrayList<KeyValuePair<String, String>> queryParams = new ArrayList<>(); |
| 1074 | + if (page != null) { |
| 1075 | + queryParams.add(new KeyValuePair<>("page", page.toString())); |
| 1076 | + } |
| 1077 | + if (pageSize != null) { |
| 1078 | + queryParams.add(new KeyValuePair<>("page_size", pageSize.toString())); |
| 1079 | + } |
| 1080 | + String relativeUrl = "/v3/style_profiles" + ApiClientHelper.createQueryString(queryParams); |
| 1081 | + HttpResponse response = httpClientWrapper.sendGetRequestWithBackoff(relativeUrl); |
| 1082 | + checkResponse(response, false, false); |
| 1083 | + return jsonParser.parseStyleProfileList(response.getBody()); |
| 1084 | + } |
| 1085 | + |
| 1086 | + /** |
| 1087 | + * Functions the same as {@link DeepLClient#listStyleProfiles(Integer, Integer)} but with default |
| 1088 | + * parameters (all null). |
| 1089 | + * |
| 1090 | + * @see DeepLClient#listStyleProfiles(Integer, Integer) |
| 1091 | + */ |
| 1092 | + public List<StyleProfile> listStyleProfiles() throws DeepLException, InterruptedException { |
| 1093 | + return listStyleProfiles(null, null); |
| 1094 | + } |
| 1095 | + |
| 1096 | + /** |
| 1097 | + * Creates a style profile and returns a {@link StyleProfile} object with details about it. If any |
| 1098 | + * of the given component IDs cannot be used, the request fails and no style profile is created. |
| 1099 | + * |
| 1100 | + * @param name Name of the style profile. |
| 1101 | + * @param options Optional description, industry and linked components, or <code>null</code>. |
| 1102 | + * Fields left <code>null</code> are not sent. |
| 1103 | + * @return {@link StyleProfile} object with details about the new style profile. |
| 1104 | + * @throws InterruptedException If the thread is interrupted during execution of this function. |
| 1105 | + * @throws DeepLException If any error occurs while communicating with the DeepL API. |
| 1106 | + */ |
| 1107 | + public StyleProfile createStyleProfile(String name, @Nullable StyleProfileOptions options) |
| 1108 | + throws DeepLException, InterruptedException { |
| 1109 | + HttpResponse response = |
| 1110 | + httpClientWrapper.sendJsonRequestWithBackoff( |
| 1111 | + "/v3/style_profiles", createStyleProfileBody(name, options)); |
| 1112 | + checkResponse(response, false, false); |
| 1113 | + return jsonParser.parseStyleProfile(response.getBody()); |
| 1114 | + } |
| 1115 | + |
| 1116 | + /** |
| 1117 | + * Functions the same as {@link DeepLClient#createStyleProfile(String, StyleProfileOptions)} but |
| 1118 | + * without options. |
| 1119 | + * |
| 1120 | + * @see DeepLClient#createStyleProfile(String, StyleProfileOptions) |
| 1121 | + */ |
| 1122 | + public StyleProfile createStyleProfile(String name) throws DeepLException, InterruptedException { |
| 1123 | + return createStyleProfile(name, null); |
| 1124 | + } |
| 1125 | + |
| 1126 | + /** |
| 1127 | + * Retrieves the style profile with the given ID. |
| 1128 | + * |
| 1129 | + * @param styleProfileId ID of the style profile to retrieve. |
| 1130 | + * @return {@link StyleProfile} object with details about the style profile. |
| 1131 | + * @throws IllegalArgumentException If the ID is <code>null</code> or empty. |
| 1132 | + * @throws NotFoundException If no style profile with the given ID is found. |
| 1133 | + * @throws InterruptedException If the thread is interrupted during execution of this function. |
| 1134 | + * @throws DeepLException If any error occurs while communicating with the DeepL API. |
| 1135 | + */ |
| 1136 | + public StyleProfile getStyleProfile(String styleProfileId) |
| 1137 | + throws DeepLException, InterruptedException { |
| 1138 | + HttpResponse response = |
| 1139 | + httpClientWrapper.sendGetRequestWithBackoff(styleProfilePath(styleProfileId)); |
| 1140 | + checkResponse(response, false, false); |
| 1141 | + return jsonParser.parseStyleProfile(response.getBody()); |
| 1142 | + } |
| 1143 | + |
| 1144 | + /** |
| 1145 | + * Functions the same as {@link DeepLClient#getStyleProfile(String)} but accepts a {@link |
| 1146 | + * StyleProfile} object. |
| 1147 | + * |
| 1148 | + * @see DeepLClient#getStyleProfile(String) |
| 1149 | + */ |
| 1150 | + public StyleProfile getStyleProfile(StyleProfile styleProfile) |
| 1151 | + throws DeepLException, InterruptedException { |
| 1152 | + return getStyleProfile(styleProfileId(styleProfile)); |
| 1153 | + } |
| 1154 | + |
| 1155 | + /** |
| 1156 | + * Updates the style profile with the given ID and returns the updated {@link StyleProfile}. |
| 1157 | + * Fields left <code>null</code> are not sent and stay unchanged. An empty string clears the |
| 1158 | + * description or industry; a component list that is sent replaces the linked list in full, and an |
| 1159 | + * empty list unlinks all components of that kind. With nothing set, nothing changes. |
| 1160 | + * |
| 1161 | + * @param styleProfileId ID of the style profile to update. |
| 1162 | + * @param name New name of the style profile, or <code>null</code> to keep the current name. |
| 1163 | + * @param options Fields to change, or <code>null</code>. |
| 1164 | + * @return {@link StyleProfile} object with details about the updated style profile. |
| 1165 | + * @throws IllegalArgumentException If the ID is <code>null</code> or empty. |
| 1166 | + * @throws NotFoundException If no style profile with the given ID is found. |
| 1167 | + * @throws InterruptedException If the thread is interrupted during execution of this function. |
| 1168 | + * @throws DeepLException If any error occurs while communicating with the DeepL API. |
| 1169 | + */ |
| 1170 | + public StyleProfile updateStyleProfile( |
| 1171 | + String styleProfileId, @Nullable String name, @Nullable StyleProfileOptions options) |
| 1172 | + throws DeepLException, InterruptedException { |
| 1173 | + String relativeUrl = styleProfilePath(styleProfileId); |
| 1174 | + HttpResponse response = |
| 1175 | + httpClientWrapper.sendJsonPatchRequestWithBackoff( |
| 1176 | + relativeUrl, createStyleProfileBody(name, options)); |
| 1177 | + checkResponse(response, false, false); |
| 1178 | + return jsonParser.parseStyleProfile(response.getBody()); |
| 1179 | + } |
| 1180 | + |
| 1181 | + /** |
| 1182 | + * Functions the same as {@link DeepLClient#updateStyleProfile(String, String, |
| 1183 | + * StyleProfileOptions)} but accepts a {@link StyleProfile} object. |
| 1184 | + * |
| 1185 | + * @see DeepLClient#updateStyleProfile(String, String, StyleProfileOptions) |
| 1186 | + */ |
| 1187 | + public StyleProfile updateStyleProfile( |
| 1188 | + StyleProfile styleProfile, @Nullable String name, @Nullable StyleProfileOptions options) |
| 1189 | + throws DeepLException, InterruptedException { |
| 1190 | + return updateStyleProfile(styleProfileId(styleProfile), name, options); |
| 1191 | + } |
| 1192 | + |
| 1193 | + /** |
| 1194 | + * Deletes the style profile with the given ID. |
| 1195 | + * |
| 1196 | + * @param styleProfileId ID of the style profile to delete. |
| 1197 | + * @throws IllegalArgumentException If the ID is <code>null</code> or empty. |
| 1198 | + * @throws NotFoundException If no style profile with the given ID is found. |
| 1199 | + * @throws InterruptedException If the thread is interrupted during execution of this function. |
| 1200 | + * @throws DeepLException If any error occurs while communicating with the DeepL API. |
| 1201 | + */ |
| 1202 | + public void deleteStyleProfile(String styleProfileId) |
| 1203 | + throws DeepLException, InterruptedException { |
| 1204 | + HttpResponse response = |
| 1205 | + httpClientWrapper.sendDeleteRequestWithBackoff(styleProfilePath(styleProfileId)); |
| 1206 | + checkResponse(response, false, false); |
| 1207 | + } |
| 1208 | + |
| 1209 | + /** |
| 1210 | + * Functions the same as {@link DeepLClient#deleteStyleProfile(String)} but accepts a {@link |
| 1211 | + * StyleProfile} object. |
| 1212 | + * |
| 1213 | + * @see DeepLClient#deleteStyleProfile(String) |
| 1214 | + */ |
| 1215 | + public void deleteStyleProfile(StyleProfile styleProfile) |
| 1216 | + throws DeepLException, InterruptedException { |
| 1217 | + deleteStyleProfile(styleProfileId(styleProfile)); |
| 1218 | + } |
| 1219 | + |
1058 | 1220 | /** |
1059 | 1221 | * Retrieves the languages supported by the given DeepL API resource, along with the features each |
1060 | 1222 | * language supports for that resource. |
@@ -1713,6 +1875,40 @@ private static String spokenTermsId(SpokenTerms spokenTerms) throws IllegalArgum |
1713 | 1875 | return spokenTerms.getSpokenTermsId(); |
1714 | 1876 | } |
1715 | 1877 |
|
| 1878 | + /** Extracts the ID of the given style profile. */ |
| 1879 | + private static String styleProfileId(StyleProfile styleProfile) throws IllegalArgumentException { |
| 1880 | + if (styleProfile == null) { |
| 1881 | + throw new IllegalArgumentException("styleProfile must not be null"); |
| 1882 | + } |
| 1883 | + return styleProfile.getStyleProfileId(); |
| 1884 | + } |
| 1885 | + |
| 1886 | + /** Validates the style profile ID and builds its URL path with the ID escaped. */ |
| 1887 | + private String styleProfilePath(String styleProfileId) throws DeepLException { |
| 1888 | + validateParameter("styleProfileId", styleProfileId); |
| 1889 | + try { |
| 1890 | + return "/v3/style_profiles/" + ApiClientHelper.encodeQueryComponent(styleProfileId); |
| 1891 | + } catch (UnsupportedEncodingException exception) { |
| 1892 | + throw new DeepLException("Error while URL-encoding request", exception); |
| 1893 | + } |
| 1894 | + } |
| 1895 | + |
| 1896 | + /** Builds the JSON body of a style profile request; Gson omits the options left null. */ |
| 1897 | + private String createStyleProfileBody( |
| 1898 | + @Nullable String name, @Nullable StyleProfileOptions options) { |
| 1899 | + JsonObject body = new JsonObject(); |
| 1900 | + if (name != null) { |
| 1901 | + body.addProperty("name", name); |
| 1902 | + } |
| 1903 | + if (options != null) { |
| 1904 | + for (Map.Entry<String, JsonElement> entry : |
| 1905 | + jsonParser.getGson().toJsonTree(options).getAsJsonObject().entrySet()) { |
| 1906 | + body.add(entry.getKey(), entry.getValue()); |
| 1907 | + } |
| 1908 | + } |
| 1909 | + return jsonParser.getGson().toJson(body); |
| 1910 | + } |
| 1911 | + |
1716 | 1912 | /** Builds the query string that selects the term list of the given language. */ |
1717 | 1913 | private static String langQueryString(String lang) { |
1718 | 1914 | List<KeyValuePair<String, String>> queryParams = new ArrayList<>(); |
|
0 commit comments