Skip to content

Commit

Permalink
Merge pull request #7 from upwork/v2.1.4
Browse files Browse the repository at this point in the history
Add GraphQL support
  • Loading branch information
mnovozhylov committed Jan 3, 2022
2 parents 9d3475c + 36f1d7e commit 960bd11
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 6 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/vendor
*~
composer.lock
Build
Makefile.PL
META.yml
META.json
MYMETA.yml
MYMETA.json
blib/*
_build/*
*.bak
Net-Upwork-API-*.tar.gz
2 changes: 1 addition & 1 deletion Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ my $builder = Module::Build->new(
module_name => 'Net::Upwork::API',
license => 'apache',
dist_author => 'Maksym Novozhylov <mnovozhilov@upwork.com>',
dist_version => '2.1.3',
dist_version => '2.1.4',
dist_abstract => 'Perl bindings for Upwork API (OAuth2)',
build_requires => {
'Test::More' => '0.66',
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2.1.4
* Add GraphQL support

## 2.1.3
* Fixed HR module

Expand Down
2 changes: 2 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ lib/Net/Upwork/API/Routers/Activities/Team.pm
lib/Net/Upwork/API/Routers/Auth.pm
lib/Net/Upwork/API/Routers/Freelancers/Profile.pm
lib/Net/Upwork/API/Routers/Freelancers/Search.pm
lib/Net/Upwork/API/Routers/Graphql.pm
lib/Net/Upwork/API/Routers/Hr/Clients/Applications.pm
lib/Net/Upwork/API/Routers/Hr/Clients/Offers.pm
lib/Net/Upwork/API/Routers/Hr/Contracts.pm
Expand Down Expand Up @@ -76,3 +77,4 @@ t/28.routers_reports_time.t
t/29.routers_reports_finance_accounts.t
t/30.routers_reports_finance_billings.t
t/31.routers_reports_finance_earnings.t
t/32.routers_graphql.t
22 changes: 22 additions & 0 deletions example/example.pl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Data::Dumper;
use Net::Upwork::API;
use Net::Upwork::API::Routers::Auth;
use Net::Upwork::API::Routers::Graphql;

$config = Net::Upwork::API::Config->new(
'client_id' => 'xxxxxxxx',
Expand Down Expand Up @@ -55,3 +56,24 @@
$data = $auth->get_user_info();

print Dumper $data;

my $query = <<'EOF';
query {
user {
id
nid
rid
}
organization {
id
}
}
EOF
$graphql = Net::Upwork::API::Routers::Graphql->new($api);
#$graphql->set_org_uid_header('1234567890'); # Organization UID (optional)
%params = (
'query' => $query
);
$data2 = $graphql->execute(%params);

print Dumper $data2;
2 changes: 1 addition & 1 deletion lib/Net/Upwork/API.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use warnings;
use Net::Upwork::API::Config;
use Net::Upwork::API::Client;

our $VERSION = '2.1.3';
our $VERSION = '2.1.4';

use constant TOKEN_TYPE_BEARER => 'Bearer';

Expand Down
44 changes: 41 additions & 3 deletions lib/Net/Upwork/API/Client.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use LWP::UserAgent;

use constant BASE_HOST => "https://www.upwork.com";
use constant DEFAULT_EPOINT => "api";
use constant GRAPHQL_EPOINT => "https://api.upwork.com/graphql";

use constant DATA_FORMAT => "json";
use constant OVERLOAD_VAR => "http_method";
Expand All @@ -31,9 +32,12 @@ use constant URI_ATOKEN => "/api/v3/oauth2/token";

use constant ENTRY_POINT_API => "api";
use constant ENTRY_POINT_GDS => "gds";
use constant ENTRY_POINT_GQL => "graphql";

use constant UPWORK_LIBRARY_USER_AGENT => "Github Upwork API Perl Client";

our $lwp;

=pod
=head1 NAME
Expand Down Expand Up @@ -77,8 +81,8 @@ sub new {
sub get_oauth_client {
my $self = shift;

my $ua = LWP::UserAgent->new();
$ua->agent(UPWORK_LIBRARY_USER_AGENT);
$lwp = LWP::UserAgent->new();
$lwp->agent(UPWORK_LIBRARY_USER_AGENT);

$self->{oauth_client} = Net::OAuth2::Profile::WebServer->new(
client_id => $self->{config}{client_id},
Expand All @@ -92,7 +96,7 @@ sub get_oauth_client {
access_token_path => URI_ATOKEN,
refresh_token_path => URI_ATOKEN,
redirect_uri => $self->{config}{redirect_uri},
user_agent => $ua
user_agent => $lwp
);
}

Expand Down Expand Up @@ -245,6 +249,40 @@ sub send_request {
return $response->decoded_content;
}

=item graphql_request
Send a signed GraphQL request to a specific OAuth2 protected resource
B<Parameters>
$params
API parameters
B<Return value>
String, a response content
=cut

sub graphql_request {
my ($self, $json, $tenant_id) = @_;

my $req = HTTP::Request->new('POST', GRAPHQL_EPOINT);
$req->header(
'Content-Type' => 'application/json',
'Authorization' => sprintf("%s %s", $self->{access_token_session}->token_type(), $self->{access_token_session}->access_token())
);
if (defined($tenant_id)) {
$req->header('X-Upwork-API-TenantId' => $tenant_id);
}
$req->content($json);

my $res = $lwp->request($req);

return $res->decoded_content;
}

=item format_uri
Create a path to a specific resource
Expand Down
103 changes: 103 additions & 0 deletions lib/Net/Upwork/API/Routers/Graphql.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Licensed under the Upwork's API Terms of Use;
# you may not use this file except in compliance with the Terms.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author:: Maksym Novozhylov (mnovozhilov@upwork.com)
# Copyright:: Copyright 2021(c) Upwork.com
# License:: See LICENSE.txt and TOS - https://developers.upwork.com/api-tos.html

package Net::Upwork::API::Routers::Graphql;

use strict;
use warnings;
use parent "Net::Upwork::API";
use JSON::MaybeXS qw/encode_json/;

use constant ENTRY_POINT => Net::Upwork::API::Client::ENTRY_POINT_GQL;

=pod
=head1 NAME
Graphql
=head1 FUNCTIONS
=over 4
=item new($api)
Create a new object for accessing GraphQL API
B<Parameters>
$api
API object
=cut

sub new {
my ($class, $api) = @_;
return Net::Upwork::API::init_router($class, $api, ENTRY_POINT);
}

=item set_org_uid_header
Configure X-Upwork-API-TenantId header
B<Parameters>
$tenant_id
Organization UID
B<Return value>
void
=cut

sub set_org_uid_header {
my ($self, $tenant_id) = @_;

$self->{tenant_id} = $tenant_id;
}

=item execute
Execute GraphQL request
B<Return value>
JSON response as a string
=cut

sub execute {
my $self = shift;
my %params = @_;

my $json = encode_json \%params;

return $self->client()->graphql_request($json, $self->{tenant_id});
}

=back
=head1 AUTHOR
Maksym Novozhylov C<< <mnovozhilov@upwork.com> >>
=head1 COPYRIGHT
Copyright E<copy> Upwork Global Corp., 2021
=cut

1;
3 changes: 2 additions & 1 deletion t/00.load.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env perl
use strict;
use Test::More tests => 29;
use Test::More tests => 30;
use lib qw(lib);

use_ok('Net::Upwork::API');
Expand Down Expand Up @@ -32,3 +32,4 @@ use_ok('Net::Upwork::API::Routers::Hr::Clients::Applications');
use_ok('Net::Upwork::API::Routers::Hr::Clients::Offers');
use_ok('Net::Upwork::API::Routers::Hr::Freelancers::Applications');
use_ok('Net::Upwork::API::Routers::Hr::Freelancers::Offers');
use_ok('Net::Upwork::API::Routers::Graphql');
9 changes: 9 additions & 0 deletions t/32.routers_graphql.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env perl
use strict;
use Test::More tests => 3;
use lib qw(lib);
use Net::Upwork::API::Routers::Graphql;

can_ok('Net::Upwork::API::Routers::Graphql', 'new');
can_ok('Net::Upwork::API::Routers::Graphql', 'set_org_uid_header');
can_ok('Net::Upwork::API::Routers::Graphql', 'execute');

0 comments on commit 960bd11

Please sign in to comment.