keboola/notification-api-php-client

Notification API 客户端

3.0.0 2023-08-03 10:28 UTC

This package is auto-updated.

Last update: 2024-09-03 12:38:26 UTC


README

Notification API 的 PHP 客户端 (API 文档)。

用法

客户端使用两种授权方式 - 存储API令牌用于订阅API(SubscriptionClient 类)和带有作用域 notifications:push-event 的管理API应用程序令牌用于事件API(EventsClient 类)。

composer require keboola/notification-api-php-client
use Keboola\NotificationClient\EventsClient;
use Keboola\NotificationClient\Requests\PostEvent\JobFailedEventData;
use Keboola\NotificationClient\Requests\PostEvent\JobData;
use Keboola\NotificationClient\Requests\Event;
use Psr\Log\NullLogger;

$client = new EventsClient(
    new NullLogger(),
    'http://notifications.connection.keboola.com/',
    'xxx-xxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
$client->postEvent(
    new Event(        
        new JobFailedEventData(
            '123',
            'My Project',
            'Job finished with error',
            new JobData('my-project', '123', 'http://someUrl', '2020-01-02', '2020-01-01', 'my-orchestration')
        )
    )
);

或使用工厂创建客户端

use Keboola\NotificationClient\ClientFactory;

$clientFactory = new ClientFactory('https://connection.keboola.com');
$clientFactory->getEventsClient('xxx-xxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

开发

  • 创建一个Azure服务主体以下载所需的映像并登录

        SERVICE_PRINCIPAL_NAME=[USERNAME]-notification-api-pull
        ACR_REGISTRY_ID=$(az acr show --name keboolapes --query id --output tsv --subscription c5182964-8dca-42c8-a77a-fa2a3c6946ea)
        SP_PASSWORD=$(az ad sp create-for-rbac --name http://$SERVICE_PRINCIPAL_NAME --scopes $ACR_REGISTRY_ID --role acrpull --query password --output tsv)
        SP_APP_ID=$(az ad sp show --id http://$SERVICE_PRINCIPAL_NAME --query appId --output tsv)    
  • 登录并检查您是否可以拉取映像

        docker login keboolapes.azurecr.io --username $SP_APP_ID --password $SP_PASSWORD
        docker pull keboolapes.azurecr.io/notification-service:latest
  • 将凭据添加到k8s集群

        kubectl create secret docker-registry regcred --docker-server="https://keboolapes.azurecr.io" --docker-username="$SP_APP_ID" --docker-password="$SP_PASSWORD" --namespace dev-notification-client
        kubectl patch serviceaccount default -p "{\"imagePullSecrets\":[{\"name\":\"regcred\"}]}" --namespace dev-notification-client
  • set-env.sh 文件中设置以下环境变量(使用 set-env.template.sh 作为示例)

    • STORAGE_API_URL - Keboola Connection URL,指向已注册通知服务的任意堆栈。
    • TEST_STORAGE_API_TOKEN - 测试项目的令牌。
    • TEST_STORAGE_API_PROJECT_ID - 测试项目的项目ID。
    • TEST_MANAGE_API_APPLICATION_TOKEN - 带有作用域 notifications:push-event 的应用程序令牌。
  • 设置Azure或AWS资源之一(或两者,但只需要一个)。

AWS 设置

  • 使用 provisioning/aws.json CF 模板创建用于本地开发的用户(NotificationUser)。
    • 为创建的用户创建AWS密钥。
    • set-env.sh 文件中设置以下环境变量(使用 set-env.template.sh 作为示例)
      • TEST_AWS_ACCESS_KEY - 为 JobQueueApiPhpClient 用户创建的安全凭证。
      • TEST_AWS_SECRET_ACCESS_KEY - 为 JobQueueApiPhpClient 用户创建的安全凭证。
      • AWS_REGION - 上述堆栈的 Region 输出。
      • AWS_LOGS_S3_BUCKET - 上述堆栈的 S3LogsBucket 输出。

Azure 设置

  • 创建一个资源组

    az account set --subscription "Keboola DEV PS Team CI"
    az group create --name testing-notification-api-php-client --location "East US"
  • 创建一个服务主体

    az ad sp create-for-rbac --name testing-notification-api-php-client
  • 使用响应设置 TEST_AZURE_CLIENT_IDTEST_AZURE_CLIENT_SECRETTEST_AZURE_TENANT_ID 的值在 set-env.sh 文件中

    {
      "appId": "268a6f05-xxxxxxxxxxxxxxxxxxxxxxxxxxx", //-> TEST_AZURE_CLIENT_ID
      "displayName": "testing-notification-api-php-client",
      "name": "http://testing-notification-api-php-client",
      "password": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", //-> TEST_AZURE_CLIENT_SECRET
      "tenant": "9b85ee6f-xxxxxxxxxxxxxxxxxxxxxxxxxxx" //-> TEST_AZURE_TENANT_ID
    }
  • 获取服务主体的ID

    SERVICE_PRINCIPAL_ID=$(az ad sp list --display-name testing-notification-api-php-client --query "[0].objectId" --output tsv)
  • 部署用于日志的存储帐户,提供前面的命令中的租户ID、服务主体ID和组ID

    az deployment group create --resource-group testing-notification-api-php-client --template-file provisioning/azure.json --parameters vault_name=test-notification-client tenant_id=$TEST_AZURE_TENANT_ID service_principal_object_id=$SERVICE_PRINCIPAL_ID
  • 获取连接字符串

    az storage account show-connection-string -g testing-notification-api-php-client -n mirontfcnacc2 --query "connectionString" --output tsv

将提供的参数作为创建命令的连接字符串和容器名称设置到 set-env.sh 文件中的以下环境变量

  • AZURE_LOGS_ABS_CONTAINER
  • AZURE_LOGS_ABS_CONNECTION_STRING

生成环境配置

export DATABASE_URL_BASE64=$(printf "mysql://root:root@dev-mysql-service:3310/notifications?serverVersion=8.0" | base64 --wrap=0)
export DATABASE_PASSWORD_BASE64=$(printf "root" | base64 --wrap=0)
export TEST_AZURE_CLIENT_SECRET_BASE64=$(printf "%s" "$TEST_AZURE_CLIENT_SECRET"| base64 --wrap=0)
export TEST_AWS_SECRET_ACCESS_KEY_BASE64=$(printf "%s" "$TEST_AWS_SECRET_ACCESS_KEY"| base64 --wrap=0)
export AZURE_LOGS_ABS_CONNECTION_STRING_BASE64=$(printf "%s" "$AZURE_LOGS_ABS_CONNECTION_STRING"| base64 --wrap=0)

./set-env.sh
envsubst < provisioning/environments.yaml.template > provisioning/environments.yaml
kubectl apply -f provisioning/environments.yaml
kubectl apply -f provisioning/notification.yaml
TEST_NOTIFICATION_API_IP=`kubectl get svc --output jsonpath --template "{.items[?(@.metadata.name==\"dev-notification-service\")].status.loadBalancer.ingress[].ip}" --namespace=dev-notification-client`

printf "TEST_NOTIFICATION_API_URL: http://%s:8181" "$TEST_NOTIFICATION_API_IP"

将结果 TEST_NOTIFICATION_API_URL 存储在 set-env.sh 中。

运行测试

  • 使用上述设置,您可以运行测试

    docker-compose build
    source ./set-env.sh && docker-compose run tests
  • 要使用本地代码运行测试,请使用

    docker-compose run tests-local composer install
    source ./set-env.sh && docker-compose run tests-local

许可

MIT 许可,请参阅 LICENSE 文件。