Skip to content
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

amberdata: Use gzip compression for requests #236

Merged
merged 1 commit into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/api/amberdata/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export async function fetchAssetPrice(
headers: {
accept: "application/json",
"x-api-key": apiKey,
"Accept-Encoding": "gzip",
},
},
)
Expand Down
28 changes: 2 additions & 26 deletions app/api/proxy/amberdata/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ const AMBERDATA_BASE_URL = "https://api.amberdata.com"
const API_KEY_HEADER = "x-api-key"

export async function GET(request: NextRequest) {
const startTime = performance.now()
let fetchStartTime: number = 0
let fetchEndTime: number = 0

try {
// Get the path and search params from the request
const { searchParams } = new URL(request.url)
Expand All @@ -30,47 +26,27 @@ export async function GET(request: NextRequest) {
amberdataUrl.searchParams.append(key, value)
}
})
fetchStartTime = performance.now()
const response = await fetch(amberdataUrl, {
headers: {
[API_KEY_HEADER]: process.env.AMBERDATA_API_KEY as string,
"Accept-Encoding": "gzip, deflate, br",
"Accept-Encoding": "gzip",
},
})
fetchEndTime = performance.now()

if (!response.ok) {
throw new Error(`Amberdata API responded with status: ${response.status}`)
}

const endTime = performance.now()

// Log timing information
console.log({
totalTime: `${(endTime - startTime).toFixed(2)}ms`,
fetchTime: `${(fetchEndTime - fetchStartTime).toFixed(2)}ms`,
path,
})

return new NextResponse(response.body, {
status: response.status,
headers: {
"Content-Type":
response.headers.get("Content-Type") || "application/json",
"Content-Encoding": response.headers.get("Content-Encoding") || "",
"X-Total-Time": (endTime - startTime).toFixed(2),
"X-Fetch-Time": (fetchEndTime - fetchStartTime).toFixed(2),
},
})
} catch (error) {
const endTime = performance.now()
console.error("Error in Amberdata proxy:", error, {
totalTime: `${(endTime - startTime).toFixed(2)}ms`,
fetchTime:
fetchStartTime && fetchEndTime
? `${(fetchEndTime - fetchStartTime).toFixed(2)}ms`
: "N/A",
})
console.error("Error in Amberdata proxy:", error)

return NextResponse.json(
{ error: "An error occurred while processing your request" },
Expand Down
1 change: 1 addition & 0 deletions app/api/savings/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ async function amberdataRequest(
API_KEY_HEADER,
process.env.AMBERDATA_API_KEY as string,
)
amberdataReq.headers.set("Accept-Encoding", "gzip")

return amberdataReq
}
Expand Down