Skip to content

Commit b39198a

Browse files
authored
Merge pull request #11 from upwork/v2.2.1
Add GraphQL support
2 parents 23b901b + 428d27b commit b39198a

File tree

9 files changed

+144
-8
lines changed

9 files changed

+144
-8
lines changed

CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Release History
22

3+
## 2.3.0
4+
* Add GraphQL support
5+
36
## 2.2.0
47
* Send Message to a Batch of Rooms API
58
* Milestones::delete was replaced with Milestones::deleteMilestone (breaking!)

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "upwork/php-upwork-oauth2",
33
"description": "PHP bindings for Upwork API (OAuth2)",
4-
"version": "v2.2.0",
4+
"version": "v2.3.0",
55
"type": "library",
66
"keywords": ["upwork", "php", "api", "oauth2"],
77
"homepage": "http://www.upwork.com",
8-
"time": "2020-09-18",
8+
"time": "2021-12-28",
99
"license": "Apache-2.0",
1010
"authors": [
1111
{

example/example.php

+17
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
);
3333

3434
$client = new \Upwork\API\Client($config);
35+
//$client::setOrgUidHeader('1234567890'); // Organization UID (optional)
3536

3637
// $accessTokenInfo has the following structure
3738
// array('access_token' => ..., 'refresh_token' => ..., 'expires_in' => ...);
@@ -54,3 +55,19 @@
5455
$auth = new \Upwork\API\Routers\Auth($client);
5556
$info = $auth->getUserInfo();
5657
print_r($info);
58+
59+
/*$graphql = new \Upwork\API\Routers\Graphql($client);
60+
$params['query'] = <<<QUERY
61+
query {
62+
user {
63+
id
64+
nid
65+
rid
66+
}
67+
organization {
68+
id
69+
}
70+
}
71+
QUERY;
72+
$data = $graphql->execute($params);
73+
print_r($data);*/

src/Upwork/API/AuthTypes/OAuth2ClientLib.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,25 @@ public function getInstance()
5252
* @param string $type Type of request
5353
* @param string $url URL
5454
* @param array $params (Optional) Parameters
55+
* @param string $tenantId (Optional) Organization UID
5556
* @access public
5657
* @return mixed
5758
* @throw ApiException Invalid method requested
5859
*/
59-
public function request($type, $url, $params = array())
60+
public function request($type, $url, $params = array(), string $tenantId = null)
6061
{
6162
ApiDebug::p('running request from ' . __CLASS__);
6263

6364
switch ($type) {
6465
case \League\OAuth2\Client\Provider\AbstractProvider::METHOD_POST:
65-
$options = array('headers' => array('content-type' => 'application/x-www-form-urlencoded'));
66-
$options['body'] = http_build_query($params, null, '&', \PHP_QUERY_RFC3986);
66+
if (self::$_epoint == UPWORK_GRAPHQL_EP_NAME) {
67+
$options = array('headers' => array('content-type' => 'application/json'));
68+
is_null($tenantId) || $options['headers']['X-Upwork-API-TenantId'] = $tenantId;
69+
$options['body'] = $params;
70+
} else {
71+
$options = array('headers' => array('content-type' => 'application/x-www-form-urlencoded'));
72+
$options['body'] = http_build_query($params, null, '&', \PHP_QUERY_RFC3986);
73+
}
6774

6875
$url = ApiUtils::getFullUrl($url, self::$_epoint);
6976
break;
@@ -84,6 +91,9 @@ public function request($type, $url, $params = array())
8491
// do not use getParsedResponse, it returns an array
8592
// but we need a raw json that will be decoded and returned as StdClass object
8693
$response = $this->getInstance()->getResponse($request);
94+
} catch (\GuzzleHttp\Exception\ClientException $e) {
95+
$eResponse = $e->getResponse();
96+
$response = $eResponse->getBody()->getContents();
8797
} catch (\Exception $e) {
8898
$response = $e->getResponseBody();
8999
}

src/Upwork/API/Client.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ class Client
3030
*/
3131
static protected $_epoint = UPWORK_API_EP_NAME;
3232

33+
/**
34+
* @var Organization UID
35+
*/
36+
static protected $_tenantId = null;
37+
3338
/**
3439
* @var Server instance (AuthTypes)
3540
*/
@@ -112,6 +117,17 @@ public function auth()
112117
return $this->_server->auth();
113118
}
114119

120+
/**
121+
* Configure X-Upwork-API-TenantId header
122+
*
123+
* @param string $tenantId (Optional) Organization UID
124+
* @return void
125+
*/
126+
public static function setOrgUidHeader(string $tenantId)
127+
{
128+
self::$_tenantId = $tenantId;
129+
}
130+
115131
/**
116132
* Run API request
117133
*
@@ -140,15 +156,17 @@ protected function _request($type, $url, $params = array())
140156
break;
141157
}
142158

143-
if (self::$_epoint == UPWORK_API_EP_NAME) {
159+
if (self::$_epoint == UPWORK_GRAPHQL_EP_NAME) {
160+
$params = json_encode($params);
161+
} elseif (self::$_epoint == UPWORK_API_EP_NAME) {
144162
$url = $url . '.' . self::DATA_FORMAT;
145163
} elseif (self::$_epoint == UPWORK_GDS_EP_NAME) {
146164
$params['tqx'] = 'out:' . self::DATA_FORMAT;
147165
}
148166

149167
$this->_server->option('epoint', self::$_epoint);
150168

151-
$response = $this->_server->request($method, $url, $params);
169+
$response = $this->_server->request($method, $url, $params, self::$_tenantId);
152170

153171
return json_decode($response);
154172
}

src/Upwork/API/Routers/Graphql.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Upwork auth library for using with public API by OAuth
4+
* GraphQL
5+
*
6+
* @final
7+
* @package UpworkAPI
8+
* @since 12/28/2021
9+
* @copyright Copyright 2021(c) Upwork.com
10+
* @author Maksym Novozhylov <mnovozhilov@upwork.com>
11+
* @license Upwork's API Terms of Use {@link https://developers.upwork.com/api-tos.html}
12+
*/
13+
14+
namespace Upwork\API\Routers;
15+
16+
use Upwork\API\Debug as ApiDebug;
17+
use Upwork\API\Client as ApiClient;
18+
19+
/**
20+
* GraphQL
21+
*
22+
* @link https://www.upwork.com/developer/
23+
*/
24+
final class Graphql extends ApiClient
25+
{
26+
const ENTRY_POINT = UPWORK_GRAPHQL_EP_NAME;
27+
28+
/**
29+
* @var Client instance
30+
*/
31+
private $_client;
32+
33+
/**
34+
* Constructor
35+
*
36+
* @param ApiClient $client Client object
37+
*/
38+
public function __construct(ApiClient $client)
39+
{
40+
ApiDebug::p('init ' . __CLASS__ . ' router');
41+
$this->_client = $client;
42+
parent::$_epoint = self::ENTRY_POINT;
43+
}
44+
45+
/**
46+
* Execute GraphQL request
47+
*
48+
* @param array $params List of parameters (query and variables)
49+
* @return object
50+
*/
51+
public function execute(array $params)
52+
{
53+
ApiDebug::p(__FUNCTION__);
54+
55+
$data = $this->_client->post('', $params);
56+
ApiDebug::p('found GraphQL data', $data);
57+
58+
return $data;
59+
}
60+
}

src/Upwork/API/Utils.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static public function getFullUrl($url, $ep = null, $params = null)
3737
? 'UPWORK_BASE_URL_' . strtoupper($ep)
3838
: 'UPWORK_BASE_URL';
3939

40-
$queryData = is_array($params) ? $queryData = '?' . http_build_query($params) : '';
40+
$queryData = (is_array($params) && $ep != UPWORK_GRAPHQL_EP_NAME) ? $queryData = '?' . http_build_query($params) : '';
4141
$fullUrl = constant($name) . $url . $queryData;
4242
ApiDebug::p('url', $fullUrl);
4343

src/Upwork/API/constants.php

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
define('UPWORK_API_EP_NAME', 'api');
1414
define('UPWORK_GDS_EP_NAME', 'gds');
15+
define('UPWORK_GRAPHQL_EP_NAME', 'graphql');
1516
define('UPWORK_BASE_URL', 'https://www.upwork.com');
1617
define('UPWORK_BASE_URL_API', UPWORK_BASE_URL . '/' . UPWORK_API_EP_NAME);
1718
define('UPWORK_BASE_URL_GDS', UPWORK_BASE_URL . '/' . UPWORK_GDS_EP_NAME);
19+
define('UPWORK_BASE_URL_GRAPHQL', 'https://api.upwork.com/graphql');
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
namespace Upwork\API\Tests\Routers;
3+
4+
require_once __DIR__ . '/CommonTestRouter.php';
5+
6+
class GraphqlTest extends CommonTestRouter
7+
{
8+
/**
9+
* Setup
10+
*/
11+
public function setUp(): void
12+
{
13+
parent::setUp();
14+
}
15+
16+
/**
17+
* @test
18+
*/
19+
public function testExecute()
20+
{
21+
$router = new \Upwork\API\Routers\Graphql($this->_client);
22+
$response = $router->Execute(array('query' => 'query{}'));
23+
24+
$this->_checkResponse($response);
25+
}
26+
}

0 commit comments

Comments
 (0)