-
Notifications
You must be signed in to change notification settings - Fork 0
feat: download contract API #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
WalkthroughA new tool for downloading contract files from SpotDraft via API v2.1 is introduced. This includes a tool definition, request interface, and handler function. Supporting changes add a Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant ToolHandler
participant SpotDraftClient
participant SpotDraftAPI
User->>ToolHandler: Request contract download (composite_id)
ToolHandler->>SpotDraftClient: downloadFile(endpoint)
SpotDraftClient->>SpotDraftAPI: POST /v2.1/contracts/{composite_id}/download
SpotDraftAPI-->>SpotDraftClient: File response (binary, headers)
SpotDraftClient-->>ToolHandler: { data (base64), filename, contentType }
ToolHandler-->>User: Return file info (base64, filename, contentType)
Estimated code review effort2 (~18 minutes) Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/spotdraft_client.ts (1)
66-69
: Improve optional chaining for filename extraction.The filename extraction can be simplified using optional chaining as suggested by the static analysis tool.
- if (contentDisposition) { - const filenameMatch = contentDisposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/); - if (filenameMatch && filenameMatch[1]) { - filename = decodeURIComponent(filenameMatch[1].replace(/['"]/g, '')); - } - } + if (contentDisposition) { + const filenameMatch = contentDisposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/); + if (filenameMatch?.[1]) { + filename = decodeURIComponent(filenameMatch[1].replace(/['"]/g, '')); + } + }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/contract/get_contract_download.ts
(1 hunks)src/index.ts
(3 hunks)src/spotdraft_client.ts
(1 hunks)
🧬 Code Graph Analysis (2)
src/index.ts (1)
src/contract/get_contract_download.ts (3)
getContractDownloadTool
(54-54)getContractDownload
(54-54)GetContractDownloadRequest
(54-54)
src/contract/get_contract_download.ts (1)
src/spotdraft_client.ts (1)
SpotDraftClient
(1-80)
🪛 Biome (1.9.4)
src/spotdraft_client.ts
[error] 67-67: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
🧰 Additional context used
🧬 Code Graph Analysis (2)
src/index.ts (1)
src/contract/get_contract_download.ts (3)
getContractDownloadTool
(54-54)getContractDownload
(54-54)GetContractDownloadRequest
(54-54)
src/contract/get_contract_download.ts (1)
src/spotdraft_client.ts (1)
SpotDraftClient
(1-80)
🪛 Biome (1.9.4)
src/spotdraft_client.ts
[error] 67-67: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
🔇 Additional comments (8)
src/spotdraft_client.ts (1)
60-62
: Good implementation for binary data handling.The conversion from ArrayBuffer to base64 using Buffer is appropriate for handling binary file downloads.
src/index.ts (3)
20-24
: LGTM - Clean import integration.The import statements follow the established pattern and are properly structured.
77-77
: LGTM - Tool properly added to the registry.The new tool is correctly added to the tools array following the existing pattern.
128-130
: LGTM - Handler case properly implemented.The switch case follows the established pattern with appropriate type casting and async handling.
src/contract/get_contract_download.ts (4)
4-40
: Excellent tool documentation and schema definition.The tool description is comprehensive and provides clear guidance on:
- Parameter requirements with examples
- Response structure explanation
- Detailed usage instructions for models
- Security considerations (not attempting to decode binary data)
This level of documentation will help ensure proper usage of the tool.
42-44
: Interface definition is clean and appropriate.The interface correctly defines the required
composite_id
parameter with proper typing.
46-52
: Implementation correctly delegates to the client method.The function properly constructs the API v2.1 endpoint and delegates to the
downloadFile
method. The endpoint path follows the expected SpotDraft API pattern.
54-54
: All necessary exports are properly defined.The exports include the function, interface, and tool definition needed for integration.
async downloadFile(endpoint: string): Promise<{ data: string; filename?: string; contentType?: string }> { | ||
const response = await this.post(endpoint, {}); | ||
|
||
if (!response.ok) { | ||
const errorBody = await response.text(); | ||
throw new Error(`SpotDraft API Error: ${response.status} ${response.statusText} - ${errorBody}`); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix critical logic error in downloadFile method.
The method calls this.post(endpoint, {})
which returns a parsed JSON response, but then tries to check response.ok
on that result. The post()
method already handles response validation and returns the parsed response body, not the raw Response object.
async downloadFile(endpoint: string): Promise<{ data: string; filename?: string; contentType?: string }> {
- const response = await this.post(endpoint, {});
-
- if (!response.ok) {
- const errorBody = await response.text();
- throw new Error(`SpotDraft API Error: ${response.status} ${response.statusText} - ${errorBody}`);
- }
+ const response = await fetch(`${this.baseUrl}${endpoint}`, {
+ method: 'POST',
+ headers: this.headers,
+ body: JSON.stringify({}),
+ });
+
+ if (!response.ok) {
+ const errorBody = await response.text();
+ throw new Error(`SpotDraft API Error: ${response.status} ${response.statusText} - ${errorBody}`);
+ }
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
async downloadFile(endpoint: string): Promise<{ data: string; filename?: string; contentType?: string }> { | |
const response = await this.post(endpoint, {}); | |
if (!response.ok) { | |
const errorBody = await response.text(); | |
throw new Error(`SpotDraft API Error: ${response.status} ${response.statusText} - ${errorBody}`); | |
} | |
async downloadFile(endpoint: string): Promise<{ data: string; filename?: string; contentType?: string }> { | |
const response = await fetch(`${this.baseUrl}${endpoint}`, { | |
method: 'POST', | |
headers: this.headers, | |
body: JSON.stringify({}), | |
}); | |
if (!response.ok) { | |
const errorBody = await response.text(); | |
throw new Error(`SpotDraft API Error: ${response.status} ${response.statusText} - ${errorBody}`); | |
} |
🤖 Prompt for AI Agents
In src/spotdraft_client.ts around lines 52 to 58, the downloadFile method
incorrectly treats the result of this.post(endpoint, {}) as a raw Response
object and checks response.ok, but this.post returns parsed JSON and already
handles response validation. Remove the check for response.ok and the error
handling based on it, and directly use the returned parsed response data as
intended.
Summary by CodeRabbit