Skip to content

Commit db262d1

Browse files
committed
refactor auth, error handling in commands and unittests
1 parent 60b2a57 commit db262d1

22 files changed

Lines changed: 345 additions & 514 deletions

cli115/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
__title__ = "115cli"
22
__description__ = "An unofficial CLI tool and higher-level Python API client for 115.com cloud storage service"
33
__url__ = "https://github.com/Xavier-Lam/115cli"
4-
__version__ = "0.1.0"
4+
__version__ = "0.1.1"
55
__author__ = "Xavier-Lam"
66
__author_email__ = "xavierlam7@hotmail.com"

cli115/client/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
from cli115.client.base import (
2-
AccountClient,
32
Client,
43
Directory,
5-
DownloadClient,
64
File,
7-
FileClient,
85
FileSystemEntry,
96
SortField,
107
SortOrder,
@@ -13,12 +10,9 @@
1310
from cli115.client.factory import create_client
1411

1512
__all__ = [
16-
"AccountClient",
1713
"Client",
1814
"Directory",
19-
"DownloadClient",
2015
"File",
21-
"FileClient",
2216
"FileSystemEntry",
2317
"SortField",
2418
"SortOrder",

cli115/client/base.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,6 @@ def path(self):
151151
val = self.__dict__.get("path")
152152
if val is not None:
153153
return val
154-
if self._file_client is None:
155-
return None
156154
parts = [self.name]
157155
parent_id = self.parent_id
158156
while parent_id and parent_id != "0":

cli115/cmds/account.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from __future__ import annotations
44

55
import argparse
6-
import sys
76

87
from cli115.cmds.base import BaseCommand
98
from cli115.cmds.formatter import PairFormatterMixin
@@ -18,11 +17,7 @@ def register(self, parser: argparse.ArgumentParser) -> None:
1817
def execute(self, args: argparse.Namespace) -> None:
1918
client = self._create_client()
2019

21-
try:
22-
info = client.account.info()
23-
except Exception as e:
24-
print(f"Error: {e}", file=sys.stderr)
25-
sys.exit(1)
20+
info = client.account.info()
2621

2722
pairs = [
2823
("Username", info.user_name),

cli115/cmds/auth.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import sys
77
from http.cookies import SimpleCookie
88

9+
from cli115.auth.cookie import CookieAuth
10+
from cli115.client import create_client
911
from cli115.cmds.base import BaseCommand
1012
from cli115.cmds.config import save_cookie_credential
1113

@@ -44,9 +46,18 @@ def execute(self, args: argparse.Namespace) -> None:
4446
)
4547
sys.exit(1)
4648

47-
cred_path = save_cookie_credential(args.user, cookies)
48-
print(f"Credential saved to {cred_path}")
49-
print(f"Active credential set to cookie_{args.user}.json")
49+
# Validate the saved cookie by calling account info
50+
auth = CookieAuth(
51+
uid=cookies["UID"],
52+
cid=cookies["CID"],
53+
seid=cookies["SEID"],
54+
kid=cookies["KID"],
55+
)
56+
client = create_client(auth)
57+
account = client.account.info()
58+
print(f"Authenticated as {account.user_name} (User ID: {account.user_id})")
59+
60+
save_cookie_credential(args.user, cookies)
5061

5162

5263
def _parse_cookie_string(cookie_str: str) -> dict[str, str]:

cli115/cmds/cp.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,10 @@ def execute(self, args: argparse.Namespace) -> None:
2727
*src_paths, dst_path = args.paths
2828
client = self._create_client()
2929

30-
try:
31-
if len(src_paths) == 1:
32-
client.file.copy(src_paths[0], dst_path)
33-
else:
34-
client.file.batch_copy(*src_paths, dest_dir=dst_path)
35-
except Exception as e:
36-
print(f"Error: {e}", file=sys.stderr)
37-
sys.exit(1)
30+
if len(src_paths) == 1:
31+
client.file.copy(src_paths[0], dst_path)
32+
else:
33+
client.file.batch_copy(*src_paths, dest_dir=dst_path)
3834

3935
for src in src_paths:
4036
print(f"Copied: {src} -> {dst_path}")

cli115/cmds/download.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,7 @@ class DownloadQuotaCommand(PairFormatterMixin, BaseCommand):
6868

6969
def execute(self, args: argparse.Namespace) -> None:
7070
client = self._create_client()
71-
try:
72-
quota = client.download.quota()
73-
except Exception as e:
74-
print(f"Error: {e}", file=sys.stderr)
75-
sys.exit(1)
71+
quota = client.download.quota()
7672
self.output(
7773
[("Remaining", quota.quota), ("Total", quota.total)],
7874
args,
@@ -92,11 +88,7 @@ def execute(self, args: argparse.Namespace) -> None:
9288
user_specified_page = args.page is not None
9389
page = args.page if args.page is not None else 1
9490
client = self._create_client()
95-
try:
96-
tasks, pagination = client.download.list(page)
97-
except Exception as e:
98-
print(f"Error: {e}", file=sys.stderr)
99-
sys.exit(1)
91+
tasks, pagination = client.download.list(page)
10092
records = [_task_record(t) for t in tasks]
10193
self.output(records, args)
10294
if not user_specified_page and pagination.total > pagination.limit:
@@ -123,15 +115,11 @@ def execute(self, args: argparse.Namespace) -> None:
123115
client = self._create_client()
124116
urls = args.urls
125117
dest_dir = getattr(args, "dest", None)
126-
try:
127-
if len(urls) == 1:
128-
task = client.download.add_url(urls[0], dest_dir=dest_dir)
129-
tasks = [task]
130-
else:
131-
tasks = client.download.add_urls(*urls, dest_dir=dest_dir)
132-
except Exception as e:
133-
print(f"Error: {e}", file=sys.stderr)
134-
sys.exit(1)
118+
if len(urls) == 1:
119+
task = client.download.add_url(urls[0], dest_dir=dest_dir)
120+
tasks = [task]
121+
else:
122+
tasks = client.download.add_urls(*urls, dest_dir=dest_dir)
135123
records = [_task_record(t) for t in tasks]
136124
self.output(records, args)
137125

@@ -146,10 +134,6 @@ def register(self, parser: argparse.ArgumentParser) -> None:
146134

147135
def execute(self, args: argparse.Namespace) -> None:
148136
client = self._create_client()
149-
try:
150-
client.download.delete(*args.hashes)
151-
except Exception as e:
152-
print(f"Error: {e}", file=sys.stderr)
153-
sys.exit(1)
137+
client.download.delete(*args.hashes)
154138
for h in args.hashes:
155139
print(f"Deleted: {h}")

cli115/cmds/download_info.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import argparse
66
import shlex
7-
import sys
87

98
from cli115.cmds.base import BaseCommand
109
from cli115.cmds.config import load_config
@@ -65,12 +64,8 @@ def register(self, parser: argparse.ArgumentParser) -> None:
6564
)
6665

6766
def execute(self, args: argparse.Namespace) -> None:
68-
try:
69-
client = self._create_client()
70-
info = client.file.download_info(args.path)
71-
except Exception as e:
72-
print(f"Error: {e}", file=sys.stderr)
73-
sys.exit(1)
67+
client = self._create_client()
68+
info = client.file.download_info(args.path)
7469

7570
pairs = [
7671
("url", info.url),

cli115/cmds/find.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ def _find_record(entry: FileSystemEntry) -> list[tuple[str, object]]:
1919
size = "-"
2020
ftype = "dir"
2121
mtime = (
22-
entry.modified_time.strftime("%Y-%m-%d %H:%M")
23-
if entry.modified_time
24-
else "-"
22+
entry.modified_time.strftime("%Y-%m-%d %H:%M") if entry.modified_time else "-"
2523
)
2624
return [
2725
("Name", entry.name + ("/" if entry.is_directory else "")),
@@ -38,7 +36,9 @@ class FindCommand(ListFormatterMixin, BaseCommand):
3836
def register(self, parser: argparse.ArgumentParser) -> None:
3937
super().register(parser)
4038
parser.add_argument(
41-
"path", nargs="?", default=None,
39+
"path",
40+
nargs="?",
41+
default=None,
4242
help="Directory to search within (default: global search)",
4343
)
4444
parser.add_argument("keyword", help="Search keyword")
@@ -55,16 +55,12 @@ def execute(self, args: argparse.Namespace) -> None:
5555
limit = args.limit if args.limit is not None else DEFAULT_PAGE_SIZE
5656

5757
client = self._create_client()
58-
try:
59-
entries, pagination = client.file.find(
60-
args.keyword,
61-
path=args.path,
62-
limit=limit,
63-
offset=offset,
64-
)
65-
except Exception as e:
66-
print(f"Error: {e}", file=sys.stderr)
67-
sys.exit(1)
58+
entries, pagination = client.file.find(
59+
args.keyword,
60+
path=args.path,
61+
limit=limit,
62+
offset=offset,
63+
)
6864

6965
records = [_find_record(e) for e in entries]
7066
self.output(records, args)

cli115/cmds/id.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from __future__ import annotations
44

55
import argparse
6-
import sys
76

87
from cli115.cmds.base import BaseCommand
98
from cli115.cmds.formatter import format_entry, PairFormatterMixin
@@ -19,10 +18,6 @@ def register(self, parser: argparse.ArgumentParser) -> None:
1918
def execute(self, args: argparse.Namespace) -> None:
2019
client = self._create_client()
2120

22-
try:
23-
entry = client.file.id(args.file_id)
24-
except Exception as e:
25-
print(f"Error: {e}", file=sys.stderr)
26-
sys.exit(1)
21+
entry = client.file.id(args.file_id)
2722

2823
self.output(format_entry(entry), args)

0 commit comments

Comments
 (0)