跳转到主要内容

Schul-Cloud 内容API

项目描述

资源API包允许轻松访问Schul-Cloud资源服务器。要了解API的定义概述,请参阅仓库

安装

您可以从PyPI使用pip安装此包

pip install schul_cloud_resources_api_v1

访问API

假设,服务器在http://localhost:8080/v1下运行。您可以使用API来连接到它。如果您没有服务器,您可以从schul_cloud_resources_server_tests包中获取测试服务器。

# import the api classes for access
from schul_cloud_resources_api_v1 import ApiClient, ResourceApi

# create the client objects
url = "http://localhost:8080/v1"
client = ApiClient(url)
api = ResourceApi(client)

api对象提供了对服务器的访问。在这里,您可以了解如何访问API

# import the resource examples
from schul_cloud_resources_api_v1.schema import get_valid_examples

# get a valid resource
resource = get_valid_examples()[0]
print(resource)

# add the resource to the server
response = api.add_resource({"data": {"type": "resource", "attributes": resource}})

# verify the resource is on the server
all_my_resssources_on_the_server = [
    _id.id for _id in api.get_resource_ids().data]
assert response.data.id in all_my_resssources_on_the_server

# get the resource from the server
resource_copy = api.get_resource(response.data.id)
assert resource_copy == resource.data.attributes

# delete the resource
api.delete_resource(response.data.id)

身份验证

身份验证有这些选项

  • 无身份验证:这是默认设置,不需要执行任何操作。

  • 基本身份验证:使用用户名和密码进行身份验证

  • API密钥身份验证:提供一个密钥来授权请求。

身份验证是全局状态。所有ApiClients都使用此全局状态。因此,您一次只能对一个API进行身份验证。

import schul_cloud_resources_api_v1.auth as auth

您可以移除所有身份验证。这是默认情况。

auth.none()

您可以使用用户名和密码进行身份验证。这是基本身份验证

auth.basic("username", "password")

您可以使用API密钥进行身份验证。

auth.api_key("your-api-key")

验证资源

当您使用资源时,您可能想验证它们是否具有正确的格式。格式在resource-schema中指定。此模式包含在此包中。

from schul_cloud_resources_api_v1.schema import (
    get_valid_examples, get_invalid_examples, validate_resource, is_valid_resource
)

您可以通过调用is_valid_resource来测试资源是否有效

valid_resource = get_valid_examples()[0]
assert is_valid_resource(valid_resource)

invalid_resource = get_invalid_examples()[0]
assert not is_valid_resource(invalid_resource)

如果您想了解更多关于资源为什么无效的原因,您可以使用validate_resource

validate_resource({'title': 'hello'})

在这个例子中,出现了错误,即没有找到但必须存在的 url 属性。

jsonschema.exceptions.ValidationError: 'url' is a required property

Failed validating 'required' in schema:
    {'properties': {'contentCategory': {'$ref': '#/definitions/ContentCategory'},
                    'contextUrl': {'$ref': '#/definitions/URL'},
                    'curricula': {'items': {'$ref': '../curriculum/curriculum.json'},
                                  'type': 'array'},
                    'dimensions': {'$ref': '#/definitions/Dimensions'},
                    'duration': {'type': 'number'},
                    'languages': {'description': 'As described in IEEE '
                                                 'LOM, Section 1.3 '
                                                 'http://129.115.100.158/txlor/docs/IEEE_LOM_1484_12_1_v1_Final_Draft.pdf',
                                  'items': {'$ref': '#/definitions/Language'},
                                  'type': 'array'},
                    'licenses': {'items': {'$ref': '../license/license.json'},
                                 'type': 'array'},
                    'mimeType': {'description': 'https://tools.ietf.org/html/rfc2046',
                                 'example': 'text/html',
                                 'type': 'string'},
                    'size': {'format': 'int64', 'type': 'integer'},
                    'thumbnail': {'$ref': '#/definitions/URL'},
                    'title': {'description': 'The title of the resource.',
                              'example': 'Schul-Cloud',
                              'type': 'string'},
                    'url': {'$ref': '#/definitions/URL'}},
     'required': ['title',
                  'url',
                  'licenses',
                  'mimeType',
                  'contentCategory',
                  'languages'],
     'type': 'object'}

On instance:
    {'title': 'hello'}

进一步阅读

项目详情


发布历史 发布通知 | RSS 源

下载文件

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

源分布

schul_cloud_resources_api_v1-1.0.0.326.tar.gz (34.3 kB 查看哈希值

上传时间

构建分布

schul_cloud_resources_api_v1-1.0.0.326-py3-none-any.whl (84.6 kB 查看哈希值

上传时间 Python 3

支持者