跳转到主要内容

用于与苹果App Store Connect API协同工作的Python SDK。

项目描述

Applaud

Applaud 是一个用于访问 App Store Connect API 的 Python 客户端库,由 Applaudgen 生成。

PyPI version

功能

  • 支持App Store Connect API最新版本1.6
  • 支持 filter, fields, include, limit, sort, exists 等查询参数
  • 实现了所有端点/路径,包括但不限于:应用信息、TestFlight、用户和角色、销售和财务
  • Pythonic,所有 camelCase 架构字段都表示为 snake_case 类属性
  • 支持 Python类型提示
  • 使用 Python Requests 处理HTTP会话
  • ErrorResponse 可以作为异常捕获

安装

使用 pip 安装

pip install applaud

使用 Poetry 安装

poetry add applaud

使用 Pipenv 安装

pipenv install applaud

用法

调用API需要授权,因此在我们开始之前,您需要从组织的App Store Connect账户中获取密钥以创建令牌。请参阅为App Store Connect API创建API密钥以创建您的密钥和令牌。

连接

ConnectionApplaud 的核心类,它维护客户端和远程服务之间的连接,在过期之前生成新的令牌

from applaud.connection import Connection

# Create a connection object using API keys
connection = Connection(APPSTORE_ISSUER_ID, APPSTORE_KEY_ID, APPSTORE_PRIVATE_KEY)

在大多数情况下,您应该在 Connection 对象上启动所有希望在对远程服务执行的任务。 Connection 提供了许多函数,可以帮助您创建 …Endpoint 对象

# Return an AppListEndpoint object
connection.apps()

端点

…Endpoint 类封装了对特定资源可以执行的所有操作。例如,此代码片段获取前两个(按应用名称排序)已准备好销售且启用了游戏中心版本的“应用”

# Return an AppsResponse object
connection.apps().filter(
    app_store_versions_app_store_state=AppStoreVersionState.READY_FOR_SALE
).exists(
    game_center_enabled_versions=True
).limit(
    2
).sort(
    name: SortOrder.ASC
).get()

…Endpoint.get()

get() 操作在端点路径上发起 HTTP GET 请求。例如,列出应用 服务端点的 URL 是

GET https://api.appstoreconnect.apple.com/v1/apps

Applaud 中的对应代码

# Return an AppsResponse object
response = connection.apps().get()

for app in response.data:
    print(f'{app.attributes.name}: {app.attributes.bundle_id}')

与其它操作(如 create()update()delete())不同,get() 操作可以通过查询参数函数进行链式调用

filter()

您可以使用 filter() 函数提取匹配的资源。例如

filter[bundleId]  Attributes, relationships, and IDs by which to filter.
        [string]

Applaud 中的对应代码

response = connection.apps().filter(
    bundle_id="com.exmaple.app1"
).get()
# or
connection.apps().filter(
    bundle_id=["com.exmaple.app1", "com.exmaple.app2"]
).get()

for app in response.data:
    print(f'{app.attributes.name}: {app.attributes.bundle_id}')

include()

您可以使用 include() 函数请求包含在响应中的关系数据。例如

 include  Relationship data to include in the response.
[string]  Possible values: appClips, appInfos, appStoreVersions,
          availableTerritories, betaAppLocalizations,
          betaAppReviewDetail, betaGroups, betaLicenseAgreement,
          builds, ciProduct, endUserLicenseAgreement,
          gameCenterEnabledVersions, inAppPurchases, preOrder,
          preReleaseVersions, prices

Applaud 中的对应代码

response = connection.apps().include(
    AppListEndpoint.Include.BETA_LICENSE_AGREEMENT
).get()
# or
response = connection.apps().include(
    [AppListEndpoint.Include.BETA_LICENSE_AGREEMENT, AppListEndpoint.Include.PRICES]
).get()

fields()

您可以使用 fields() 函数请求在 get() 操作中返回的包含相关资源的字段数据。在 fields() 函数中指定的相关资源必须在 include() 函数中显式包含,否则,远程服务可能不会返回您预期的字段数据。例如

fields[betaLicenseAgreements]  Fields to return for included related types.
                     [string]  Possible values: agreementText, app

Applaud 中的对应代码

connection.apps().include(
    AppListEndpoint.Include.BETA_LICENSE_AGREEMENT
).fields(
    beta_license_agreement=[BetaLicenseAgreementField.AGREEMENT_TEXT, BetaLicenseAgreementField.APP]
).get()

limit()

您可以使用 limit() 函数限制在 get() 操作中返回的最大资源数量。例如

  limit  Number of resources to return.
integer  Maximum Value: 200

Applaud 中的对应代码

# Return a response contains 10 apps at most
connection.apps().limit(10).get()

# Raise a ValueError exception, the maxinmu allowed value is 200
connection.apps().limit(400).get()

您还可以限制返回的相关资源数量,就像在 fields() 函数中一样,您必须在 include() 函数中显式指定相关资源。例如

limit[appStoreVersions]  integer
                         Maximum Value: 50

Applaud 中的对应代码

# All returned apps have 5 related app store version at most
connection.apps().include(
    AppListEndpoint.Include.APP_STORE_VERSIONS
).limit(app_store_versions=5).get()

# Raise a ValueError exception, the maxinmu allowed value is 50
connection.apps().include(
    AppListEndpoint.Include.APP_STORE_VERSIONS
).limit(app_store_versions=100).get()

通过利用 limit() 函数与 sort() 函数结合,您的脚本可以更加响应。

sort()

您可以使用 sort() 函数按属性升序或降序对返回的资源进行排序。例如

    sort  Attributes by which to sort.
[string]  Possible values: bundleId, -bundleId, name, -name, sku, -sku

Applaud 中的对应代码

connection.apps().sort(name=SortOrder.ASC, bundleId=SortOrder.DESC).get()

exists()

exists() 是一种特殊的过滤类型——根据相关资源是否存在进行过滤。例如

exists[gameCenterEnabledVersions]  [string]

Applaud 中的对应代码

connection.apps().exists(game_center_enabled_versions=True).get()

…Endpoint.create()

create() 操作在端点路径上发起 HTTP POST 请求。例如,创建 App Store 版本 服务端点的 URL 是

POST https://api.appstoreconnect.apple.com/v1/appStoreVersions

Applaud 中的对应代码

request = AppStoreVersionCreateRequest(
            data = AppStoreVersionCreateRequest.Data(
                relationships = AppStoreVersionCreateRequest.Data.Relationships(
                    app = AppStoreVersionCreateRequest.Data.Relationships.App(
                        data = AppStoreVersionCreateRequest.Data.Relationships.App.Data(
                            id = 'com.exmaple.app1'
                        )
                    )
                ),
                attributes = AppStoreVersionCreateRequest.Data.Attributes(
                    version_string = '1.6',
                    platform = Platform.IOS,
                    copyright = f'Copyright © 2021 Codinn Technologies. All rights reserved.',
                    release_type = AppStoreVersionReleaseType.AFTER_APPROVAL
                )
            )
        )

# Return an AppStoreVersionResponse object
reponse = connection.app_store_versions().create(request)

version = response.data
print(f'{version.version_string}: {version.created_date}, {version.app_store_state}')

…Endpoint.update()

update() 操作在端点路径上发起 HTTP PATCH 请求。例如,修改 App Store 版本 服务端点的 URL 是

PATCH https://api.appstoreconnect.apple.com/v1/appStoreVersions/{id}

Applaud 中的对应代码

# Get the version id created in previous example
version_id = version.data.id

# Update version's information
request = AppStoreVersionUpdateRequest(
            data = AppStoreVersionUpdateRequest.Data(
                id = version.data.id,

                attributes = AppStoreVersionUpdateRequest.Data.Attributes(
                    version_string = '1.6.1',
                    platform = Platform.IOS,
                    copyright = f'Copyright © 2022 Codinn Technologies. All rights reserved.',
                    release_type = AppStoreVersionReleaseType.AFTER_APPROVAL
                )
            )
        )

# Return an AppStoreVersionResponse object
reponse = connection.app_store_version(version_id).update(request)

version = response.data
print(f'{version.version_string}: {version.copyright}, {version.app_store_state}')

…Endpoint.delete()

delete() 操作在端点路径上发起 HTTP DELETE 请求。例如,删除 App Store 版本 服务端点的 URL 是

DELETE https://api.appstoreconnect.apple.com/v1/appStoreVersions/{id}

Applaud 中的对应代码

# Get the version id created in previous example
version_id = version.data.id

connection.app_store_version(version_id).delete()

异常

…Endpoint.get()…Endpoint.create()…Endpoint.update()…Endpoint.delete() 可能引发两种类型的异常

  1. Python Requests 引起的 HTTP 请求异常
  2. 远程服务返回 ErrorResponse

对于第二种情况,Applaud 引发一个 EndpointException 异常,并在 EndpointException.errors 属性中附加所有 ErrorResponse.Error 对象。

一些错误是无害的,例如,App Store Connect 没有API让您知道测试者是否已接受测试邀请。当您尝试重新发送测试邀请时,可能会遇到 ALREADY_ACCEPTED 错误,您可以安全地忽略此类错误

try:
    # Send / resend beta test invitations
    response = connection.beta_tester_invitations().create()
except EndpointException as err:
    already_accepted_error = False
    for e in err.errors:
        if e.code == 'STATE_ERROR.TESTER_INVITE.ALREADY_ACCEPTED':
            # silent this error
            already_accepted_error = True
            break

    if not already_accepted_error:
        raise err

注意事项

  • 查询参数函数(filter()includefields 等)仅在配合 …Endpoint.get() 使用时发挥作用。尽管与 …Endpoint.create()…Endpoint.update()…Endpoint.delete() 操作链式调用时没有副作用,但并不建议这样做。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

applaud-0.9.4.tar.gz (88.9 kB 查看哈希值)

上传时间 源代码

构建分布

applaud-0.9.4-py3-none-any.whl (147.2 kB 查看哈希值)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页