import{format}from"util";import{CodeBuilder}from"../../helpers/code-builder";import{normalizeMimeType}from"../../../../utils";import{addIndentation,buildUrlExpression,printUrlVariablesDeclarations}from"../../helpers/code-helpers";import{HTTPSnippet}from"../..";import{Lang}from"../../helpers/constants";import{MediaTypes}from"../../../../../constants";const handler=function(e,t,{target:s,client:a}){var o,r,n,i;const l=Object.assign({indent:"  "},t),{level:p=0}=l,u=new CodeBuilder({indentation:l.indent,variablesPrefix:l.variablesPrefix,lang:Lang.PYTHON});l.withComments&&addComments(u),l.withImports&&u.push("import requests").blank();let d,h,c=!1,m=e.allHeaders;if(null===(o=e.securityOAuth2ExtraCalls)||void 0===o?void 0:o.length){const t=new HTTPSnippet(null===(r=e.securityOAuth2ExtraCalls)||void 0===r?void 0:r[0]).convert(s,a,Object.assign(Object.assign({},l),{withImports:!1,withComments:!1,variablesPrefix:"oauth2_"}));u.push(t),u.blank(),m=m||{},m.Authorization='"Bearer " + oauth2_data["access_token"]',c=!0}if(printUrlVariablesDeclarations(e,u),u.push(`${u.var("url").toLowerCase()} = %s`,buildUrlExpression(e,u)).blank(),Object.keys(e.queryObj||{}).length){d="query = "+jsObjectToPythonDict(e.queryObj,l.indent),u.push(d).blank()}const b=normalizeMimeType(null===(n=e.postData)||void 0===n?void 0:n.mimeType);if(e.postData)switch(b){case MediaTypes.URL_ENCODED:h=e.postData.paramsObj?`${addIndentation(jsObjectToPythonDict(e.postData.paramsObj,l.indent),{level:0,firstLine:!1})}`:e.postData.text;break;case MediaTypes.JSON:e.postData.jsonObj&&(h=addIndentation(jsObjectToPythonDict(e.postData.jsonObj,l.indent),{level:0,firstLine:!1}));break;case MediaTypes.MULTIPART:u.unshift("# multipart/form-data is not supported yet\n");break;default:e.postData.text&&(h=`'''\n${addIndentation(e.postData.text.trim(),{level:1,indent:l.indent,firstLine:!0})}\n'''`)}let f;h&&u.push(`${u.var("payload").toLowerCase()} = %s`,h).blank();const v=Object.keys(m).length,C=(e,t)=>"Authorization"===e&&c?t:`"${t}"`;if(1===v)for(f in m)u.push(`${u.var("headers").toLowerCase()} = {"${f}": ${C(f,m[f])}}`).blank();else if(v>1){let e=1;for(f in u.push(`${u.var("headers").toLowerCase()} = {`),m)e++!==v?u.push(1,`"${f}": ${C(f,m[f])},`):u.push(1,`"${f}": ${C(f,m[f])}`);u.push(0,"}").blank()}const $=e.method.toLowerCase();let L=format(`${u.var("response").toLowerCase()} = requests.${$}(${u.var("url").toLowerCase()}`);if(h){L+=`, ${b===MediaTypes.JSON?"json":"data"}=${u.var("payload").toLowerCase()}`}return v>0&&(L+=`, headers=${u.var("headers").toLowerCase()}`),d&&(L+=`, params=${u.var("query").toLowerCase()}`),e.basicAuth&&(L+=`, auth=('${e.basicAuth.username}','${e.basicAuth.password}')`),L+=")",u.push(L).blank(),(null===(i=e.allResponseCodes)||void 0===i?void 0:i.includes("204"))?u.push("if response.status_code == 204:").push(p+1,'print("success")').push("else:").push(p+1,`${u.var("data").toLowerCase()} = ${u.var("response").toLowerCase()}.json()`).push(p+1,`print(${u.var("data").toLowerCase()})`):u.push(`${u.var("data").toLowerCase()} = ${u.var("response").toLowerCase()}.json()`).push(`print(${u.var("data").toLowerCase()})`),u.join()};export default handler;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")}
//# sourceMappingURL=requests.js.map