Skip to content

Commit

Permalink
Add support for data regions
Browse files Browse the repository at this point in the history
  • Loading branch information
Simona Peneva committed Jul 13, 2020
1 parent 98607a6 commit 682ed7c
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 32 deletions.
2 changes: 1 addition & 1 deletion docs/api-oauth.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ initOauthToken({
client_id: string,
redirect_uri: string,
response_type: "token" | "code",
receiveToken: (token) => void
receiveToken: (token, locationid) => void
});
```

Expand Down
2 changes: 1 addition & 1 deletion docs/oauth.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pCloudSdk.oauth.initOauthToken({
client_id: "YOUR_CLIENT_ID",
redirect_uri: "YOUR_REDIRECT_URL",
response_type: "token" | "code",
receiveToken: function(access_token) {
receiveToken: function(access_token, locationid) {
// do something with the token.
}
});
Expand Down
4 changes: 3 additions & 1 deletion examples/download.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<script>
'use strict';
var access_token = false;
var locationid = "";
var client = false;

function downloadfile(fileid) {
Expand Down Expand Up @@ -73,9 +74,10 @@
pCloudSdk.oauth.initOauthToken({
client_id: 'p1WznE2dEPm',
redirect_uri: 'http://127.0.0.1:8080/oauth.html',
receiveToken: function(token) {
receiveToken: function(token, id) {
console.log(token);
access_token = token;
locationid = id || 1;
client = pCloudSdk.createClient(token);

listfiles();
Expand Down
10 changes: 6 additions & 4 deletions examples/listfolder.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<script>
'use strict';
var access_token = false;
var locationid = "";
var client = false;

function children(metadata) {
Expand Down Expand Up @@ -95,9 +96,10 @@
return client.listfolder(0).then(children).then(map(name));
}

function receiveTokenCb(token) {
console.log(token);
function receiveTokenCb(token, id) {
console.log(token, id);
access_token = token;
locationid = id || 1;
client = pCloudSdk.createClient(token);

folderSize(0).then((a) => {
Expand All @@ -111,7 +113,7 @@
// Oauth authenticate with redirect uri
el('gettoken-redirect').addEventListener('click', function (e) {
pCloudSdk.oauth.initOauthToken({
client_id: 'BJpmGFpuj3S',
client_id: '3dunsTvYJsu',
redirect_uri: 'http://127.0.0.1:8080/oauth.html',
receiveToken: receiveTokenCb,
onError: err => console.log(err)
Expand All @@ -121,7 +123,7 @@
// Oauth authenticate without redirect uri
el('gettoken-poll').addEventListener('click', function (e) {
pCloudSdk.oauth.initOauthPollToken({
client_id: 'BJpmGFpuj3S',
client_id: '3dunsTvYJsu',
receiveToken: receiveTokenCb,
onError: err => console.log(err)
});
Expand Down
6 changes: 4 additions & 2 deletions examples/remoteupload.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<script>
'use strict';
var access_token = false;
var locationid = "";
var client = false;
el("file").addEventListener('click', function(e) {
if (!client) {
Expand Down Expand Up @@ -56,9 +57,10 @@
pCloudSdk.oauth.initOauthToken({
client_id: 'p1WznE2dEPm',
redirect_uri: 'http://127.0.0.1:8080/oauth.html',
receiveToken: function(token) {
console.log(token);
receiveToken: function(token, id) {
console.log(token, id);
access_token = token;
locationid = id || 1;
client = pCloudSdk.createClient(token);
el('file').disabled = false;
}
Expand Down
6 changes: 4 additions & 2 deletions examples/renamefile.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<script>
'use strict';
var access_token = false;
var locationid = "";
var client = false;

function children(metadata) {
Expand Down Expand Up @@ -89,9 +90,10 @@
pCloudSdk.oauth.initOauthToken({
client_id: 'p1WznE2dEPm',
redirect_uri: 'http://' + location.host + '/oauth.html',
receiveToken: function(token) {
console.log(token);
receiveToken: function(token, id) {
console.log(token, id);
access_token = token;
locationid = id || 1;
client = pCloudSdk.createClient(token);

firstFile(0).then(file => {
Expand Down
6 changes: 4 additions & 2 deletions examples/share.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
var clientid = "p1WznE2dEPm";

var access_token = false;
var locationid = "";
var client = false;
var clientUserId = false;

Expand Down Expand Up @@ -105,9 +106,10 @@
pCloudSdk.oauth.initOauthToken({
client_id: clientid,
redirect_uri: 'http://127.0.0.1:8080/oauth.html',
receiveToken: function(token) {
console.log(token);
receiveToken: function(token, id) {
console.log(token, id);
access_token = token;
locationid = id || 1;
client = pCloudSdk.createClient(token);

client.userinfo().then(function (userinfo) {
Expand Down
10 changes: 6 additions & 4 deletions examples/thumbs.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<script>
'use strict';
var access_token = false;
var apiServer = "";
var client = false;

function children(metadata) {
Expand Down Expand Up @@ -99,16 +100,17 @@

el('gettoken').addEventListener('click', function(e) {
pCloudSdk.oauth.initOauthToken({
client_id: 'p1WznE2dEPm',
client_id: 'mQ35NJTiuQu',
redirect_uri: 'http://127.0.0.1:8080/oauth.html',
receiveToken: function(token) {
console.log(token);
receiveToken: function(token, id) {
console.log(token, id);
access_token = token;
locationid = id || 1;
client = pCloudSdk.createClient(token);

imagesInFolder(0).then(a => console.log('Items', a));
imagesInFolder(0).then(fileids => {
client.getthumbs(fileids, thumb => console.log(thumb), 'auto', '32x32', (thumb) => console.log('got thumb', thumb))
client.getthumbs(fileids, thumb => console.log(thumb), 'auto', '120x120', (thumb) => console.log('got thumb', thumb))
.then(stuff => console.log('==', stuff))
});
}
Expand Down
6 changes: 4 additions & 2 deletions examples/upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<script>
'use strict';
var access_token = false;
var locationid = "";
var client = false;
el("file").addEventListener('change', function(e) {
if (!client) {
Expand Down Expand Up @@ -53,9 +54,10 @@
response_type: "token",
client_id: 'p1WznE2dEPm',
redirect_uri: 'http://127.0.0.1:8080/oauth.html',
receiveToken: function(token) {
console.log(token);
receiveToken: function(token, id) {
console.log(token, id);
access_token = token;
locationid = id || 1;
client = pCloudSdk.createClient(token);
el('file').disabled = false;
}
Expand Down
8 changes: 6 additions & 2 deletions src/api/ApiMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import invariant from "invariant";
import { isApiMethod, isAuthMethod } from "../utils";
import ApiRequest from "./ApiRequest";
import type { ApiResult, ApiMethodOptions } from "./types";
const defaultApiServer = "api.pcloud.com";
const defaultApiServer = "eapi.pcloud.com";

const locations = {
1: "api.pcloud.com",
2: "eapi.pcloud.com",
};
export default function ApiMethod(method: string, options: ApiMethodOptions = {}): Promise<ApiResult> {
const { apiServer = defaultApiServer, apiProtocol = "https", params = {}, ...requestParams } = options;

Expand All @@ -19,7 +23,7 @@ export default function ApiMethod(method: string, options: ApiMethodOptions = {}

const requestUrl: string = url.format({
protocol: apiProtocol,
host: apiServer,
host: locations[locationid] || apiServer,
pathname: method,
query: params,
});
Expand Down
4 changes: 1 addition & 3 deletions src/client/createClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ export default function createClient(token: string, type: ClientType = "oauth",

// Local Params
// apiServer, token, type
let apiServer = defaultApiServer;

function initialOptions(method: string) {
let options = { apiServer: apiServer, params: {} };
let options = { params: {} };

if (isAuthMethod(method) && token) {
options.params["oauth" === type ? "access_token" : "auth"] = token;
Expand All @@ -39,7 +38,6 @@ export default function createClient(token: string, type: ClientType = "oauth",
if (error.result === 500 && apiServer !== defaultApiServer) {
// reset API server
apiServer = defaultApiServer;

// retry
return api(method, options);
} else {
Expand Down
18 changes: 10 additions & 8 deletions src/oauth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ function initOauthToken(options: oAuthOptions) {
response_type: response_type,
});

window.open(oauthUrl, "oauth", "width=680,height=535");
window.__setPcloudToken = function(token) {
receiveToken(token);
window.open(oauthUrl, "oauth", "width=680,height=700");
window.__setPcloudToken = function(token, locationid) {
receiveToken(token, locationid);
delete window.__setPcloudToken;
};
}
Expand All @@ -65,13 +65,14 @@ function initOauthPollToken(options: oAuthPollOptions) {
client_id: client_id,
response_type: "poll_token",
});
window.open(oauthUrl, "", "width=680,height=535");
window.open(oauthUrl, "", "width=680,height=700");

ApiMethod("oauth2_token", {
params: { client_id: client_id, request_id: request_id },
params: { client_id: client_id, request_id: request_id, apiServer: "eapi.pcloud.com" },
})
.then(res => {
receiveToken(res.access_token);
console.log("oauth2_token res>>>>>>>>>>", res)
receiveToken(res.access_token, res.locationid);
})
.catch(err => {
onError(err);
Expand All @@ -81,11 +82,12 @@ function initOauthPollToken(options: oAuthPollOptions) {
function popup() {
const matchToken = location.hash.match(/access_token=([^&]+)/);
const matchCode = location.search.match(/code=([^&]+)/);

const locationIdMatch = location.hash.match(/locationid=([^&]+)/);
const locationid = locationIdMatch ? locationIdMatch[1] : null;
const token = matchToken ? matchToken[1] : matchCode ? matchCode[1] : null;

if (token) {
window.opener.__setPcloudToken(token);
window.opener.__setPcloudToken(token, locationid);
window.close();
}
}
Expand Down

0 comments on commit 682ed7c

Please sign in to comment.