"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const util_1=require("util"),code_builder_1=require("../../helpers/code-builder"),utils_1=require("../../../../utils"),code_helpers_1=require("../../helpers/code-helpers"),__1=require("../.."),constants_1=require("../../helpers/constants"),constants_2=require("../../../../../constants"),handler=function(e,t,{target:s,client:a}){var r,o,n,i;const l=Object.assign({indent:"  "},t),{level:u=0}=l,p=new code_builder_1.CodeBuilder({indentation:l.indent,variablesPrefix:l.variablesPrefix,lang:constants_1.Lang.PYTHON});l.withComments&&addComments(p),l.withImports&&p.push("import requests").blank();let d,h,c=!1,_=e.allHeaders;if(null===(r=e.securityOAuth2ExtraCalls)||void 0===r?void 0:r.length){const t=new __1.HTTPSnippet(null===(o=e.securityOAuth2ExtraCalls)||void 0===o?void 0:o[0]).convert(s,a,Object.assign(Object.assign({},l),{withImports:!1,withComments:!1,variablesPrefix:"oauth2_"}));p.push(t),p.blank(),_=_||{},_.Authorization='"Bearer " + oauth2_data["access_token"]',c=!0}if((0,code_helpers_1.printUrlVariablesDeclarations)(e,p),p.push(`${p.var("url").toLowerCase()} = %s`,(0,code_helpers_1.buildUrlExpression)(e,p)).blank(),Object.keys(e.queryObj||{}).length){d="query = "+jsObjectToPythonDict(e.queryObj,l.indent),p.push(d).blank()}const b=(0,utils_1.normalizeMimeType)(null===(n=e.postData)||void 0===n?void 0:n.mimeType);if(e.postData)switch(b){case constants_2.MediaTypes.URL_ENCODED:h=e.postData.paramsObj?`${(0,code_helpers_1.addIndentation)(jsObjectToPythonDict(e.postData.paramsObj,l.indent),{level:0,firstLine:!1})}`:e.postData.text;break;case constants_2.MediaTypes.JSON:e.postData.jsonObj&&(h=(0,code_helpers_1.addIndentation)(jsObjectToPythonDict(e.postData.jsonObj,l.indent),{level:0,firstLine:!1}));break;case constants_2.MediaTypes.MULTIPART:p.unshift("# multipart/form-data is not supported yet\n");break;default:e.postData.text&&(h=`'''\n${(0,code_helpers_1.addIndentation)(e.postData.text.trim(),{level:1,indent:l.indent,firstLine:!0})}\n'''`)}let v;h&&p.push(`${p.var("payload").toLowerCase()} = %s`,h).blank();const C=Object.keys(_).length,$=(e,t)=>"Authorization"===e&&c?t:`"${t}"`;if(1===C)for(v in _)p.push(`${p.var("headers").toLowerCase()} = {"${v}": ${$(v,_[v])}}`).blank();else if(C>1){let e=1;for(v in p.push(`${p.var("headers").toLowerCase()} = {`),_)e++!==C?p.push(1,`"${v}": ${$(v,_[v])},`):p.push(1,`"${v}": ${$(v,_[v])}`);p.push(0,"}").blank()}const f=e.method.toLowerCase();let O=(0,util_1.format)(`${p.var("response").toLowerCase()} = requests.${f}(${p.var("url").toLowerCase()}`);if(h){O+=`, ${b===constants_2.MediaTypes.JSON?"json":"data"}=${p.var("payload").toLowerCase()}`}return C>0&&(O+=`, headers=${p.var("headers").toLowerCase()}`),d&&(O+=`, params=${p.var("query").toLowerCase()}`),e.basicAuth&&(O+=`, auth=('${e.basicAuth.username}','${e.basicAuth.password}')`),O+=")",p.push(O).blank(),(null===(i=e.allResponseCodes)||void 0===i?void 0:i.includes("204"))?p.push("if response.status_code == 204:").push(u+1,'print("success")').push("else:").push(u+1,`${p.var("data").toLowerCase()} = ${p.var("response").toLowerCase()}.json()`).push(u+1,`print(${p.var("data").toLowerCase()})`):p.push(`${p.var("data").toLowerCase()} = ${p.var("response").toLowerCase()}.json()`).push(`print(${p.var("data").toLowerCase()})`),p.join()};function addComments(e){e.push('"""'),e.push("Requires python >= 3"),e.blank(),e.push('Requires module "requests" >= 2.25.1'),e.push("See here for installation details:"),e.push("  https://requests.readthedocs.io/"),e.push('"""'),e.blank()}function jsObjectToPythonDict(e,t){return JSON.stringify(e,(function(e,t){return e&&"boolean"==typeof t?t?"__REDOCLY_<True>__":"__REDOCLY_<False>__":null===t?"__REDOCLY_<None>__":t}),t).replace(/"__REDOCLY_<(\w+)>__"/g,"$1")}exports.default=handler;
//# sourceMappingURL=requests.js.map