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'}
进一步阅读
要编辑此描述,您可以编辑 GitHub 上的文件。您可以使用 此编辑器。
项目详情
关闭
schul_cloud_resources_api_v1-1.0.0.326.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 21886c20a89a42bd9d91828ae147760f01ec2b3bcf9f5aaf574b2d614b9a829e |
|
MD5 | dc9a0766068c17fb84f12cd710797948 |
|
BLAKE2b-256 | 5c501e13576662e5e1993f5497d67835b86b36f1b5b645af010bb53f16925e9a |
关闭
schul_cloud_resources_api_v1-1.0.0.326-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 6c4e074d1a06b4b6ae1f2012f0478ee0594bb99418050789783503b7fabb0390 |
|
MD5 | 775e5c620232bc4c5bd51a81b84eb6d6 |
|
BLAKE2b-256 | 48e62f06314f81fd2990bdd7ec347361b5841c91f969f8610ffb60f1c090d757 |