Skip to main content

提供与 ArchivesSpace API 交互时可以使用的方法和类。

项目描述

ArchivesSpace Python 客户端

PyPI 版本

aspace-clientPython 包提供了面向 ArchivesSpace v2.X 及更高版本 API 的 Web 客户端功能。该软件包旨在帮助正在进行和未来的 ArchivesSpace 迁移。

关于

aspace模块SessionrequestsPython 库扩展类的功能,并尝试提供对 ArchivesSpace 的所有 REST API 端点的访问,同时还保留向后兼容性和支持 Pylint 等开发工具。ArchivesSpace 目前(2019 年 5 月)拥有超过 250 个 API 端点。支持和维护所有这些端点是一项艰巨的任务,因此新功能和错误修复的优先级取决于它们对执行正在进行的 ArchivesSpace 迁移的价值。

安装

这个项目在 PyPI 上有一个列表!

pip install aspace-client

您也可以直接从 GitHub 存储库安装此项目:

pip install https://github.com/AtlasSystems/ArchivesSpace-Python-Client/zipball/master

开发者安装

以下是在“可编辑”模式下安装此软件包的说明。这将允许您对包进行更改并实时测试它们。

AS_CLIENT_DIR="/path/to/aspace-client"
git clone https://github.com/AtlasSystems/ArchivesSpace-Python-Client.git "$AS_CLIENT_DIR"

# In your python project directory, or in your venv
pip install -e "$AS_CLIENT_DIR"

用法

from aspace.client import ASpaceClient

client = ASpaceClient('http://localhost:8089', 'admin', 'admin')

这将初始化一个以 ArchivesSpace API 实例为目标的客户端。此初始化还将自动验证您的用户。如果您将此软件包用作较大脚本的一部分,则可以选择关闭此功能,其中与 ArchivesSpace 交互不是主要组件,并且您所处的环境不能 100% 确定 ArchivesSpace 是运行和访问。对于该操作:

from time import sleep
from aspace.client import ASpaceClient

client = ASpaceClient(
    base_url='http://localhost:8089', # Base url for connecting to your ASpace's API.
    username='admin',
    password='admin',
    auto_authenticate=False,
)

while client.get('/version').status_code != 200:
    print('ArchivesSpace API is not up')
    sleep(2)

client.authenticate()

还有对上述操作的内置支持,以及从 ConfigParser 实例中提取设置的内置功能。

在 settings.ini 中:

[aspace_credentials]
api_host = 'http://aspace.cloudapp.eastus.azure.com'
username = 'automation-user'
password = 'automation-user-password'

在 app.py 中:

import configparser
import aspace

config = configparser.ConfigParser()
config.load('settings.ini')

client = (
    aspace.client.ASpaceClient
    .init_from_config(config)
    .wait_until_ready(
        check_interval=2.0,
        max_wait_time=200.0,
        authenticate_on_success=True,
    )
)

失败的身份验证会引发错误,因此如果这些脚本中的任何一个仍在运行,您就可以查询 API!此包使用以下注意事项与 ArchivesSpace API 交互。

  1. requests我们都喜欢的 Python 库所描述的语法
  2. ArchivesSpace API 文档描述的 API 端点结构

Python 库的典型语法requests被保留,因此所有 HTTP 方法(POST、GET、DELETE 等)通常以相对于 API 的基本 URL 的 URI 或端点开始。从不假定 URI 确保所有操作都是可预测的,并且 API 的所有功能都被正确使用。

获取档案空间系统信息

# Get the system info
print(client.get('/').json())

管理存储库

# Get a listing of all repositories
repositories = client.get('/repositories').json()
for repository in repositories:
    print(repository)

# Create a new repository
new_repo = {}
new_repo['repo_code'] = 'test_repo'
new_repo['name'] = 'Test Repository'
response = client.post('/repositories', json=new_repo).json()

# Update the name of that repository
repo = client.get(response['uri']).json()
repo['name'] = 'Andy Samberg University Archives - Test Repository'
client.post(repo['uri'], json=repo)

# Delete the repository
client.delete(new_repo['uri'])

只要响应以 JSON 形式返回,此语法就可用于与 ArchivesSpace 的所有端点进行交互。大多数都这样做。目前还提供了一些对 ArchivesSpace 的 API 功能的扩展。

流媒体记录

# Cleanup your resource records one at a time, no matter how many you have
for resource in client.streams.resources():
    if resource['title'].endswith('!'):
        print('Cleaning Resource:', resource['uri'], resource['title'])
        resource['title'] = resource['title'].rstrip('!')
        update_result = client.post(resource['uri'], json=resource).json()
        print(update_result)

# Works for accessions and agents
client.streams.accessions()
client.streams.people()
client.streams.corporate_entities()
client.streams.families()
client.streams.software()
client.streams.all_agents()


# Works for endpoints that do not have an explicitly defined stream method
client.streams.records('container_profiles'):
    pass

# Works for endpoints that do not have an explicitly defined stream method
# and require a repository reference in the URI.
for assessment in client.streams.repository_relative_records('assessments'):
    pass

# Optional limits can be placed on record streams, so that only 1 repository
# is considered, as opposed to streaming all records from all repositories,
# which is default.
assessments_stream = client.streams.repository_relative_records(
    'assessments',
    repository_uris=['/repositories/2']
)

for assessment in assessments_stream:
    pass

贡献

如果您有任何建议或错误报告,请随时在 问题选项卡中报告 或发送电子邮件至devgineers@atlas-sys.com给我们。如果您是 Git 新手,但仍想做出贡献,请随时给我们发送电子邮件。

欢迎拉取请求,但它们将受到审查过程的约束。一致的代码风格是该项目的目标,因为它目前试图遵循 PEP8 Python 风格指南中列出的编码标准。请在提交或请求捐款时记住这一点,但也要记住 PEP 是一个灵活的标准,我们愿意例外。

下载文件

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

源分布

aspace-client-2.5.0.tar.gz (25.1 kB 查看哈希

已上传 source

内置分布

aspace_client-2.5.0-py3-none-any.whl (27.0 kB 查看哈希

已上传 py3