Skip to content

Commit df699bc

Browse files
doublefacedoubleface
doubleface
authored andcommittedMay 23, 2024
feat: Allow to use a custom requestInstance in solveCaptcha
1 parent 2bec3f0 commit df699bc

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed
 

‎packages/cozy-konnector-libs/src/libs/solveCaptcha.js

+32-20
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ const solveCaptcha = async (params = {}) => {
5454
const defaultParams = {
5555
type: 'recaptcha',
5656
timeout: DEFAULT_TIMEOUT,
57-
withFullSolution: false
57+
withFullSolution: false,
58+
requestInstance: request
5859
}
5960
let solution
6061
let resultAttribute = 'gRecaptchaResponse'
@@ -69,7 +70,8 @@ const solveCaptcha = async (params = {}) => {
6970
solution = await solveWithAntiCaptcha(
7071
{ websiteKey, websiteURL, type: 'NoCaptchaTaskProxyless' },
7172
params.timeout,
72-
secrets
73+
secrets,
74+
params
7375
)
7476
} else if (params.type === 'recaptchav3') {
7577
checkMandatoryParams(params, [
@@ -88,7 +90,8 @@ const solveCaptcha = async (params = {}) => {
8890
type: 'RecaptchaV3TaskProxyless'
8991
},
9092
params.timeout,
91-
secrets
93+
secrets,
94+
params
9295
)
9396
} else if (params.type === 'hcaptcha') {
9497
checkMandatoryParams(params, ['websiteKey', 'websiteURL'])
@@ -100,7 +103,8 @@ const solveCaptcha = async (params = {}) => {
100103
type: 'HCaptchaTaskProxyless'
101104
},
102105
params.timeout,
103-
secrets
106+
secrets,
107+
params
104108
)
105109
} else if (params.type === 'image') {
106110
checkMandatoryParams(params, ['body'])
@@ -109,7 +113,8 @@ const solveCaptcha = async (params = {}) => {
109113
solution = await solveWithAntiCaptcha(
110114
{ body: params.body, type: 'ImageToTextTask' },
111115
params.timeout,
112-
secrets
116+
secrets,
117+
params
113118
)
114119
}
115120
if (params.withFullSolution) {
@@ -132,7 +137,8 @@ function checkMandatoryParams(params = {}, mandatoryParams = []) {
132137
async function solveWithAntiCaptcha(
133138
taskParams,
134139
timeout = DEFAULT_TIMEOUT,
135-
secrets
140+
secrets,
141+
params
136142
) {
137143
const antiCaptchaApiUrl = 'https://api.anti-captcha.com'
138144
let gRecaptchaResponse = null
@@ -142,23 +148,29 @@ async function solveWithAntiCaptcha(
142148
const clientKey = secrets.antiCaptchaClientKey
143149
if (clientKey) {
144150
log('debug', ' Creating captcha resolution task...')
145-
const task = await request.post(`${antiCaptchaApiUrl}/createTask`, {
146-
body: {
147-
clientKey,
148-
task: taskParams
149-
},
150-
json: true
151-
})
151+
const task = await params.requestInstance.post(
152+
`${antiCaptchaApiUrl}/createTask`,
153+
{
154+
body: {
155+
clientKey,
156+
task: taskParams
157+
},
158+
json: true
159+
}
160+
)
152161
if (task && task.taskId) {
153162
log('debug', ` Task id : ${task.taskId}`)
154163
while (!gRecaptchaResponse) {
155-
const resp = await request.post(`${antiCaptchaApiUrl}/getTaskResult`, {
156-
body: {
157-
clientKey,
158-
taskId: task.taskId
159-
},
160-
json: true
161-
})
164+
const resp = await params.requestInstance.post(
165+
`${antiCaptchaApiUrl}/getTaskResult`,
166+
{
167+
body: {
168+
clientKey,
169+
taskId: task.taskId
170+
},
171+
json: true
172+
}
173+
)
162174
if (resp.status === 'ready') {
163175
if (resp.errorId) {
164176
log('error', `Anticaptcha error: ${JSON.stringify(resp)}`)

0 commit comments

Comments
 (0)
Please sign in to comment.