Perform a single HTTP POST request against the API for a specific action. Use this method if you want single-item semantics (creating single assets, building single transactions) but the API endpoint is implemented as a batch call. Because request bodies for batch calls do not share a consistent fo
(
String action, Object body, final Type tClass, final Type eClass)
| 207 | * @throws ChainException |
| 208 | */ |
| 209 | public <T> T singletonBatchRequest( |
| 210 | String action, Object body, final Type tClass, final Type eClass) throws ChainException { |
| 211 | ResponseCreator<T> rc = |
| 212 | new ResponseCreator<T>() { |
| 213 | public T create(Response response, Gson deserializer) throws ChainException, IOException { |
| 214 | BatchResponse<T> batch = new BatchResponse<>(response, deserializer, tClass, eClass); |
| 215 | |
| 216 | List<APIException> errors = batch.errors(); |
| 217 | if (errors.size() == 1) { |
| 218 | // This throw must occur within this lambda in order for APIClient's |
| 219 | // retry logic to take effect. |
| 220 | throw errors.get(0); |
| 221 | } |
| 222 | |
| 223 | List<T> successes = batch.successes(); |
| 224 | if (successes.size() == 1) { |
| 225 | return successes.get(0); |
| 226 | } |
| 227 | |
| 228 | // We should never get here, unless there is a bug in either the SDK or |
| 229 | // API code, causing a non-singleton response. |
| 230 | throw new ChainException( |
| 231 | "Invalid singleton response, request ID " |
| 232 | + batch.response().headers().get("Chain-Request-ID")); |
| 233 | } |
| 234 | }; |
| 235 | return post(action, body, rc); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Returns the preferred base URL stored in the client. |