Add first version for usage as CLI Tool

NOT FINISHED!
This commit is contained in:
2024-02-25 12:37:47 +00:00
parent bec839e98e
commit 17f3f29c76
7 changed files with 125 additions and 127 deletions
+63 -99
View File
@@ -1,115 +1,79 @@
import configparser
import logging
import argparse
import json
from pprint import pprint
from ActiveCollabAPI import AC_LOGIN_BASE_URL
from ActiveCollabAPI.ACAuthenticator import ACAuthenticator
from ActiveCollabAPI.ACClient import ACClient
from ActiveCollabAPI.ACTokenAuthenticator import ACTokenAuthenticator
# Enabling debugging at http.client level (requests->urllib3->http.client)
# you will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
# the only thing missing will be the response.body which is not logged.
from http.client import HTTPConnection
from ActiveCollabAPI.ActiveCollab import ActiveCollab
HTTPConnection.debuglevel = 1
logging.basicConfig() # you need to initialize logging, otherwise you will not see anything from requests
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
def run(args, config):
# run the command
ac = ActiveCollab(config["base_url"])
ac.login_to_first_account(config["username"], config["password"])
output = None
if args.object == "info":
output = run_info(ac, args)
if args.object == "account":
output = run_account(ac, args)
if args.object == "project":
output = run_project(ac, args, output)
return output
def run_account(ac, args):
if args.command == "list":
return ac.session.to_dict()
def run_project(ac, args, output):
if args.command == "list":
output = list(map(lambda p: p.to_dict(), ac.get_projects()))
return output
def run_info(ac, args):
output = ac.get_info()
return output
def serialize_output(output):
# serialize the output
return json.dumps(output)
def load_config(args):
with open(args.config, "r", encoding="utf8") as fh:
config = json.load(fh)
return config
def main():
config = configparser.ConfigParser()
config.read('config.ini')
# parse arguments
parser = argparse.ArgumentParser(
prog='activecollab',
description='This is a CLI Client to Active-Collab Service',
epilog='This is not a offical tool provided by the AC Guys!')
parser.add_argument('-c', '--config', required=True,
help="use the named config file")
parser.add_argument('-v', '--version', action='store_true',
help="show version information for this tool")
username = config['LOGIN']['username']
password = config['LOGIN']['password']
account_name = config['LOGIN']['account_name']
subparsers = parser.add_subparsers(help='sub-command help')
# 1. you need to log in to get a session and a list of accounts
ac = ActiveCollab(AC_LOGIN_BASE_URL)
session = ac.login(username, password)
account = ac.get_account(session) # default returning first account
token = ac.get_token(account, session.user)
# account list
parser_account = subparsers.add_parser('account', help='account help')
parser_account.set_defaults(object='account')
parser_account.add_argument('command', choices=["list"], help='list all accessible accounts')
# 2. you need to get a token for this account
token = ACTokenAuthenticator(account, auth.user).issue_token_intent()
# pprint(token)
# project list get
parser_project = subparsers.add_parser('project', help='project help')
parser_project.set_defaults(object='project')
parser_project.add_argument('command', choices=["list"], help='what to do with the account help')
# 3. now you can query the API
ac = ACClient(account, token)
args = parser.parse_args()
# check version
res = ac.get_info()
ac_version = res.json()
pprint(ac_version)
if ac_version['version'] != '7.4.337':
raise Exception("Supporting only Version 7.4.337!")
# get a list of all projects
# res = ac.get_projects()
# pprint(res.json())
# get task with sub-tasks
# "https://app.activecollab.com/416910/projects/310/tasks/6260"
# project_id = 310
# task_id = 6260
# task_res = ac.get_project_task(project_id, task_id)
# pprint(task_res.json())
# task = task_res.json()
# ac.delete_all_open_subtasks(task)
# create a new subtask for the task without assignment
# subtask = {
# "task_id": task_id,
# "project_id": project_id,
# "assignee_id": "",
# "body": "test qqq"
# }
# res = ac.create_subtask(subtask)
# pprint(res.json())
# # bulk create subtasks
# subtask_texts = []
# with open("sub-tasks.txt", "r", encoding="utf-8") as fh:
# for line in fh:
# subtask_texts.append(line.rstrip('\n'))
# create
# for text in subtask_texts:
# subtask['body'] = text
# ac.create_subtask(subtask)
# delete
# for text in subtask_texts:
# subtask = ac.find_subtask_with_body(task, text)
# if subtask is not None:
# print(subtask)
# ac.delete_subtask(subtask)
# # unassign a single subtask
# subtask_id = 12203
# subtask_res = ac.get_project_task_subtask(project_id, task_id, subtask_id)
# # pprint(subtask_res.json())
# subtask = subtask_res.json()['single']
# res = ac.unassign_sub_task(subtask)
# pprint(res.json())
# delete a subtask
# subtask = {
# "task_id": task_id,
# "project_id": project_id,
# "id": 13764
# }
# res = ac.delete_subtask(subtask)
# pprint(res.json())
res = ac.get_projects()
if res.status_code == 200:
pprint(res.json())
config = load_config(args)
output = run(args, config)
print(serialize_output(output))
if __name__ == "__main__":