Add get projects
This commit is contained in:
@@ -3,6 +3,8 @@ import json
|
|||||||
import requests
|
import requests
|
||||||
|
|
||||||
from ActiveCollabAPI import AC_USER_AGENT, AC_API_VERSION
|
from ActiveCollabAPI import AC_USER_AGENT, AC_API_VERSION
|
||||||
|
from ActiveCollabAPI.AcAccount import AcAccount
|
||||||
|
from ActiveCollabAPI.AcToken import AcToken
|
||||||
|
|
||||||
|
|
||||||
class ACClient:
|
class ACClient:
|
||||||
@@ -10,17 +12,17 @@ class ACClient:
|
|||||||
account = None
|
account = None
|
||||||
token = None
|
token = None
|
||||||
|
|
||||||
def __init__(self, account, token):
|
def __init__(self, account: AcAccount, token: AcToken):
|
||||||
self.account = account
|
self.account = account
|
||||||
self.token = token
|
self.token = token
|
||||||
#
|
#
|
||||||
self.base_url = self.account['url'] + '/api/v%d' % AC_API_VERSION
|
self.base_url = self.account.url + '/api/v%d' % AC_API_VERSION
|
||||||
|
|
||||||
def headers(self):
|
def headers(self):
|
||||||
return {
|
return {
|
||||||
'Content-Type': 'application/json; charset=utf8',
|
'Content-Type': 'application/json; charset=utf8',
|
||||||
'Accept': 'application/json',
|
'Accept': 'application/json',
|
||||||
'X-Angie-AuthApiToken': self.token,
|
'X-Angie-AuthApiToken': self.token.token,
|
||||||
'User-Agent': AC_USER_AGENT
|
'User-Agent': AC_USER_AGENT
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from ActiveCollabAPI.AcUser import AcUser
|
||||||
|
from ActiveCollabAPI.AcAccount import AcAccount
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class AcLoginResponse:
|
||||||
|
user: AcUser
|
||||||
|
accounts: list[AcAccount]
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class AcProject:
|
||||||
|
based_on_id: int
|
||||||
|
based_on_type: str
|
||||||
|
body: str
|
||||||
|
body_formatted: str
|
||||||
|
budget: float
|
||||||
|
budget_type: str
|
||||||
|
budgeting_interval: str
|
||||||
|
category_id: int
|
||||||
|
class_: str
|
||||||
|
company_id: int
|
||||||
|
completed_by_id: int
|
||||||
|
completed_on: int
|
||||||
|
count_discussions: int
|
||||||
|
count_files: int
|
||||||
|
count_notes: int
|
||||||
|
count_tasks: int
|
||||||
|
created_by_email: str
|
||||||
|
created_by_id: int
|
||||||
|
created_by_name: str
|
||||||
|
created_on: int
|
||||||
|
currency_id: int
|
||||||
|
email: str
|
||||||
|
file_size: int
|
||||||
|
id: int
|
||||||
|
is_billable: bool
|
||||||
|
is_client_reporting_enabled: bool
|
||||||
|
is_completed: bool
|
||||||
|
is_estimate_visible_to_subcontractors: bool
|
||||||
|
is_sample: bool
|
||||||
|
is_tracking_enabled: bool
|
||||||
|
is_trashed: bool
|
||||||
|
label_id: int
|
||||||
|
last_activity_on: int
|
||||||
|
leader_id: int
|
||||||
|
members: list[int]
|
||||||
|
members_can_change_billable: bool
|
||||||
|
name: str
|
||||||
|
project_number: int
|
||||||
|
trashed_by_id: int
|
||||||
|
trashed_on: int
|
||||||
|
updated_by_id: int
|
||||||
|
updated_on: int
|
||||||
|
url_path: str
|
||||||
|
|
||||||
|
|
||||||
|
def project_from_json(json_obj: dict) -> AcProject:
|
||||||
|
json_obj["class_"] = json_obj["class"]
|
||||||
|
del json_obj["class"]
|
||||||
|
return AcProject(**json_obj)
|
||||||
@@ -2,22 +2,23 @@ from dataclasses import dataclass
|
|||||||
|
|
||||||
from ActiveCollabAPI import AC_API_VERSION
|
from ActiveCollabAPI import AC_API_VERSION
|
||||||
from ActiveCollabAPI.ACAuthenticator import ACAuthenticator
|
from ActiveCollabAPI.ACAuthenticator import ACAuthenticator
|
||||||
|
from ActiveCollabAPI.ACClient import ACClient
|
||||||
from ActiveCollabAPI.ACTokenAuthenticator import ACTokenAuthenticator
|
from ActiveCollabAPI.ACTokenAuthenticator import ACTokenAuthenticator
|
||||||
from ActiveCollabAPI.AcAccount import AcAccount
|
from ActiveCollabAPI.AcAccount import AcAccount
|
||||||
|
from ActiveCollabAPI.AcLoginResponse import AcLoginResponse
|
||||||
|
from ActiveCollabAPI.AcProject import AcProject, project_from_json
|
||||||
from ActiveCollabAPI.AcSession import AcSession
|
from ActiveCollabAPI.AcSession import AcSession
|
||||||
from ActiveCollabAPI.AcToken import AcToken
|
from ActiveCollabAPI.AcToken import AcToken
|
||||||
from ActiveCollabAPI.AcUser import AcUser
|
from ActiveCollabAPI.AcUser import AcUser
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class AcLoginResponse:
|
|
||||||
user: AcUser
|
|
||||||
accounts: list[AcAccount]
|
|
||||||
|
|
||||||
|
|
||||||
class ActiveCollab:
|
class ActiveCollab:
|
||||||
|
|
||||||
base_url: str = ""
|
base_url: str = ""
|
||||||
|
|
||||||
|
session: AcSession = None
|
||||||
|
|
||||||
def __init__(self, base_url: str):
|
def __init__(self, base_url: str):
|
||||||
self.base_url = base_url
|
self.base_url = base_url
|
||||||
|
|
||||||
@@ -25,8 +26,8 @@ class ActiveCollab:
|
|||||||
login_res = self.user_login(email, password)
|
login_res = self.user_login(email, password)
|
||||||
cur_account = self.select_first_account(login_res.accounts)
|
cur_account = self.select_first_account(login_res.accounts)
|
||||||
token = self.create_token(cur_account, login_res.user)
|
token = self.create_token(cur_account, login_res.user)
|
||||||
session = AcSession(login_res.user, login_res.accounts, cur_account, token)
|
self.session = AcSession(login_res.user, login_res.accounts, cur_account, token)
|
||||||
return session
|
return self.session
|
||||||
|
|
||||||
def user_login(self, email: str, password: str) -> AcLoginResponse:
|
def user_login(self, email: str, password: str) -> AcLoginResponse:
|
||||||
auth = ACAuthenticator(self.base_url)
|
auth = ACAuthenticator(self.base_url)
|
||||||
@@ -59,4 +60,10 @@ class ActiveCollab:
|
|||||||
return AcToken(res_data["token"])
|
return AcToken(res_data["token"])
|
||||||
|
|
||||||
def get_projects(self):
|
def get_projects(self):
|
||||||
pass
|
client = ACClient(self.session.cur_account, self.session.token)
|
||||||
|
res = client.get_projects()
|
||||||
|
if res.status_code != 200:
|
||||||
|
raise Exception("Error %d" % res.status_code)
|
||||||
|
res_data = res.json()
|
||||||
|
projects = list(map(lambda p: project_from_json(p), res_data))
|
||||||
|
return projects
|
||||||
|
|||||||
@@ -13,10 +13,15 @@ class Test_Integration_Login(TestCase):
|
|||||||
def test_login_success(self):
|
def test_login_success(self):
|
||||||
ac = ActiveCollab(config["base_url"])
|
ac = ActiveCollab(config["base_url"])
|
||||||
session = ac.login_to_first_account(config["username"], config["password"])
|
session = ac.login_to_first_account(config["username"], config["password"])
|
||||||
pprint(session)
|
|
||||||
|
|
||||||
self.assertEqual(config["first_name"], session.user.first_name)
|
self.assertEqual(config["first_name"], session.user.first_name)
|
||||||
self.assertEqual(config["last_name"], session.user.last_name)
|
self.assertEqual(config["last_name"], session.user.last_name)
|
||||||
|
|
||||||
self.assertEqual(1, len(session.accounts))
|
self.assertEqual(1, len(session.accounts))
|
||||||
self.assertGreater(len(session.token.token), 5)
|
self.assertGreater(len(session.token.token), 5)
|
||||||
|
|
||||||
|
def test_get_projects(self):
|
||||||
|
ac = ActiveCollab(config["base_url"])
|
||||||
|
ac.login_to_first_account(config["username"], config["password"])
|
||||||
|
projects = ac.get_projects()
|
||||||
|
self.assertGreater(len(projects), 0)
|
||||||
|
self.assertEqual('Project', projects[0].class_)
|
||||||
|
self.assertGreater(projects[0].id, 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user