Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work and test #345

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env python

import setuptools

if __name__ == "__main__":
setuptools.setup()
23 changes: 19 additions & 4 deletions src/xero/filesmanager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import requests
from urllib.parse import parse_qs

import requests
from xero.auth import OAuth2Credentials

from .constants import XERO_FILES_URL
from .exceptions import (
XeroBadRequest,
Expand All @@ -14,6 +16,7 @@
XeroRateLimitExceeded,
XeroUnauthorized,
XeroUnsupportedMediaType,
XeroTenantIdNotSet,
)


Expand Down Expand Up @@ -67,6 +70,14 @@ def wrapper(*args, **kwargs):
uri, params, method, body, headers, singleobject, files = func(
*args, **kwargs
)
if headers is None:
headers = {}

if isinstance(self.credentials, OAuth2Credentials):
if self.credentials.tenant_id:
headers["Xero-tenant-id"] = self.credentials.tenant_id
else:
raise XeroTenantIdNotSet

response = getattr(requests, method)(
uri,
Expand Down Expand Up @@ -169,15 +180,19 @@ def _delete(self, id):
uri = "/".join([self.base_url, self.name, id])
return uri, {}, "delete", None, None, False, None

def _upload_file(self, path, folderId=None):
def _upload_file(self, path=None, file=None, filename=None, folderId=None):

if folderId is not None:
uri = "/".join([self.base_url, self.name, folderId])
else:
uri = "/".join([self.base_url, self.name])
filename = self.filename(path)

files = dict()
files[filename] = open(path, mode="rb")
if path:
filename = self.filename(path)
files[filename] = open(path, mode="rb")
elif file and filename:
files[filename] = file

return uri, {}, "post", None, None, False, files

Expand Down
34 changes: 34 additions & 0 deletions tests/test_filesmanager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import unittest
from time import time
from unittest.mock import patch

from xero import Xero
from xero.auth import OAuth2Credentials


class FilesManagerTest(unittest.TestCase):
def setUp(self):
super().setUp()
# Create an expired token to be used by tests
self.expired_token = {
"access_token": "1234567890",
"expires_in": 1800,
"token_type": "Bearer",
"refresh_token": "0987654321",
"expires_at": time(),
}

@patch("requests.get")
def test_tenant_is_used_in_xero_request(self, r_get):
credentials = OAuth2Credentials(
"client_id", "client_secret", token=self.expired_token, tenant_id="12345"
)
xero = Xero(credentials)
# Just return any old response
r_get.return_value = None
try:
xero.filesAPI.files.all()
except: # NOQA: E722
pass

self.assertEqual(r_get.call_args[1]["headers"]["Xero-tenant-id"], "12345")
Loading