jonathanraftery/bullhorn-rest-client

Bullhorn API的简单REST客户端,包括自动化的OAuth2登录

1.4.1 2022-10-02 12:14 UTC

This package is auto-updated.

Last update: 2024-08-30 01:06:13 UTC


README

提供Bullhorn REST API的简单客户端。

安装

$ composer require jonathanraftery/bullhorn-rest-client

使用

use jonathanraftery\Bullhorn\Rest\Client as BullhornClient;
$client = new BullhornClient();

默认情况下,客户端将在环境变量中查找凭据

  • BULLHORN_CLIENT_ID
  • BULLHORN_CLIENT_SECRET
  • BULLHORN_USERNAME
  • BULLHORN_PASSWORD

选项

客户端构造函数接受一个选项数组。

use jonathanraftery\Bullhorn\Rest\Auth\CredentialsProvider\MemoryCredentialsProvider;
use jonathanraftery\Bullhorn\Rest\Client as BullhornClient;
use jonathanraftery\Bullhorn\Rest\ClientOptions;

$client = new BullhornClient([
    // NOTE: MemoryCredentialProvider not recommended for production
    ClientOptions::CredentialsProvider => new MemoryCredentialsProvider(
        'clientId', 'clientSecret', 'username', 'password'
    ),
]);

可以使用ClientOptions类查看可用选项。

凭据提供者

凭据提供者向客户端提供连接API所需的凭据。包括简单提供者,或者你可以通过实现CredentialsProviderInterface的类(例如,从Google Cloud Secret Manager获取凭据)创建自己的提供者。

use jonathanraftery\Bullhorn\Rest\Client as BullhornClient;
use jonathanraftery\Bullhorn\Rest\ClientOptions;
use jonathanraftery\Bullhorn\Rest\Auth\CredentialsProvider\CredentialsProviderInterface;

class CustomCredentialProvider implements CredentialsProviderInterface {
    public function getClientId() : string{ return 'id'; }
    public function getClientSecret() : string{ return 'secret'; }
    public function getUsername() : string{ return 'username'; }
    public function getPassword() : string{ return 'password'; }
}

$client = new BullhornClient([
    ClientOptions::CredentialsProvider => new CustomCredentialProvider()
]);

默认情况下,客户端将使用EnvironmentCredentialsProvider,它将在上面列出的环境变量中查找凭据。可以通过使用其他变量构建EnvironmentCredentialsProvider来更改所使用的变量。

认证数据存储

API会话数据需要持久化在数据存储中。默认情况下,客户端将使用本地JSON文件进行此存储,但可以使用自定义存储。

use jonathanraftery\Bullhorn\Rest\Client as BullhornClient;
use jonathanraftery\Bullhorn\Rest\ClientOptions;
use jonathanraftery\Bullhorn\Rest\Auth\Store\DataStoreInterface;

class CustomDataStore implements DataStoreInterface {
    private $vars;

    public function get(string $key) : ?string{
        return $this->vars[$key];
    }

    public function store(string $key,$value){
        $this->vars[$key] = $value;
    }
}

$client = new BullhornClient([
    ClientOptions::AuthDataStore => new CustomDataStore()
]);

初始OAuth同意

在Bullhorn授权来自新用户的API调用之前,用户需要给予同意。如果尚未给予同意,则库将抛出IdentityException,客户端将响应带有同意表单的HTML表示。

要永久解决这个问题,请使用凭据访问授权URL auth.bullhornstaffing.com/oauth/authorize?response_type=code&action=Login&username=&password=&state=<client_secret>&approval_prompt=auto&client_id=<client_id> 并在bullhorn中登录,然后按 同意 按钮。这将授权你的应用程序以用户的名义使用API。

原始请求

可以像Bullhorn API文档中记录的简单请求一样运行

use jonathanraftery\Bullhorn\Rest\Client as BullhornClient;
$client = new BullhornClient();
$response = $client->rawRequest(
    'GET',
    'search/JobOrder',
    [
        'query' => 'id:1234'
    ]
);

PUT/POST请求

客户端使用GuzzleHTTP进行请求,请求方法中的参数与创建Guzzle请求对象时的参数匹配。第三个参数是请求选项,如Guzzle文档中所述。

要设置PUT/POST请求的主体,将请求的"body"选项设置为请求主体的JSON内容,如下所示

use jonathanraftery\Bullhorn\Rest\Client as BullhornClient;
$client = new BullhornClient();
$response = $client->rawRequest(
    'PUT',
    'entity/Candidate',
    [
        'body' => json_encode(['firstName' => 'Alanzo', 'lastName' => 'Smith', 'status' => 'Registered'])
    ]
);

实体操作

可以检索、创建和删除实体

use jonathanraftery\Bullhorn\Rest\Client as BullhornClient;
use jonathanraftery\Bullhorn\Rest\BullhornEntities;
$client = new BullhornClient();
$fetchedJobOrders = $client->fetchEntities(BullhornEntities::JobOrder, [1,2,3], [
    'fields' => 'id',
]);
$createdJobOrder = $client->createEntity(BullhornEntities::JobOrder, [
    'propName' => 'value',
    'propName2' => 'value',
]);
$deleteId = 1;
$client->deleteEntity(BullhornEntities::JobOrder, $deleteId);

事件订阅

use jonathanraftery\Bullhorn\Rest\Client as BullhornClient;
use jonathanraftery\Bullhorn\Rest\BullhornEntities;
use jonathanraftery\Bullhorn\Rest\EventTypes;
$client = new BullhornClient();
$client->createEventSubscription('SubscriptionName', [BullhornEntities::JobOrder], [EventTypes::Created]);
$client->fetchEventSubscriptionEvents('SubscriptionName');
$client->deleteEventSubscription('SubscriptionName');

会话

如果数据存储中没有会话,则将在第一次请求时自动启动会话。

可以通过以下方式手动启动会话

use jonathanraftery\Bullhorn\Rest\Client as BullhornClient;
$client = new BullhornClient();
$client->initiateSession();

如果检测到过期,会话将自动刷新,或者可以手动刷新(如可选参数所示)

use jonathanraftery\Bullhorn\Rest\Client as BullhornClient;
$client = new BullhornClient();
$client->refreshSession(['ttl' => 60]);