File: _stripe_context.py

package info (click to toggle)
python-stripe 13.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,476 kB
  • sloc: python: 187,843; makefile: 13; sh: 9
file content (40 lines) | stat: -rw-r--r-- 1,368 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from typing import List, Optional


class StripeContext:
    """
    The StripeContext class provides an immutable container and convenience methods for interacting with the `Stripe-Context` header. All methods return a new instance of StripeContext.

    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.
    """

    def __init__(self, segments: Optional[List[str]] = None):
        self._segments = segments or []

    def push(self, segment: str) -> "StripeContext":
        return StripeContext(self._segments + [segment])

    def pop(self) -> "StripeContext":
        if not self._segments:
            raise ValueError("Cannot pop from an empty StripeContext")

        return StripeContext(self._segments[:-1])

    def __str__(self) -> str:
        return "/".join(self._segments)

    def __repr__(self) -> str:
        return f"StripeContext({self._segments!r})"

    def __eq__(self, other) -> bool:
        if not isinstance(other, StripeContext):
            return False
        return self._segments == other._segments

    @staticmethod
    def parse(context_str: str) -> "StripeContext":
        if not context_str:
            return StripeContext()

        segments = context_str.split("/")
        return StripeContext(segments)