1
- #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
- r"""
4
- @DATE: 2024/4/7 16:51
5
- @File: http.py
6
- @IDE: pycharm
7
- @Description:
8
- http会话对象
9
1
"""
2
+ @author: cunyue
3
+ @file: __init__.py
4
+ @time: 2025/6/16 13:29
5
+ @description: swanlab 客户端,负责发送 http 请求
6
+ """
7
+
10
8
import json
11
9
from datetime import datetime
12
10
from typing import Optional , Tuple , Dict , Union , List , AnyStr
17
15
from swankit .log import FONT
18
16
from urllib3 .util .retry import Retry
19
17
18
+ from swanlab .api import LoginInfo , ProjectInfo , ExperimentInfo
19
+ from swanlab .api .auth .login import login_by_key
20
20
from swanlab .error import NetworkError , ApiError
21
21
from swanlab .log import swanlog
22
22
from swanlab .package import get_package_version
23
- from .auth .login import login_by_key
24
23
from .cos import CosClient
25
- from .info import LoginInfo , ProjectInfo , ExperimentInfo
26
24
27
25
28
26
def decode_response (resp : requests .Response ) -> Union [Dict , AnyStr , List ]:
@@ -38,7 +36,7 @@ def decode_response(resp: requests.Response) -> Union[Dict, AnyStr, List]:
38
36
return resp .text
39
37
40
38
41
- class HTTP :
39
+ class Client :
42
40
"""
43
41
封装请求函数,添加get、post、put、delete方法
44
42
"""
@@ -344,38 +342,38 @@ def _():
344
342
FONT .loading ("Updating experiment status..." , _ )
345
343
346
344
347
- http : Optional ["HTTP " ] = None
345
+ client : Optional ["Client " ] = None
348
346
"""
349
- 一个进程只有一个http请求对象
347
+ 一个进程只有一个客户端对象
350
348
"""
351
349
352
350
353
- def create_http (login_info : LoginInfo ) -> HTTP :
351
+ def create_client (login_info : LoginInfo ) -> Client :
354
352
"""
355
- 创建http请求对象
353
+ 创建客户端对象
356
354
"""
357
- global http
358
- http = HTTP (login_info )
359
- return http
355
+ global client
356
+ client = Client (login_info )
357
+ return client
360
358
361
359
362
- def get_http () -> HTTP :
360
+ def get_client () -> Client :
363
361
"""
364
- 创建http请求对象
365
- :return: http请求对象
362
+ 获取客户端对象
363
+ :return: client
366
364
"""
367
- global http
368
- if http is None :
369
- raise ValueError ("http object is not initialized" )
370
- return http
365
+ global client
366
+ if client is None :
367
+ raise ValueError ("client object is not initialized" )
368
+ return client
371
369
372
370
373
- def reset_http ():
371
+ def reset_client ():
374
372
"""
375
- 重置http对象
373
+ 重置client对象
376
374
"""
377
- global http
378
- http = None
375
+ global client
376
+ client = None
379
377
380
378
381
379
def sync_error_handler (func ):
@@ -396,3 +394,14 @@ def wrapper(*args, **kwargs) -> Tuple[Optional[Union[dict, str]], Optional[Excep
396
394
return None , e
397
395
398
396
return wrapper
397
+
398
+
399
+ __all__ = [
400
+ "get_client" ,
401
+ "reset_client" ,
402
+ "create_client" ,
403
+ "sync_error_handler" ,
404
+ "decode_response" ,
405
+ "CosClient" ,
406
+ "Client" ,
407
+ ]
0 commit comments