pixel-client 1.2.0__tar.gz → 1.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pixel-client
3
- Version: 1.2.0
3
+ Version: 1.2.2
4
4
  Summary: Client library for Geodata Pixel
5
5
  Project-URL: Documentation, https://doc.geodatapixel.no/
6
6
  Project-URL: Organization, https://geodata.no
@@ -12,8 +12,10 @@ Classifier: Programming Language :: Python :: 3
12
12
  Requires-Python: >=3.11
13
13
  Requires-Dist: geojson-pydantic>=1.2.0
14
14
  Requires-Dist: httpx>=0.28.1
15
+ Requires-Dist: packaging>=25.0
15
16
  Requires-Dist: pydantic-settings>=2.8.0
16
17
  Requires-Dist: pydantic[email]>=2.10.6
18
+ Requires-Dist: tenacity>=9.1.2
17
19
  Requires-Dist: tqdm>=4.67.1
18
20
  Description-Content-Type: text/markdown
19
21
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pixel-client"
3
- version = "1.2.0"
3
+ version = "1.2.2"
4
4
  description = "Client library for Geodata Pixel"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.11"
@@ -23,6 +23,8 @@ dependencies = [
23
23
  "pydantic[email]>=2.10.6",
24
24
  "pydantic-settings>=2.8.0",
25
25
  "tqdm>=4.67.1",
26
+ "packaging>=25.0",
27
+ "tenacity>=9.1.2",
26
28
  ]
27
29
 
28
30
  [project.urls]
@@ -56,3 +58,8 @@ docs = [
56
58
  include = [
57
59
  "src/*"
58
60
  ]
61
+
62
+
63
+ [tool.basedpyright]
64
+ include = ["src"]
65
+ typeCheckingMode = "basic"
@@ -5,10 +5,12 @@ from functools import partial
5
5
  from pathlib import Path
6
6
  from typing import Any, AsyncGenerator, Literal, TypeVar, Self, TypedDict, Unpack
7
7
  from uuid import uuid4
8
-
8
+ import tenacity
9
9
  import httpx
10
10
  from geojson_pydantic import Polygon
11
- from tqdm import tqdm
11
+ import tqdm
12
+ from tqdm.asyncio import tqdm_asyncio
13
+
12
14
 
13
15
  from .auth import KeyCloakAuth
14
16
  from .exceptions import (
@@ -53,10 +55,13 @@ DEFAULT_PIXEL_TIMEOUT = httpx.Timeout(
53
55
  write=15.0,
54
56
  )
55
57
  DEFAULT_UPLOAD_TIMEOUT = httpx.Timeout(
56
- 10.0,
58
+ 30.0,
57
59
  write=300.0, # Increased write timeout for uploads
58
60
  )
59
61
 
62
+ DEFAULT_UPLOAD_RETRIES = 8
63
+ DEFAULT_MAX_CONCURRENCY = 10 # Maximum number of concurrent uploads
64
+
60
65
 
61
66
  class PixelClientKwargs(TypedDict, total=False):
62
67
  """TypedDict for PixelClient constructor arguments."""
@@ -67,6 +72,12 @@ class PixelClientKwargs(TypedDict, total=False):
67
72
  timeout_upload: httpx.Timeout
68
73
  """Timeout configuration for file uploads. Defaults to DEFAULT_UPLOAD_TIMEOUT"""
69
74
 
75
+ upload_num_retries: int
76
+ """Number of retries for upload operations. Defaults to DEFAULT_UPLOAD_RETRIES."""
77
+
78
+ upload_max_concurrency: int
79
+ """Maximum number of concurrent uploads. Defaults to 10."""
80
+
70
81
 
71
82
  class PixelClientAsync:
72
83
  """Asynchronous client for interacting with the Pixel API.
@@ -86,6 +97,8 @@ class PixelClientAsync:
86
97
  password: str,
87
98
  timeout_pixel: httpx.Timeout = DEFAULT_PIXEL_TIMEOUT,
88
99
  timeout_upload: httpx.Timeout = DEFAULT_UPLOAD_TIMEOUT,
100
+ upload_num_retries: int = DEFAULT_UPLOAD_RETRIES,
101
+ upload_max_concurrency: int = DEFAULT_MAX_CONCURRENCY,
89
102
  ):
90
103
  """Initialize a new Pixel API client.
91
104
 
@@ -111,6 +124,17 @@ class PixelClientAsync:
111
124
  self._internal_client = httpx.AsyncClient(
112
125
  timeout=self._timeout_upload, # Use a separate client for uploads
113
126
  ) # For s3 uploads
127
+ self._semaphore: asyncio.Semaphore = asyncio.Semaphore(upload_max_concurrency)
128
+
129
+ self._retry_policy = tenacity.AsyncRetrying(
130
+ stop=tenacity.stop_after_attempt(upload_num_retries),
131
+ wait=tenacity.wait_random_exponential(),
132
+ before=tenacity.before_log(logger, logging.DEBUG),
133
+ after=tenacity.after_log(logger, logging.DEBUG),
134
+ )
135
+ self._internal_client.request = self._retry_policy.wraps(
136
+ self._internal_client.request
137
+ )
114
138
 
115
139
  @classmethod
116
140
  def from_settings(
@@ -1099,61 +1123,43 @@ class PixelClientAsync:
1099
1123
  Raises:
1100
1124
  httpx.HTTPStatusError: If the upload fails.
1101
1125
  """
1102
- response = await self._internal_client.post(
1103
- upload_job["url"],
1104
- files={"file": file_path.read_bytes()},
1105
- data=upload_job["upload_fields"],
1106
- )
1107
- response.raise_for_status()
1108
- return None
1109
-
1110
- async def _upload_part_to_s3(
1111
- self,
1112
- part: dict,
1113
- part_data: bytes,
1114
- ):
1115
- """Upload a part of a file to S3 as part of a multipart upload.
1116
-
1117
- Args:
1118
- part: Part object containing the S3 URL and headers.
1119
- part_data: Binary data for this part of the file.
1120
-
1121
- Returns:
1122
- None: This method returns None on success.
1123
-
1124
- Raises:
1125
- httpx.HTTPStatusError: If the upload fails.
1126
- """
1127
- response = await self._internal_client.put(
1128
- part["url"], content=part_data, headers=part["headers"]
1129
- )
1130
- response.raise_for_status()
1126
+ async with self._semaphore:
1127
+ response = await self._internal_client.post(
1128
+ upload_job["url"],
1129
+ files={"file": file_path.read_bytes()},
1130
+ data=upload_job["upload_fields"],
1131
+ )
1132
+ response.raise_for_status()
1131
1133
  return None
1132
1134
 
1133
- async def _upload_multipart_to_s3(
1134
- self,
1135
- upload_job: dict,
1136
- file_path: Path,
1137
- multipart_part_size: int,
1138
- ):
1139
- """Upload all parts of a file to S3 using a multipart upload.
1140
1135
 
1141
- Args:
1142
- upload_job: Upload job object containing the parts information.
1143
- file_path: Path to the file to upload.
1144
- multipart_part_size: Size of each part in bytes.
1145
-
1146
- Returns:
1147
- None: This method returns None on success.
1148
- """
1136
+ async def _upload_multipart_to_s3(self, upload_job: dict, file_path: Path, multipart_part_size: int):
1149
1137
  parts = sorted(upload_job["upload_parts"], key=lambda p: p["part_number"])
1150
- upload_iterator = zip(iter_file_parts(file_path, multipart_part_size), parts)
1151
- await asyncio.gather(
1152
- *[
1153
- self._upload_part_to_s3(part, part_data)
1154
- for (_, _, part_data), part in upload_iterator
1155
- ]
1156
- )
1138
+
1139
+
1140
+ async def read_and_upload_single_part(part_index, part):
1141
+
1142
+ async with self._semaphore:
1143
+ with open(file_path, 'rb') as f:
1144
+ f.seek(part_index * multipart_part_size)
1145
+ part_data = f.read(multipart_part_size)
1146
+
1147
+ response = await self._internal_client.put(
1148
+ part["url"], content=part_data, headers=part["headers"]
1149
+ )
1150
+ response.raise_for_status()
1151
+
1152
+
1153
+
1154
+ tasks = [
1155
+ read_and_upload_single_part(i, part)
1156
+ for i, part in enumerate(parts)
1157
+ ]
1158
+
1159
+ with silence_logger(logging.getLogger("httpx")):
1160
+ await tqdm_asyncio.gather(*tasks, total=len(tasks),
1161
+ desc=f"Uploading {file_path.name} to Pixel")
1162
+
1157
1163
  return None
1158
1164
 
1159
1165
  async def _upload_to_s3(
@@ -1888,7 +1894,7 @@ class PixelClientAsync:
1888
1894
  num_jobs = job["total_jobs"]
1889
1895
  with (
1890
1896
  silence_logger(logging.getLogger("httpx")),
1891
- tqdm(total=num_jobs, desc=f"Running group job {group_job_id}") as pbar,
1897
+ tqdm.tqdm(total=num_jobs, desc=f"Running group job {group_job_id}") as pbar,
1892
1898
  ):
1893
1899
  while not job["completed"]:
1894
1900
  job = await self.get_job_group(group_job_id)
File without changes
File without changes
File without changes