-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b4e5e2
commit 29cdb03
Showing
25 changed files
with
2,282 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/com/basis/theory/api/core/pagination/BasePage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* This file was auto-generated by Fern from our API Definition. | ||
*/ | ||
package com.basis.theory.api.core.pagination; | ||
|
||
import java.util.List; | ||
|
||
public abstract class BasePage<T> { | ||
private final boolean hasNext; | ||
private final List<T> items; | ||
|
||
public BasePage(boolean hasNext, List<T> items) { | ||
this.hasNext = hasNext; | ||
this.items = items; | ||
} | ||
|
||
public boolean hasNext() { | ||
return !items.isEmpty() && hasNext; | ||
} | ||
|
||
public List<T> getItems() { | ||
return items; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/basis/theory/api/core/pagination/SyncPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* This file was auto-generated by Fern from our API Definition. | ||
*/ | ||
package com.basis.theory.api.core.pagination; | ||
|
||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.function.Supplier; | ||
|
||
public class SyncPage<T> extends BasePage<T> { | ||
protected final Supplier<? extends SyncPage<T>> nextSupplier; | ||
|
||
public SyncPage(boolean hasNext, List<T> items, Supplier<? extends SyncPage<T>> nextSupplier) { | ||
super(hasNext, items); | ||
this.nextSupplier = nextSupplier; | ||
} | ||
|
||
public SyncPage<T> nextPage() { | ||
if (!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
return nextSupplier.get(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/basis/theory/api/core/pagination/SyncPagingIterable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* This file was auto-generated by Fern from our API Definition. | ||
*/ | ||
package com.basis.theory.api.core.pagination; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
public class SyncPagingIterable<T> extends SyncPage<T> implements Iterable<T> { | ||
|
||
public SyncPagingIterable(boolean hasNext, List<T> items, Supplier<? extends SyncPage<T>> getNext) { | ||
super(hasNext, items, getNext); | ||
} | ||
|
||
public SyncPagingIterable(boolean hasNext, Optional<List<T>> items, Supplier<? extends SyncPage<T>> getNext) { | ||
super(hasNext, items.orElse(new ArrayList<>()), getNext); | ||
} | ||
|
||
public Stream<T> streamItems() { | ||
return StreamSupport.stream(this.spliterator(), false); | ||
} | ||
|
||
@Override | ||
public Iterator<T> iterator() { | ||
return new Iterator<T>() { | ||
private Iterator<T> itemsIterator = getItems().iterator(); | ||
private SyncPage<T> currentPage = SyncPagingIterable.this; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (itemsIterator.hasNext()) { | ||
return true; | ||
} | ||
if (currentPage.hasNext()) { | ||
advancePage(); | ||
return itemsIterator.hasNext(); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public T next() { | ||
if (!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
return itemsIterator.next(); | ||
} | ||
|
||
private void advancePage() { | ||
currentPage = currentPage.nextPage(); | ||
itemsIterator = currentPage.getItems().iterator(); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/main/java/com/basis/theory/api/resources/detokenize/DetokenizeClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* This file was auto-generated by Fern from our API Definition. | ||
*/ | ||
package com.basis.theory.api.resources.detokenize; | ||
|
||
import com.basis.theory.api.core.BasisTheoryApiApiException; | ||
import com.basis.theory.api.core.BasisTheoryException; | ||
import com.basis.theory.api.core.ClientOptions; | ||
import com.basis.theory.api.core.MediaTypes; | ||
import com.basis.theory.api.core.ObjectMappers; | ||
import com.basis.theory.api.core.RequestOptions; | ||
import com.basis.theory.api.errors.BadRequestError; | ||
import com.basis.theory.api.errors.ConflictError; | ||
import com.basis.theory.api.errors.ForbiddenError; | ||
import com.basis.theory.api.errors.UnauthorizedError; | ||
import com.basis.theory.api.types.ProblemDetails; | ||
import com.basis.theory.api.types.ValidationProblemDetails; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import java.io.IOException; | ||
import okhttp3.Headers; | ||
import okhttp3.HttpUrl; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
import okhttp3.Response; | ||
import okhttp3.ResponseBody; | ||
|
||
public class DetokenizeClient { | ||
protected final ClientOptions clientOptions; | ||
|
||
public DetokenizeClient(ClientOptions clientOptions) { | ||
this.clientOptions = clientOptions; | ||
} | ||
|
||
public void detokenize(Object request) { | ||
detokenize(request, null); | ||
} | ||
|
||
public void detokenize(Object request, RequestOptions requestOptions) { | ||
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) | ||
.newBuilder() | ||
.addPathSegments("detokenize") | ||
.build(); | ||
RequestBody body; | ||
try { | ||
body = RequestBody.create( | ||
ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); | ||
} catch (JsonProcessingException e) { | ||
throw new BasisTheoryException("Failed to serialize request", e); | ||
} | ||
Request okhttpRequest = new Request.Builder() | ||
.url(httpUrl) | ||
.method("POST", body) | ||
.headers(Headers.of(clientOptions.headers(requestOptions))) | ||
.addHeader("Content-Type", "application/json") | ||
.build(); | ||
OkHttpClient client = clientOptions.httpClient(); | ||
if (requestOptions != null && requestOptions.getTimeout().isPresent()) { | ||
client = clientOptions.httpClientWithTimeout(requestOptions); | ||
} | ||
try (Response response = client.newCall(okhttpRequest).execute()) { | ||
ResponseBody responseBody = response.body(); | ||
if (response.isSuccessful()) { | ||
return; | ||
} | ||
String responseBodyString = responseBody != null ? responseBody.string() : "{}"; | ||
try { | ||
switch (response.code()) { | ||
case 400: | ||
throw new BadRequestError(ObjectMappers.JSON_MAPPER.readValue( | ||
responseBodyString, ValidationProblemDetails.class)); | ||
case 401: | ||
throw new UnauthorizedError( | ||
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ProblemDetails.class)); | ||
case 403: | ||
throw new ForbiddenError( | ||
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ProblemDetails.class)); | ||
case 409: | ||
throw new ConflictError( | ||
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ProblemDetails.class)); | ||
} | ||
} catch (JsonProcessingException ignored) { | ||
// unable to map error response, throwing generic error | ||
} | ||
throw new BasisTheoryApiApiException( | ||
"Error with status code " + response.code(), | ||
response.code(), | ||
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); | ||
} catch (IOException e) { | ||
throw new BasisTheoryException("Network error executing HTTP request", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.