dagstuhl/swh-archive-client
Composer 安装命令:
composer require dagstuhl/swh-archive-client
包简介
SoftwareHeritage Web API Client for PHP
README 文档
README
dagstuhl/swh-archive-client
A SoftwareHeritage web API client in php
This project provides a php wrapper around the SoftwareHeritage web API. It is used by Dagstuhl Publishing to integrate automatic archiving of software projects by authors into its publication workflow.
At https://github.com/dagstuhl-publishing/swh-deposit-client, we also provide a php client for the SoftwareHeritage Deposit API.
Installation
composer require dagstuhl/swh-archive-client
The client is designed to work smoothly with the config mechanism of laravel. When used inside a laravel project,
you can configure it by creating a file swh.php inside the config folder with the following contents:
<?php return [ 'web-api' => [ 'token' => env('SWH_WEB_API_TOKEN'), 'url' => env('SWH_WEB_API_URL'), // optional: caching of get requests for objects in the archive 'cache-folder' => env('SWH_WEB_API_CACHE_FOLDER'), // return null to disable cache 'cache-ttl' => env('SWH_WEB_API_CACHE_TTL'), ] ];
Based on that configuration, a default client is implicitly initialized and used whenever you request a SwhObject.
In a non-laravel environment just implement a global config function config in such a way that
config('swh.web-api.token') is your token, config('swh.web-api.url') is the api url, and so on.
Code Examples
1) Browsing the archive
Searching through the archive is quite intuitive, as you don't realise that you are dealing with an API. You simply request the relevant objects, like this:
// start with a url $repo = Repository::fromNodeUrl('https://github.com/dagstuhl-publishing/styles'); // create the corresponding origin object $origin = Origin::fromRepository($repo); // ask the origin for the SoftwareHeritage visits $visits = $origin->getVisits(); // get the snapshot object from a specific visit $snapshot = $visits[0]->getSnapshot(); // get the list of branches from a snapshot $branches = $snapshot->getBranches();
Further supported objects are Revision, Release, Directory, Content.
To fetch an object by its id, just call the byId method:
$revision = Revision::byId('60476b518914683d35ef08dd6cfdc7809e280c75');
To identify a directory/file inside a snapshot, use the Context class. Continuing the example from above, we can do the following:
// take the last snapshot $snapshot = $visits[0]->getSnapshot(); // take a "path" to a file/directory inside the repo $repoNode = new RepositoryNode('https://github.com/dagstuhl-publishing/styles/blob/master/LIPIcs/authors/lipics-v2021.cls'); // identify this node inside the snapshot (i.e., get the context) $context = $snapshot->getContext($repoNode); // display the full identifier dd($context->getIdentifier());
2) Archiving a repository
- In a first step, a
SaveRequesthas to be created:
$swhClient = SwhWebApiClient::getCurrent(); // create a repository instance from a url that points to a repo or a specific file/directory inside the repo $repo = Repository::fromNodeUrl('https://github.com/.../...'); // submit a save request to Software Heritage $origin = Origin::fromRepository($repo); $saveRequest = $origin->postSaveRequest(); if ($saveRequest === null) { // connection or network error dd('Internal server error', $swhClient->getException(), $swhClient->getLastResponse()); } else { dd('SaveRequest created by SoftwareHeritage, SaveRequestId: '.$saveRequest->id); // store $saveRequest->id in local DB to track the status of this request }
- In a second step, the status of the
SaveRequesthas to be watched (within a loop/cron-job). The$saveRequestIdis the id obtained at the end of the first step.
$saveRequest = SaveRequest::byId($saveRequestId) if ($saveRequest->saveRequestStatus == SaveRequestStatus::REJECTED) { dd('save request rejected -> abort'); } elseif ($saveRequest->saveTaskStatus == SaveTaskStatus::SUCCEEDED) { if ($saveRequest->snapshotSwhId === null) { dd('no snapshot though request succeeded -> this should actually not happen'); } else { $snapshot = $saveRequest->getSnapshot(); $repoNode = new RepositoryNode($repoNodeUrl ?? $saveRequest->originUrl); $context = $snapshot->getContext($repoNode); dd('success', $snapshot, $context, $context->getIdentifier()); } } else { dd('pending -> loop this code block again', $saveRequest); }
3) Error Handling
If null is returned instead of an object of the requested type, that indicates that an error has occurred.
More information about the error can be obtained from the current SwhWebApiClient instance like so:
$snapshot = Snapshot::byId('non-existing-or-invalid-id'); // to provoke an error if ($snapshot === null) { $swhClient = SwhWebApiClient::getCurrent(); dd( $swhClient->getException(), // last exception (e.g., in case of a network issue) $swhClient->getLastResponse() // access the last HTTP response (incl. status code, headers) for debugging purposes ); }
4) Caching and rate limit
To reduce the number of requests, all requests except for the /origin/ and the /stat/counters/ endpoint are cached.
The cache folder has to specified as absolute path in config('swh.web-api.cache-folder').
To clear the cache, you can use the clearCache command:
$swhClient = SwhWebApiClient::getCurrent(); $swhClient->clearCache('2024-09-07'); // clears the cache for a specific date $swhClient->clearCache(); // clears the whole cache
To obtain information about your rate-limiting, call
$swhClient->getRateLimits();. This will return an array of the following type:
[
'X-RateLimit-Limit' => 1200, // max. number of permitted requests per hour
'X-RateLimit-Remaining' => 1138, // remaining in current period
'X-RateLimit-Reset' => 1620639052 // at this timestamp, the rate-limit will be refreshed
]
dagstuhl/swh-archive-client 适用场景与选型建议
dagstuhl/swh-archive-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 141 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 09 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「SoftwareHeritage」 「software archiving」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dagstuhl/swh-archive-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dagstuhl/swh-archive-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dagstuhl/swh-archive-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
AI Content Translation for Laravel powered by SharpAPI.com
AI SEO Meta Tag Generator for Laravel powered by SharpAPI.com
A Software Development Kit for Silverstripe Search
Library to create classes following the State pattern in PHP. This can be a cleaner way for an object to change its behavior at runtime without resorting to large monolithic conditional statements and this improve maintainability and workflow writing.
TAO is an Open Source Assessment platform that empowers you to build, deliver, and share innovative and engaging assessments online – in any language or subject matter.
Module provides Magento 2 with missing features
统计信息
- 总下载量: 141
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-10