-
Notifications
You must be signed in to change notification settings - Fork 390
Add StripeContext object
#2057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add StripeContext object
#2057
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4692cc6
add stripe context
xavdid-stripe 890c651
clean up context creation
xavdid-stripe 9040fd9
remove comment
xavdid-stripe 9bf5980
make context public
xavdid-stripe 6632b91
pr feedback
xavdid-stripe 0562b27
Merge branch 'master' into DEVSDK-2767-2
xavdid-stripe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,98 @@ | ||
| package com.stripe; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import lombok.EqualsAndHashCode; | ||
|
|
||
| /** | ||
| * The StripeContext class provides an immutable container for interacting with the `Stripe-Context` | ||
| * header. | ||
| * | ||
| * <p>You can use it whenever you're initializing a `StripeClient` or sending `stripe_context` with | ||
| * a request. It's also found in the `EventNotification.context` property. | ||
| */ | ||
| @EqualsAndHashCode | ||
| public final class StripeContext { | ||
| private final List<String> segments; | ||
|
|
||
| /** Creates a new StripeContext with no segments. */ | ||
| public StripeContext() { | ||
| this(null); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new StripeContext with the specified segments. | ||
| * | ||
| * @param segments the list of context segments | ||
| */ | ||
| public StripeContext(List<String> segments) { | ||
| this.segments = | ||
| segments == null | ||
| ? Collections.emptyList() | ||
| : Collections.unmodifiableList(new ArrayList<>(segments)); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a new StripeContext with the given segment added to the end. | ||
| * | ||
| * @param segment the segment to add | ||
| * @return a new StripeContext instance with the segment appended | ||
| */ | ||
| public StripeContext push(String segment) { | ||
| List<String> newSegments = new ArrayList<>(this.segments); | ||
| newSegments.add(segment); | ||
| return new StripeContext(newSegments); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a new StripeContext with the last segment removed. | ||
| * | ||
| * @return a new StripeContext instance with the last segment removed | ||
| */ | ||
| public StripeContext pop() { | ||
| if (segments.isEmpty()) { | ||
| throw new IllegalStateException("Cannot pop from an empty StripeContext"); | ||
| } | ||
|
|
||
| List<String> newSegments = new ArrayList<>(this.segments); | ||
| newSegments.remove(newSegments.size() - 1); | ||
| return new StripeContext(newSegments); | ||
| } | ||
|
|
||
| /** | ||
| * Converts the context to a string by joining segments with '/'. | ||
| * | ||
| * @return string representation of the context segments joined by '/', `null` if there are no | ||
| * segments (useful for clearing context) | ||
| */ | ||
| @Override | ||
| public String toString() { | ||
| return String.join("/", segments); | ||
| } | ||
|
|
||
| /** | ||
| * Parse a context string into a StripeContext instance. | ||
| * | ||
| * @param contextStr string to parse (segments separated by '/') | ||
| * @return StripeContext instance with segments from the string | ||
| */ | ||
| public static StripeContext parse(String contextStr) { | ||
| if (contextStr == null || contextStr.isEmpty()) { | ||
| return new StripeContext(); | ||
| } | ||
|
|
||
| List<String> segments = Arrays.asList(contextStr.split("/")); | ||
| return new StripeContext(segments); | ||
| } | ||
|
|
||
| /** | ||
| * Returns an unmodifiable list of the current segments. | ||
| * | ||
| * @return the list of segments | ||
| */ | ||
| public List<String> getSegments() { | ||
| return segments; | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/stripe/model/StripeContextDeserializer.java
This file contains hidden or 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,26 @@ | ||
| package com.stripe.model; | ||
|
|
||
| import com.google.gson.JsonDeserializationContext; | ||
| import com.google.gson.JsonDeserializer; | ||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonParseException; | ||
| import com.stripe.StripeContext; | ||
| import java.lang.reflect.Type; | ||
|
|
||
| public class StripeContextDeserializer implements JsonDeserializer<StripeContext> { | ||
|
|
||
| @Override | ||
| public StripeContext deserialize( | ||
| JsonElement json, Type typeOfT, JsonDeserializationContext context) | ||
| throws JsonParseException { | ||
| if (json == null || json.isJsonNull()) { | ||
| return null; | ||
| } | ||
|
|
||
| String contextString = json.getAsString().trim(); | ||
| if (contextString.isEmpty()) { | ||
| return null; | ||
| } | ||
| return StripeContext.parse(contextString); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.