Skip to content

support clob and blob object handling #7

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

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Changes from 1 commit
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
64 changes: 33 additions & 31 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,35 +95,34 @@ export class ScrapflyClient {
/**
* Handle clob and blob large objects
*/
async handleLargeObjects(result: any): Promise<ScrapeResult> {
const format = result.format
if (format === 'clob' || format === 'blob') {
let response: Response;
try {
const url = new URL(result.content);
const params = { key: this.key };
url.search = new URLSearchParams(params).toString();
response = await this.fetch({
url: url.toString(),
method: 'GET',
headers: {
'user-agent': this.ua,
'accept-encoding': 'gzip, deflate, br',
accept: 'application/json',
},
});
} catch (e) {
log.error('error', e);
throw e;
}
const content: string = await response.text();
result.content = content
if (format === 'clob') {
result.format = 'text'
}
if (format === 'blob') {
result.format = 'binary'
}
async handleLargeObjects(result: any, format: "clob" | "blob"): Promise<ScrapeResult> {
let response: Response;

try {
const url = new URL(result.content);
const params = { key: this.key };
url.search = new URLSearchParams(params).toString();
response = await this.fetch({
url: url.toString(),
method: 'GET',
headers: {
'user-agent': this.ua,
'accept-encoding': 'gzip, deflate, br',
accept: 'application/json',
},
});
} catch (e) {
log.error('error', e);
throw e;
}

const content: string = await response.text();
result.content = content
if (format === 'clob') {
result.format = 'text'
}
if (format === 'blob') {
result.format = 'binary'
}
return result
}
Expand Down Expand Up @@ -210,8 +209,11 @@ export class ScrapflyClient {
throw new errors.ApiHttpClientError(JSON.stringify(data));
}

const content = await this.handleLargeObjects(data.result)
data.result = content
const content_format = data.result.format
if (content_format === 'clob' || content_format === 'blob') {
const content = await this.handleLargeObjects(data.result, content_format)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be simplified

Suggested change
const content = await this.handleLargeObjects(data.result, content_format)
data.result = await this.handleLargeObjects(data.result, content_format)

data.result = content
}

const result = this.handleResponse(
response,
Expand Down
Loading