Skip to content

Commit

Permalink
Create subscriptions for OneDrive notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
davicente committed May 11, 2017
1 parent 9b7d643 commit 4fa4c66
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Adapter/OneDriveNotificationsAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace JacekBarecki\FlysystemOneDrive\Adapter;

use JacekBarecki\FlysystemOneDrive\Client\OneDriveClient;
use JacekBarecki\FlysystemOneDrive\Client\OneDriveClientException;
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait;

class OneDriveNotificationsAdapter
{
use NotSupportingVisibilityTrait;

/**
* @var OneDriveClient
*/
private $client;

/**
* @param OneDriveClient $client
*/
public function __construct(OneDriveClient $client) {
$this->client = $client;
}


public function createSubscription($notificationUrl, $expirationDate, $clientState = "", $resource = "") {
if(!isset($notificationUrl)) {
throw new OneDriveClientException('NotificationUrl is mandatory');
}
$response = $this->client->subscribe($notificationUrl, $expirationDate, $clientState, $resource);
$responseContent = json_decode((string) $response->getBody());
return $responseContent;
}


}
40 changes: 40 additions & 0 deletions src/Client/OneDriveClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class OneDriveClient
private $guzzle;

const BASE_URI = 'https://api.onedrive.com/v1.0/';
const ROOT_PATH = 'drive/root';

/**
* @param string $accessToken
Expand Down Expand Up @@ -303,6 +304,45 @@ public function itemExists($path)
return true;
}


/**
* @param string $notificationUrl
* @param string $expirationDate
* @param string $clientState
* @param string $resource
*
* @return \Psr\Http\Message\ResponseInterface
*
* @throws OneDriveClientException
*
* @link https://dev.onedrive.com/webhooks/create-subscription.htm
*/
public function subscribe($notificationUrl, $expirationDate, $clientState=null, $resource = "")
{
if(!isset($notificationUrl)) {
throw new OneDriveClientException('NotificationUrl is mandatory');
}

$url = self::BASE_URI.self::ROOT_PATH.'/subscriptions';

$payload = [
'notificationUrl' => $notificationUrl,
'resource' => $resource,
'expirationDateTime' => $expirationDate
];

if(isset($clientState)) {
$payload['clientState'] = $clientState;
}

$headers = [
'Content-Type' => 'application/json'
];

return $this->getResponse('POST', $url, json_encode($payload), $headers);
}


/**
* @param string $path
* @param string $content
Expand Down

0 comments on commit 4fa4c66

Please sign in to comment.