How to use the recurly.resource.Resource.cast_json function in recurly

To help you get started, we’ve selected a few recurly examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github recurly / recurly-client-python / recurly / resource.py View on Github external
def cast_json(cls, properties, class_name=None, response=None):
        """Casts a dict of properties into a Recurly Resource"""

        if class_name is None and "object" in properties:
            # If it's a Page, let's return that now
            if (
                properties["object"] == "list"
                and "data" in properties
                and "has_more" in properties
            ):
                properties["data"] = [Resource.cast_json(i) for i in properties["data"]]
                return Page(properties)

            # If it's not a Page, we need to derive the class name
            # from the "object" property. The class_name passed in should
            # take precedence.
            name_parts = properties["object"].split("_")
            class_name = "".join(x.title() for x in name_parts)

        klass = cls.locator(class_name)

        # Special case for Empty class
        if class_name == Empty:
            klass = Empty

        # If we can't find a resource class, we should return
        # the untyped properties dict. If in strict-mode, explode.
github recurly / recurly-client-python / recurly / resource.py View on Github external
# and skip the casting
                if v is None:
                    attr = None

                # if it's a plain type, use the type to cast it
                elif type(attr_type) == type:
                    attr = attr_type(v)

                # if it's a datetime, parse it
                elif attr_type == datetime:
                    attr = datetime.datetime.strptime(v, DT_FORMAT)

                # If the schema type a string, it's a reference
                # to another resource
                elif isinstance(attr_type, str) and isinstance(v, dict):
                    attr = Resource.cast_json(v, class_name=attr_type)

                # If the schema type is a list of strings, it's a reference
                # to a list of resources
                elif (
                    isinstance(attr_type, list)
                    and isinstance(attr_type[0], str)
                    and isinstance(v, list)
                ):
                    attr = [Resource.cast_json(r, class_name=attr_type[0]) for r in v]

            # We want to explode in strict mode because
            # the schema doesn't know about this attribute. In production
            # we will just set the attr to it's value or None
            if recurly.STRICT_MODE and attr_type is None:
                raise ValueError(
                    "%s could not find property %s in schema %s given value %s"
github recurly / recurly-client-python / recurly / resource.py View on Github external
elif attr_type == datetime:
                    attr = datetime.datetime.strptime(v, DT_FORMAT)

                # If the schema type a string, it's a reference
                # to another resource
                elif isinstance(attr_type, str) and isinstance(v, dict):
                    attr = Resource.cast_json(v, class_name=attr_type)

                # If the schema type is a list of strings, it's a reference
                # to a list of resources
                elif (
                    isinstance(attr_type, list)
                    and isinstance(attr_type[0], str)
                    and isinstance(v, list)
                ):
                    attr = [Resource.cast_json(r, class_name=attr_type[0]) for r in v]

            # We want to explode in strict mode because
            # the schema doesn't know about this attribute. In production
            # we will just set the attr to it's value or None
            if recurly.STRICT_MODE and attr_type is None:
                raise ValueError(
                    "%s could not find property %s in schema %s given value %s"
                    % (klass.__name__, k, klass.schema, v)
                )
            else:
                setattr(resource, k, attr)

        if response:
            # Maintain JSON parsed body for version < 4
            response.body = properties
            # TODO: Remove this (^^) for version 4