Skip to content

Failure in creating entitlements via sdk #91

@kurayami07734

Description

@kurayami07734

Description of the Bug

Creation of entitlements is failing due incorrect response model from the create_entitlements route.

I get the following error
(CreateResponse.__init__() missing 4 required positional arguments: 'http_status_code', 'headers', 'is_idempotency_replayed', and 'entitlement')

Steps to reproduce

Minimal reproduction

Run the following script

Prerequisites

  1. install chargebee and requests packages
  2. Add your credentials to the script
from datetime import datetime
import logging

import chargebee
import requests
from requests.auth import HTTPBasicAuth

# Required packages
#  chargebee==3.11.0
#  requests==2.32.5

current_timestamp = datetime.now().timestamp()

CHARGEBEE_API_KEY = "your_api_key"
CHARGEBEE_SITE = "your_test_site"


def create_chargebee_client() -> chargebee.Chargebee:
    return chargebee.Chargebee(api_key=CHARGEBEE_API_KEY, site=CHARGEBEE_SITE)


def setup_steps(cb_client: chargebee.Chargebee) -> tuple[str, str]:
    logging.info("Creating item family, item and feature...")

    item_family_create_params = chargebee.ItemFamily.CreateParams(
        id=f"item-family-{current_timestamp}",
        name=f"Item Family {current_timestamp}",
    )
    item_family_create_response = cb_client.ItemFamily.create(item_family_create_params)
    logging.info("Created Item family!")

    item_create_params = chargebee.Item.CreateParams(
        id=f"item-{current_timestamp}",
        name=f"Item {current_timestamp}",
        item_family_id=item_family_create_response.item_family.id,
        type=chargebee.ItemType.PLAN,
    )
    item_create_response = cb_client.Item.create(item_create_params)
    logging.info("Created Item!")

    feature_create_params = chargebee.Feature.CreateParams(
        id=f"feature-{current_timestamp}",
        name=f"Feature {current_timestamp}",
        type=chargebee.Feature.Type.RANGE,
        levels=[
            chargebee.Feature.CreateLevelParams(level=0, value=0),
            chargebee.Feature.CreateLevelParams(level=1, value=100),
        ],
    )
    feature_create_response = cb_client.Feature.create(feature_create_params)
    logging.info("Created Feature!")

    return (
        item_create_response.item.id,
        feature_create_response.feature.id,
    )


def create_entitlement_via_sdk(
    cb_client: chargebee.Chargebee, item_id: str, feature_id: str
):
    logging.info("Creating entitlement via sdk!")
    response = cb_client.Entitlement.create(
        cb_client.Entitlement.CreateParams(
            action=chargebee.Action.UPSERT,
            entitlements=[
                cb_client.Entitlement.CreateEntitlementParams(
                    value=50,
                    feature_id=feature_id,
                    entity_id=item_id,
                    entity_type=chargebee.EntityType.PLAN,
                )
            ],
        )
    )
    logging.info(f"{response=}")


def create_entitlement_via_api(item_id: str, feature_id: str):
    logging.info("Creating entitlement via api call!")
    url = f"https://{CHARGEBEE_SITE}.chargebee.com/api/v2/entitlements"
    basic_auth = HTTPBasicAuth(username=CHARGEBEE_API_KEY, password="")
    body = {
        "action": chargebee.Action.UPSERT.value,
        "entitlements[feature_id][0]": feature_id,
        "entitlements[value][0]": 50,
        "entitlements[entity_id][0]": item_id,
        "entitlements[entity_type][0]": chargebee.EntityType.PLAN.value,
    }

    response = requests.post(url, auth=basic_auth, data=body)
    data = response.json()

    logging.info(f"Response: {data=}")


def main():
    cb_client = create_chargebee_client()

    item_id, feature_id = setup_steps(cb_client)

    try:
        create_entitlement_via_sdk(cb_client, item_id, feature_id)
    except Exception:
        logging.exception("Error in creating via sdk")

    try:
        create_entitlement_via_api(item_id, feature_id)
    except Exception:
        logging.exception("Error in creating via api")


if __name__ == "__main__":
    main()

Expected Behavior

Entitlement creation via the sdk should not fail.

Code Snippets (if applicable)

Operating System

Windows 11

Language version

Python 3.12.4

Library version

v3.11.0

Additional context

I have provisionally found a solution, will raise a PR to fix this issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions