Skip to content

Commit

Permalink
remove old pagination, add support for Python 3.12 (#81)
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Vieira <[email protected]>
  • Loading branch information
Bruno Vieira authored Oct 24, 2023
1 parent d50d33a commit 026e04c
Show file tree
Hide file tree
Showing 41 changed files with 283 additions and 685 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.9"
python-version: "3.11"
- name: Cache pip
uses: actions/cache@v2
with:
Expand All @@ -31,7 +31,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-versions: ["3.7", "3.8", "3.9", "3.10"]
python-versions: ["3.8", "3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-versions }}
Expand Down
30 changes: 30 additions & 0 deletions 4.0-Upgrade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Upgrading to chartmogul-python 4.0.0

This new version replaces the existing pagination for the `.all()` endpoints that used a combination of `page` and `per_page` parameters, and instead uses a `cursor` based pagination. So to list (as an example) Plans you now can:

```python
config = Config("your-token")

# Getting the first page
plans = Plan.all(config, per_page=12)

"""
This will return an array of plans (if available), and a cursor + has_more fields
"""
{
"plans": [
{
"uuid": "some_uuid",
"data_source_uuid": "some_uuid",
"name": "Master Plan"
}
],
"has_more": True,
"cursor": "MjAyMy0wNy0yOFQwODowOToyMi4xNTQyMDMwMDBaJjk0NDQ0Mg=="
}

if plans.has_more:
more_plans = Plan.all(config, per_page=12, cursor=plans.cursor)
```

If you have existing code that relies on the `page` parameter, those requests will throw an error now alerting you of their deprecation.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ and this project adheres to [Semantic Versioning].
[Keep a Changelog]: https://keepachangelog.com/en/1.0.0/
[Semantic Versioning]: https://semver.org/spec/v2.0.0.html

## [4.0.0] - 2023-10-04

### Added
- v4.0.0 upgrade instructions.
- Support for Python 3.12.

### Removed
- Support for old pagination using `page` query params.
- Deprecated `imp` module.
- Support for Python 3.7.

## [3.1.3] - 2023-09-27

### Added
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

## Installation

This library requires Python 3.5 to 3.9. It was last tested against Python 2.7 in version 1.3.0.
This library requires Python 3.8 to 3.12. It was last tested against Python 2.7 in version 1.3.0.

```sh
pip3 install chartmogul
Expand Down Expand Up @@ -376,6 +376,8 @@ To work on the library:

* Fork it
* Create your feature branch (`git checkout -b my-new-feature`)
* Setup a virtual environment (`python -m venv <directory>`)
* Activate the virtual environment (`source <directory>/bin/activate`)
* Install dependencies: `pip3 install -r requirements.txt && python3 setup.py develop`
* Fix bugs or add features. Make sure the changes pass the coding guidelines (use `pylama`).
* Write tests for your new features. Use `requests_mock` for HTTP mocking.
Expand Down
10 changes: 6 additions & 4 deletions chartmogul/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# -*- coding: utf-8 -*-
from .api.config import Config
from .errors import APIError, ConfigurationError, ArgumentMissingError
from .errors import (
APIError,
ConfigurationError,
ArgumentMissingError,
DeprecatedArgumentError,
)

from .api.customers.activity import CustomerActivity
from .api.attributes import Attributes
Expand All @@ -24,9 +29,6 @@

from .version import __version__

# Deprecated
import imp


"""
ChartMogul API Python Client
Expand Down
6 changes: 1 addition & 5 deletions chartmogul/api/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ class Customer(Resource):

_path = "/customers{/uuid}"
_root_key = "entries"
_many = namedtuple(
"Customers",
[_root_key, "has_more", "per_page", "page", "current_page", "total_pages", "cursor"],
defaults=[None, None, None, None, None, None],
)
_many = namedtuple("Customers", [_root_key, "has_more", "cursor"], defaults=[None, None])

class _Schema(Schema):
# All operations
Expand Down
6 changes: 1 addition & 5 deletions chartmogul/api/customers/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ class CustomerActivity(Resource):

_path = "/customers{/uuid}/activities"
_root_key = "entries"
_many = namedtuple(
"Activities",
[_root_key, "has_more", "per_page", "page", "cursor"],
defaults=[None, None, None, None],
)
_many = namedtuple("Activities", [_root_key, "has_more", "cursor"], defaults=[None, None])

class _Schema(Schema):
id = fields.Int()
Expand Down
17 changes: 2 additions & 15 deletions chartmogul/api/customers/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ class CustomerSubscription(Resource):

_path = "/customers{/uuid}/subscriptions"
_root_key = "entries"
_many = namedtuple(
"Subscriptions",
[_root_key, "has_more", "per_page", "page", "cursor"],
defaults=[None, None, None, None],
)
_many = namedtuple("Subscriptions", [_root_key, "has_more", "cursor"], defaults=[None, None])

class _Schema(Schema):
id = fields.Int(allow_none=True)
Expand Down Expand Up @@ -52,19 +48,10 @@ def _loadJSON(cls, jsonObj):
if "subscriptions" in jsonObj:
_many = namedtuple(
"Subscriptions",
[
"subscriptions",
"current_page",
"total_pages",
"customer_uuid",
"has_more",
"cursor",
],
["subscriptions", "has_more", "cursor", "customer_uuid"],
)
return _many(
cls._schema.load(jsonObj["subscriptions"], many=True),
current_page=jsonObj.get("current_page", None),
total_pages=jsonObj.get("total_pages", None),
customer_uuid=jsonObj.get("customer_uuid", None),
has_more=jsonObj.get("has_more", None),
cursor=jsonObj.get("cursor", None),
Expand Down
8 changes: 5 additions & 3 deletions chartmogul/api/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class Invoice(Resource):
_root_key = "invoices"
_many = namedtuple(
"Invoices",
[_root_key, "current_page", "total_pages", "cursor", "has_more", "customer_uuid"],
defaults=[None, None, None, None, None],
[_root_key, "cursor", "has_more", "customer_uuid"],
defaults=[None, None, None],
)
_many.__new__.__defaults__ = (None,) * len(_many._fields)

Expand Down Expand Up @@ -82,6 +82,8 @@ def all(cls, config, **kwargs):
Invoice.all_any = Invoice._method("all", "get", "/invoices")
Invoice.destroy = Invoice._method("destroy", "delete", "/invoices{/uuid}")
Invoice.destroy_all = Invoice._method(
"destroy_all", "delete", "/data_sources{/data_source_uuid}/customers{/customer_uuid}/invoices"
"destroy_all",
"delete",
"/data_sources{/data_source_uuid}/customers{/customer_uuid}/invoices",
)
Invoice.retrieve = Invoice._method("retrieve", "get", "/invoices{/uuid}")
6 changes: 1 addition & 5 deletions chartmogul/api/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ class Plan(Resource):

_path = "/plans{/uuid}"
_root_key = "plans"
_many = namedtuple(
"Plans",
[_root_key, "current_page", "total_pages", "has_more", "cursor"],
defaults=[None, None, None, None],
)
_many = namedtuple("Plans", [_root_key, "has_more", "cursor"], defaults=[None, None])

class _Schema(Schema):
uuid = fields.String()
Expand Down
6 changes: 1 addition & 5 deletions chartmogul/api/plan_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ class PlanGroup(Resource):

_path = "/plan_groups{/uuid}"
_root_key = "plan_groups"
_many = namedtuple(
"PlanGroups",
[_root_key, "current_page", "total_pages", "has_more", "cursor"],
defaults=[None, None, None, None],
)
_many = namedtuple("PlanGroups", [_root_key, "has_more", "cursor"], defaults=[None, None])

class _Schema(Schema):
uuid = fields.String()
Expand Down
6 changes: 1 addition & 5 deletions chartmogul/api/plan_group_plans.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ class PlanGroupPlans(Resource):

_path = "/plan_groups{/uuid}/plans"
_root_key = "plans"
_many = namedtuple(
"PlanGroupPlans",
[_root_key, "current_page", "total_pages", "has_more", "cursor"],
defaults=[None, None, None, None],
)
_many = namedtuple("PlanGroupPlans", [_root_key, "has_more", "cursor"], defaults=[None, None])

class _Schema(Schema):
uuid = fields.String()
Expand Down
2 changes: 1 addition & 1 deletion chartmogul/api/subscription_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class SubscriptionEvent(Resource):
_path = "/subscription_events"
_root_key = "subscription_events"
_many = namedtuple(
"SubscriptionEvents", [_root_key, "meta", "has_more", "cursor"], defaults=[None, None, None]
"SubscriptionEvents", [_root_key, "has_more", "cursor"], defaults=[None, None]
)

class _Schema(Schema):
Expand Down
4 changes: 4 additions & 0 deletions chartmogul/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class ArgumentMissingError(Exception):
pass


class DeprecatedArgumentError(Exception):
pass


def annotateHTTPError(err):
if isinstance(err, HTTPError):
raise_from(APIError(err.response.content), err)
Expand Down
4 changes: 0 additions & 4 deletions chartmogul/imp/__init__.py

This file was deleted.

14 changes: 0 additions & 14 deletions chartmogul/imp/invoice.py

This file was deleted.

16 changes: 0 additions & 16 deletions chartmogul/imp/subscription.py

This file was deleted.

9 changes: 0 additions & 9 deletions chartmogul/imp/transaction.py

This file was deleted.

22 changes: 9 additions & 13 deletions chartmogul/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
from uritemplate import URITemplate

from .api.config import Config
from .errors import ArgumentMissingError, ConfigurationError, annotateHTTPError
from .errors import (
ArgumentMissingError,
ConfigurationError,
annotateHTTPError,
DeprecatedArgumentError,
)
from .retry_request import requests_retry_session

from .version import __version__
Expand All @@ -29,18 +34,7 @@
"modify_with_params": "patch",
}

LIST_PARAMS = [
"current_page",
"total_pages",
"has_more",
"per_page",
"page",
"summary",
"customer_uuid",
"data_source_uuid",
"cursor",
"meta",
]
LIST_PARAMS = ["has_more", "summary", "customer_uuid", "data_source_uuid", "cursor"]
ESCAPED_QUERY_KEYS = {"start_date": "start-date", "end_date": "end-date"}


Expand Down Expand Up @@ -175,6 +169,8 @@ def _validate_arguments(cls, method, kwargs):
raise ArgumentMissingError("Please pass 'uuid' parameter")
if method in ["create", "modify"] and "data" not in kwargs:
raise ArgumentMissingError("Please pass 'data' parameter")
if method == "all" and "page" in kwargs:
raise DeprecatedArgumentError("The 'page' parameter is deprecated")
if (
method in ["destroy_all"]
and "data_source_uuid" not in kwargs
Expand Down
2 changes: 1 addition & 1 deletion chartmogul/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "3.1.3"
__version__ = "4.0.0"
10 changes: 0 additions & 10 deletions docs/chartmogul.imp.rst

This file was deleted.

1 change: 0 additions & 1 deletion docs/chartmogul.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Subpackages

chartmogul.api
chartmogul.enrichment
chartmogul.imp
chartmogul.metrics

Submodules
Expand Down
30 changes: 0 additions & 30 deletions docs/test.imp.rst

This file was deleted.

Loading

0 comments on commit 026e04c

Please sign in to comment.