Skip to content

Commit 11800c1

Browse files
committed
fix: update API authentication and request format
- Change base URL to https://api.pspdfkit.com - Use Bearer token authentication instead of X-Api-Key - Send instructions as JSON string in form data - Update Direct API to use Build API internally - Add Path object support to file handlers - Fix tests passing: 14/22 tests now working
1 parent df81f7a commit 11800c1

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

src/nutrient/client.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def _process_file(
7777
"""Process a file using the Direct API.
7878
7979
This is the internal method used by all Direct API methods.
80+
It internally uses the Build API with a single action.
8081
8182
Args:
8283
tool: The tool identifier from the API.
@@ -91,23 +92,10 @@ def _process_file(
9192
AuthenticationError: If API key is missing or invalid.
9293
APIError: For other API errors.
9394
"""
94-
# Prepare file for upload
95-
file_field, file_data = prepare_file_for_upload(input_file)
96-
files = {file_field: file_data}
97-
98-
# Prepare form data with options
99-
data = {k: str(v) for k, v in options.items() if v is not None}
100-
101-
# Make API request
102-
endpoint = f"/process/{tool}"
103-
result = self._http_client.post(endpoint, files=files, data=data)
104-
105-
# Handle output
106-
if output_path:
107-
save_file_output(result, output_path)
108-
return None
109-
else:
110-
return result
95+
# Use the builder API with a single step
96+
builder = self.build(input_file)
97+
builder.add_step(tool, options)
98+
return builder.execute(output_path)
11199

112100
def close(self) -> None:
113101
"""Close the HTTP client session."""

src/nutrient/file_handler.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pathlib import Path
66
from typing import BinaryIO, Generator, Optional, Tuple, Union
77

8-
FileInput = Union[str, bytes, BinaryIO]
8+
FileInput = Union[str, Path, bytes, BinaryIO]
99

1010
# Default chunk size for streaming operations (1MB)
1111
DEFAULT_CHUNK_SIZE = 1024 * 1024
@@ -24,7 +24,12 @@ def prepare_file_input(file_input: FileInput) -> Tuple[bytes, str]:
2424
FileNotFoundError: If file path doesn't exist.
2525
ValueError: If input type is not supported.
2626
"""
27-
if isinstance(file_input, str):
27+
# Handle Path objects
28+
if isinstance(file_input, Path):
29+
if not file_input.exists():
30+
raise FileNotFoundError(f"File not found: {file_input}")
31+
return file_input.read_bytes(), file_input.name
32+
elif isinstance(file_input, str):
2833
path = Path(file_input)
2934
if not path.exists():
3035
raise FileNotFoundError(f"File not found: {file_input}")
@@ -63,6 +68,10 @@ def prepare_file_for_upload(
6368
"""
6469
content_type = "application/octet-stream"
6570

71+
# Handle Path objects
72+
if isinstance(file_input, Path):
73+
file_input = str(file_input)
74+
6675
if isinstance(file_input, str):
6776
path = Path(file_input)
6877
if not path.exists():

src/nutrient/http_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __init__(self, api_key: Optional[str], timeout: int = 300) -> None:
2626
self._api_key = api_key
2727
self._timeout = timeout
2828
self._session = self._create_session()
29-
self._base_url = "https://www.nutrient.io/api/processor-api"
29+
self._base_url = "https://api.pspdfkit.com"
3030

3131
def _create_session(self) -> requests.Session:
3232
"""Create requests session with retry logic."""
@@ -53,7 +53,7 @@ def _create_session(self) -> requests.Session:
5353
"User-Agent": "nutrient-python-client/0.1.0",
5454
}
5555
if self._api_key:
56-
headers["X-Api-Key"] = self._api_key
56+
headers["Authorization"] = f"Bearer {self._api_key}"
5757

5858
session.headers.update(headers)
5959

@@ -144,7 +144,7 @@ def post(
144144
# Prepare multipart data if json_data is provided
145145
prepared_data = data or {}
146146
if json_data is not None:
147-
prepared_data["actions"] = (None, json.dumps(json_data), "application/json")
147+
prepared_data["instructions"] = json.dumps(json_data)
148148

149149
try:
150150
response = self._session.post(

0 commit comments

Comments
 (0)