-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
140 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import requests | ||
|
||
from pcloud.utils import log | ||
from requests_toolbelt.multipart.encoder import MultipartEncoder | ||
from pcloud.jsonprotocol import PCloudJSONConnection | ||
|
||
class NoOpSession(object): | ||
kwargs = {} | ||
|
||
def get(self, url, **kwargs): | ||
self.kwargs = kwargs | ||
self.kwargs["url"] = url | ||
return self | ||
|
||
def json(self): | ||
return self.kwargs | ||
|
||
class PCloudDummyConnection(PCloudJSONConnection): | ||
"""Connection to pcloud.com based on their JSON protocol.""" | ||
|
||
allowed_endpoints = frozenset(["test"]) | ||
|
||
def __init__(self, api): | ||
"""Connect to pcloud API based on their JSON protocol.""" | ||
self.session = NoOpSession() | ||
self.api = api | ||
|
||
def connect(self): | ||
return self | ||
|
||
def do_get_request(self, method, authenticate=True, json=True, endpoint=None, **kw): | ||
if "noop" in kw: | ||
kw.pop("noop") | ||
params = { | ||
"params": kw, | ||
"url": self.api.endpoint + method | ||
} | ||
return params | ||
else: | ||
return super().do_get_request(method, authenticate, json, endpoint, **kw) | ||
|
||
def upload(self, method, files, **kwargs): | ||
if self.api.auth_token: # Password authentication | ||
kwargs["auth"] = self.api.auth_token | ||
elif self.api.access_token: # OAuth2 authentication | ||
kwargs["access_token"] = self.api.access_token | ||
fields = list(kwargs.items()) | ||
fields.extend(files) | ||
|
||
# from requests import Request, Session | ||
|
||
# s = Session() | ||
|
||
# for entry in files: | ||
# filename, fd = entry[1] | ||
# fields["filename"] = filename | ||
# req = Request('PUT', self.api.endpoint + method, data=fields) | ||
# prepped = req.prepare() | ||
# prepped.body = fd | ||
# resp = s.send(prepped) | ||
|
||
# resp = self.session.post(self.api.endpoint + method, files=files, data=kwargs) | ||
m = MultipartEncoder(fields=fields) | ||
resp = requests.post( | ||
self.api.endpoint + method, data=m, headers={"Content-Type": m.content_type} | ||
) | ||
# data = dump_all(resp) | ||
# print(data.decode('utf-8')) | ||
return resp.json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from pcloud.dummyprotocol import PCloudDummyConnection | ||
from pcloud.jsonprotocol import PCloudJSONConnection | ||
from pcloud.binaryprotocol import PCloudBinaryConnection | ||
|
||
|
||
class TestProtocol(object): | ||
name = "test" | ||
endpoint = "http://localhost:5023/" | ||
connection = PCloudDummyConnection | ||
|
||
class JsonAPIProtocol(object): | ||
name = "api" | ||
endpoint = "https://api.pcloud.com/" | ||
connection = PCloudJSONConnection | ||
|
||
class JsonEAPIProtocol(object): | ||
name = "eapi" | ||
endpoint = "https://eapi.pcloud.com/" | ||
connection = PCloudJSONConnection | ||
|
||
class BinAPIProtocol(object): | ||
name = "binapi" | ||
endpoint = "https://binapi.pcloud.com/" | ||
connection = PCloudBinaryConnection | ||
|
||
class BinEAPIProtocol(object): | ||
name = "bineapi" | ||
endpoint = "https://bineapi.pcloud.com/" | ||
connection = PCloudBinaryConnection | ||
|
||
class NearestProtocol(object): | ||
name = "nearest" | ||
endpoint = "" | ||
connection = PCloudJSONConnection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters