@fern-api/python-dynamic-snippets 4.46.8 → 4.46.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -17,7 +17,7 @@ ${l}`);this.isAtStartOfLine()&&(s=l+s),i&&(s+=`
17
17
  `);if(s.length<=1){e.write(this.escapeString(s[0]??"")),e.write('"""');return}a&&e.writeNoIndent(`\\
18
18
  `),s.forEach((E,b)=>{e.writeNoIndent(this.escapeString(E)),b===s.length-1?l&&e.writeNoIndent(`\\
19
19
  `):e.writeNoIndent(`
20
- `)}),e.writeNoIndent('"""')}escapeString(e){let i=/(?<!\\)(["'\\\t\n\r])/g,a={'"':'\\"',"'":"\\'","\\":"\\\\"," ":"\\t","\n":"\\n","\r":"\\r"};return e.replace(i,l=>a[l]??l)}writeUnknown({writer:e,value:i}){switch(typeof i){case"boolean":e.write(i?"True":"False");return;case"string":e.write(`"${this.escapeString(i)}"`);return;case"number":e.write(i.toString());return;case"object":if(i==null){e.write("None");return}if(Array.isArray(i)){this.writeUnknownArray({writer:e,value:i});return}this.writeUnknownObject({writer:e,value:i});return;default:throw new Error(`Internal error; unsupported unknown type: ${typeof i}`)}}writeUnknownArray({writer:e,value:i}){if(i.length===0){e.write("[]");return}e.writeLine("["),e.indent();for(let a of i)e.writeNode(p.unknown(a)),e.writeLine(",");e.dedent(),e.write("]")}writeUnknownObject({writer:e,value:i}){let a=Object.entries(i);if(a.length===0){e.write("{}");return}e.writeLine("{"),e.indent();for(let[l,s]of a)e.write(`"${l}": `),e.writeNode(p.unknown(s)),e.writeLine(",");e.dedent(),e.write("}")}};function Na({entries:p}){return p.filter(t=>!ce.isNop(t.key)&&!ce.isNop(t.value))}function Sa({entries:p}){return p.filter(t=>!ce.isNop(t.value))}function Tt({values:p}){return p.filter(t=>!ce.isNop(t))}function Ba(p){return new be(p)}function Pa(p){return new rt(p)}function Fa(p){return new ke(p)}function Ca(p){return new se(p)}function ka(p){return new Te(p)}function La(p){return new Ae(p)}function Ua(p){return new Q(p)}function Ma(p){return new fe(p)}function Oa(p){return new ue(p)}function _a(p){return new ne(p)}function Da(p){return new Re(p)}function Va(p){return new Ne(p)}function $a(p){return new Se(p)}function ja(p){return new le(p)}function Wa(p){return new Ee(p)}function qa(p){return new Fe(p)}function Ga(p){return new pe(p)}function za(p){return new Pe(p)}function Ha(p){return new Ce(p)}c();m();d();var Qa="value",it=class{constructor({context:t}){y(this,"context");this.context=t}convert(t){if(t.value===null)return this.context.isNullable(t.typeReference)?x.TypeInstantiation.none():(this.context.errors.add({severity:P.Critical,message:"Expected non-null value, but got null"}),x.TypeInstantiation.nop());if(t.value===void 0)return x.TypeInstantiation.nop();switch(t.typeReference.type){case"list":return this.convertList({list:t.typeReference.value,value:t.value});case"literal":return this.convertLiteral({literalType:t.typeReference.value,value:t.value});case"map":return this.convertMap({map:t.typeReference,value:t.value});case"named":{let e=this.context.resolveNamedType({typeId:t.typeReference.value});return e==null?x.TypeInstantiation.nop():this.convertNamed({named:e,value:t.value,as:t.as})}case"optional":return this.convert({typeReference:t.typeReference.value,value:t.value,as:t.as});case"nullable":return this.convert({typeReference:t.typeReference.value,value:t.value,as:t.as});case"primitive":return this.convertPrimitive({primitive:t.typeReference.value,value:t.value,as:t.as});case"set":return this.convertSet({set:t.typeReference.value,value:t.value});case"unknown":return x.TypeInstantiation.unknown(t.value);default:C(t.typeReference)}}convertLiteral({literalType:t,value:e}){switch(t.type){case"boolean":{let i=this.context.getValueAsBoolean({value:e});return i==null?x.TypeInstantiation.nop():x.TypeInstantiation.bool(i)}case"string":{let i=this.context.getValueAsString({value:e});return i==null?x.TypeInstantiation.nop():x.TypeInstantiation.str(i)}default:C(t)}}convertList({list:t,value:e}){return Array.isArray(e)?x.TypeInstantiation.list(e.map((i,a)=>{this.context.errors.scope({index:a});try{return this.convert({typeReference:t,value:i})}finally{this.context.errors.unscope()}}),{multiline:!0}):(this.context.errors.add({severity:P.Critical,message:`Expected array but got: ${typeof e}`}),x.TypeInstantiation.nop())}convertSet({set:t,value:e}){return Array.isArray(e)?x.TypeInstantiation.list(e.map((i,a)=>{this.context.errors.scope({index:a});try{return this.convert({typeReference:t,value:i})}finally{this.context.errors.unscope()}}),{multiline:!0}):(this.context.errors.add({severity:P.Critical,message:`Expected array but got: ${typeof e}`}),x.TypeInstantiation.nop())}convertMap({map:t,value:e}){return typeof e!="object"||e==null?(this.context.errors.add({severity:P.Critical,message:`Expected object but got: ${e==null?"null":typeof e}`}),x.TypeInstantiation.nop()):x.TypeInstantiation.dict(Object.entries(e).map(([i,a])=>{this.context.errors.scope(i);try{return{key:this.convert({typeReference:t.key,value:i,as:"key"}),value:this.convert({typeReference:t.value,value:a})}}finally{this.context.errors.unscope()}}),{multiline:!0})}convertNamed({named:t,value:e,as:i}){switch(t.type){case"alias":return this.convert({typeReference:t.typeReference,value:e,as:i});case"discriminatedUnion":return this.convertDiscriminatedUnion({discriminatedUnion:t,value:e});case"enum":return this.convertEnum({enum_:t,value:e});case"object":return this.convertObject({object_:t,value:e});case"undiscriminatedUnion":return this.convertUndiscriminatedUnion({undiscriminatedUnion:t,value:e});default:C(t)}}convertDiscriminatedUnion({discriminatedUnion:t,value:e}){let i=this.context.resolveDiscriminatedUnionTypeInstance({discriminatedUnion:t,value:e});if(i==null)return x.TypeInstantiation.nop();let a=i.singleDiscriminatedUnionType,l=this.convertDiscriminatedUnionProperties({discriminatedUnionTypeInstance:i,unionVariant:a});if(l==null)return x.TypeInstantiation.nop();let s={name:this.context.getPropertyName(t.discriminant.name),value:x.TypeInstantiation.str(a.discriminantValue.wireValue)};return x.TypeInstantiation.typedDict([s,...l],{multiline:!0})}convertDiscriminatedUnionProperties({discriminatedUnionTypeInstance:t,unionVariant:e}){let i=this.getBaseFields({discriminatedUnionTypeInstance:t,singleDiscriminatedUnionType:e});switch(e.type){case"samePropertiesAsObject":{let a=this.context.resolveNamedType({typeId:e.typeId});if(a==null)return;let l=this.convertNamed({named:a,value:t.value});if(!l.isTypedDict()){this.context.errors.add({severity:P.Critical,message:"Internal error; expected union value to be an object"});return}let s=l.asTypedDictOrThrow();return[...i,...s.entries]}case"singleProperty":try{this.context.errors.scope(e.discriminantValue.wireValue);let a=this.context.getRecord(t.value);return a==null?[...i,{name:Qa,value:this.convert({typeReference:e.typeReference,value:t.value})}]:[...i,{name:this.context.getPropertyName(e.discriminantValue.name),value:this.convert({typeReference:e.typeReference,value:a[e.discriminantValue.wireValue]})}]}finally{this.context.errors.unscope()}case"noProperties":return i;default:C(e)}}getBaseFields({discriminatedUnionTypeInstance:t,singleDiscriminatedUnionType:e}){return this.context.associateByWireValue({parameters:e.properties??[],values:this.context.getRecord(t.value)??{},ignoreMissingParameters:!0}).map(a=>{this.context.errors.scope(a.name.wireValue);try{return{name:this.context.getPropertyName(a.name.name),value:this.convert(a)}}finally{this.context.errors.unscope()}})}convertObject({object_:t,value:e}){let i=this.context.associateByWireValue({parameters:t.properties,values:this.context.getRecord(e)??{}});return x.TypeInstantiation.typedDict(i.map(a=>{this.context.errors.scope(a.name.wireValue);try{return{name:this.context.getPropertyName(a.name.name),value:this.convert(a)}}finally{this.context.errors.unscope()}}),{multiline:!0})}convertEnum({enum_:t,value:e}){let i=this.getEnumValue({enum_:t,value:e});return i==null?x.TypeInstantiation.nop():x.TypeInstantiation.str(i)}getEnumValue({enum_:t,value:e}){if(typeof e!="string"){this.context.errors.add({severity:P.Critical,message:`Expected enum value string, got: ${typeof e}`});return}if(t.values.find(a=>a.wireValue===e)==null){this.context.errors.add({severity:P.Critical,message:`An enum value named "${e}" does not exist in this context`});return}return e}convertUndiscriminatedUnion({undiscriminatedUnion:t,value:e}){let i=this.findMatchingUndiscriminatedUnionType({undiscriminatedUnion:t,value:e});return i??x.TypeInstantiation.nop()}findMatchingUndiscriminatedUnionType({undiscriminatedUnion:t,value:e}){for(let i of t.types)try{return this.convert({typeReference:i,value:e})}catch{continue}this.context.errors.add({severity:P.Critical,message:`None of the types in the undiscriminated union matched the given "${typeof e}" value`})}convertPrimitive({primitive:t,value:e,as:i}){switch(t){case"INTEGER":case"LONG":case"UINT":case"UINT_64":{let a=this.getValueAsNumber({value:e,as:i});return a==null?x.TypeInstantiation.nop():x.TypeInstantiation.int(a)}case"FLOAT":case"DOUBLE":{let a=this.getValueAsNumber({value:e});return a==null?x.TypeInstantiation.nop():x.TypeInstantiation.float(a)}case"BOOLEAN":{let a=this.getValueAsBoolean({value:e,as:i});return a==null?x.TypeInstantiation.nop():x.TypeInstantiation.bool(a)}case"BASE_64":case"BIG_INTEGER":case"STRING":{let a=this.context.getValueAsString({value:e});return a==null?x.TypeInstantiation.nop():x.TypeInstantiation.str(a)}case"UUID":{let a=this.context.getValueAsString({value:e});return a==null?x.TypeInstantiation.nop():x.TypeInstantiation.uuid(a)}case"DATE":{let a=this.context.getValueAsString({value:e});return a==null?x.TypeInstantiation.nop():x.TypeInstantiation.date(a)}case"DATE_TIME":{let a=this.context.getValueAsString({value:e});return a==null?x.TypeInstantiation.nop():x.TypeInstantiation.datetime(a)}default:C(t)}}getValueAsNumber({value:t,as:e}){let i=e==="key"&&typeof t=="string"?Number(t):t;return this.context.getValueAsNumber({value:i})}getValueAsBoolean({value:t,as:e}){let i=e==="key"?typeof t=="string"?t==="true":t==="false"?!1:t:t;return this.context.getValueAsBoolean({value:i})}};c();m();d();var ot=class{constructor({context:t}){y(this,"context");this.context=t}getFilePropertyInfo({body:t,value:e}){let i={fileFields:[],bodyPropertyFields:[]},a=this.context.getRecord(e)??{};for(let l of t.properties)switch(l.type){case"file":{let s=this.getSingleFileProperty({property:l,record:a});if(x.TypeInstantiation.isNop(s))break;i.fileFields.push({name:this.context.getPropertyName(l.name),value:s});break}case"fileArray":{let s=this.getArrayFileProperty({property:l,record:a});if(x.TypeInstantiation.isNop(s))break;i.fileFields.push({name:this.context.getPropertyName(l.name),value:s});break}case"bodyProperty":{let s=this.getBodyProperty({property:l,record:a});if(x.TypeInstantiation.isNop(s))break;i.bodyPropertyFields.push({name:this.context.getPropertyName(l.name.name),value:s});break}default:C(l)}return i}getSingleFileProperty({property:t,record:e}){let i=this.context.getSingleFileValue({property:t,record:e});return i==null&&(i=`example_${t.wireValue??"file"}`),this.context.getFileFromString(i)}getArrayFileProperty({property:t,record:e}){let i=this.context.getFileArrayValues({property:t,record:e});return i==null?x.TypeInstantiation.nop():x.TypeInstantiation.list(i.map(a=>this.context.getFileFromString(a)))}getBodyProperty({property:t,record:e}){let i=e[t.name.wireValue];return i==null?x.TypeInstantiation.nop():this.context.dynamicTypeLiteralMapper.convert({typeReference:t.typeReference,value:i})}};var Ya=["list","set"],at=class p extends qe{constructor({ir:e,config:i}){super({ir:e,config:i});y(this,"ir");y(this,"customConfig");y(this,"dynamicTypeLiteralMapper");y(this,"filePropertyMapper");this.ir=e,this.customConfig=i.customConfig??{},this.dynamicTypeLiteralMapper=new it({context:this}),this.filePropertyMapper=new ot({context:this})}clone(){return new p({ir:this.ir,config:this.config})}getClassName(e){let i=e.pascalCase.safeName,a=this.getRootClientClassName();return i===a?`${a}Model`:i}getTypedDictClassName(e){return`${this.getClassName(e)}Params`}getPropertyName(e){let i=e.snakeCase.safeName;return i.startsWith("_")?"f_"+i.substring(i.lastIndexOf("_")+1):i}getMethodName(e){return Ya.includes(e.snakeCase.unsafeName)?e.snakeCase.unsafeName:e.snakeCase.safeName}getRootClientClassReference(){return x.reference({name:this.getRootClientClassName(),modulePath:this.getRootModulePath()})}getRootClientClassName(){return this.customConfig.client?.exported_class_name!=null?this.customConfig.client.exported_class_name:this.customConfig.client_class_name!=null?this.customConfig.client_class_name:this.customConfig.client?.class_name!=null?this.customConfig.client.class_name:this.pascalCase(this.config.organization)+this.pascalCase(this.config.workspaceName)}getEnvironmentClassName(){return`${this.getRootClientClassName()}Environment`}getEnvironmentTypeReferenceFromID(e){let i=this.resolveEnvironmentName(e);if(i!=null)return x.accessAttribute({lhs:this.getEnvironmentClassReference(),rhs:x.codeBlock(this.getEnvironmentEnumName(i))})}getEnvironmentClassReference(){return x.reference({name:this.getEnvironmentClassName(),modulePath:this.getEnvironmentModulePath()})}getEnvironmentEnumName(e){return e.screamingSnakeCase.safeName}isPrimitive(e){switch(e.type){case"primitive":return!0;case"optional":case"nullable":return this.isPrimitive(e.value);case"named":{let i=this.resolveNamedType({typeId:e.value});if(i==null)return!1;switch(i.type){case"alias":return this.isPrimitive(i.typeReference);case"discriminatedUnion":case"undiscriminatedUnion":case"object":case"enum":return!1;default:C(i)}break}case"list":case"set":case"map":case"literal":case"unknown":return!1;default:C(e)}}getFileFromString(e){return x.TypeInstantiation.str(e)}getRootModulePath(){if(this.customConfig.package_name!=null)return[this.customConfig.package_name];let e=this.cleanOrganizationName();return this.customConfig.use_api_name_in_package?[e,this.getApiName()]:[e]}getCoreModulePath(){return this.getRootModulePath().concat("core")}getEnvironmentModulePath(){return this.getRootModulePath().concat("environment")}shouldInlinePathParameters(){return!0}cleanOrganizationName(){return this.config.organization.replace(/[^a-zA-Z0-9]/g,"_")}getApiName(){return vt(this.config.workspaceName)}pascalCase(e){let i=wt(e);return i.charAt(0).toUpperCase()+i.slice(1)}};c();m();d();var Za={type:"primitive",value:"STRING"},Ja=["example"],pi="client",st="request",ut=class{constructor({context:t}){y(this,"context");this.context=t}async generateSnippet({endpoint:t,request:e}){return this.buildPythonFile({endpoint:t,snippet:e}).toString()}generateSnippetSync({endpoint:t,request:e}){return this.buildPythonFile({endpoint:t,snippet:e}).toString()}async generateSnippetAst({endpoint:t,request:e}){return this.buildPythonFile({endpoint:t,snippet:e})}generateMethodCallSnippetAst({endpoint:t,request:e}){return this.callMethod({endpoint:t,snippet:e})}buildPythonFile({endpoint:t,snippet:e}){return x.file({path:Ja,statements:[this.constructClient({endpoint:t,snippet:e}),this.callMethod({endpoint:t,snippet:e})]})}constructClient({endpoint:t,snippet:e}){return x.assign({lhs:x.reference({name:pi}),rhs:x.instantiateClass({classReference:this.context.getRootClientClassReference(),arguments_:this.getConstructorArgs({endpoint:t,snippet:e}).map(i=>x.methodArgument({name:i.name,value:i.value})),multiline:!0})})}getConstructorArgs({endpoint:t,snippet:e}){let i=[],a=this.getConstructorEnvironmentArgs({baseUrl:e.baseURL,environment:e.environment});return a.length>0&&i.push(...a),t.auth!=null&&(e.auth!=null?i.push(...this.getConstructorAuthArgs({auth:t.auth,values:e.auth})):this.context.errors.add({severity:P.Warning,message:`Auth with ${t.auth.type} configuration is required for this endpoint`})),this.context.errors.scope(Z.PathParameters),this.context.ir.pathParameters!=null&&i.push(...this.getPathParameters({namedParameters:this.context.ir.pathParameters,snippet:e})),this.context.errors.unscope(),this.context.errors.scope(Z.Headers),this.context.ir.headers!=null&&e.headers!=null&&i.push(...this.getConstructorHeaderArgs({headers:this.context.ir.headers,values:e.headers})),this.context.errors.unscope(),i}getConstructorEnvironmentArgs({baseUrl:t,environment:e}){let i=this.getEnvironmentValue({baseUrl:t,environment:e});return i==null?[]:e!=null&&this.context.isMultiEnvironmentValues(e)?[{name:"environment",value:i}]:[{name:this.getEnvironmentOptionName({environment:e}),value:i}]}getEnvironmentValue({baseUrl:t,environment:e}){if(t!=null&&e!=null){this.context.errors.add({severity:P.Critical,message:"Cannot specify both baseUrl and environment options"});return}if(t!=null)return x.TypeInstantiation.str(t);if(e!=null){if(this.context.isSingleEnvironmentID(e)){let i=this.context.getEnvironmentTypeReferenceFromID(e);if(i==null){this.context.errors.add({severity:P.Warning,message:`Environment ${JSON.stringify(e)} was not found`});return}return x.TypeInstantiation.reference(i)}if(this.context.isMultiEnvironmentValues(e))return this.context.validateMultiEnvironmentUrlValues(e)?x.TypeInstantiation.reference(x.instantiateClass({classReference:this.context.getEnvironmentClassReference(),arguments_:Object.entries(e).map(([i,a])=>({name:i,value:this.context.dynamicTypeLiteralMapper.convert({typeReference:Za,value:a})})).map(i=>x.methodArgument({name:i.name,value:i.value})),multiline:!0})):void 0}}getConstructorAuthArgs({auth:t,values:e}){switch(t.type){case"basic":return e.type!=="basic"?(this.addAuthMismatchError(t,e),[]):this.getConstructorBasicAuthArg({auth:t,values:e});case"bearer":return e.type!=="bearer"?(this.addAuthMismatchError(t,e),[]):this.getConstructorBearerAuthArgs({auth:t,values:e});case"header":return e.type!=="header"?(this.addAuthMismatchError(t,e),[]):this.getConstructorHeaderAuthArgs({auth:t,values:e});case"oauth":return e.type!=="oauth"?(this.addAuthMismatchError(t,e),[]):this.getConstructorOAuthArgs({auth:t,values:e});case"inferred":return e.type!=="inferred"?(this.addAuthMismatchError(t,e),[]):(this.addWarning("The Python SDK Generator does not support Inferred auth scheme yet"),[]);default:C(t)}}addAuthMismatchError(t,e){this.context.errors.add({severity:P.Critical,message:this.context.newAuthMismatchError({auth:t,values:e}).message})}addWarning(t){this.context.errors.add({severity:P.Warning,message:t})}getConstructorBasicAuthArg({auth:t,values:e}){return[{name:this.context.getPropertyName(t.username),value:x.TypeInstantiation.str(e.username)},{name:this.context.getPropertyName(t.password),value:x.TypeInstantiation.str(e.password)}]}getConstructorBearerAuthArgs({auth:t,values:e}){return[{name:this.context.getPropertyName(t.token),value:x.TypeInstantiation.str(e.token)}]}getConstructorHeaderAuthArgs({auth:t,values:e}){return[{name:this.context.getPropertyName(t.header.name.name),value:this.context.dynamicTypeLiteralMapper.convert({typeReference:t.header.typeReference,value:e.value})}]}getConstructorOAuthArgs({auth:t,values:e}){return[{name:this.context.getPropertyName(t.clientId),value:x.TypeInstantiation.str(e.clientId)},{name:this.context.getPropertyName(t.clientSecret),value:x.TypeInstantiation.str(e.clientSecret)}]}getConstructorHeaderArgs({headers:t,values:e}){let i=[];for(let a of t){let l=this.getConstructorHeaderArg({header:a,value:e.value});l!=null&&i.push(l)}return i}getConstructorHeaderArg({header:t,value:e}){let i=this.context.dynamicTypeLiteralMapper.convert({typeReference:t.typeReference,value:e});if(!x.TypeInstantiation.isNop(i))return{name:this.context.getPropertyName(t.name.name),value:i}}callMethod({endpoint:t,snippet:e}){return x.invokeMethod({on:x.reference({name:pi}),method:this.getMethod({endpoint:t}),arguments_:this.getMethodArgs({endpoint:t,snippet:e}).map(i=>x.methodArgument({name:i.name,value:i.value})),multiline:!0})}getMethodArgs({endpoint:t,snippet:e}){switch(t.request.type){case"inlined":return this.getMethodArgsForInlinedRequest({request:t.request,snippet:e});case"body":return this.getMethodArgsForBodyRequest({request:t.request,snippet:e});default:C(t.request)}}getMethodArgsForBodyRequest({request:t,snippet:e}){let i=[];this.context.errors.scope(Z.PathParameters);let a=[...this.context.ir.pathParameters??[],...t.pathParameters??[]],l=new Set;if(t.body!=null){let s=this.getBodyRequestArgs({body:t.body,value:e.requestBody});if(l=new Set(s.map(E=>E.name)),t.body.type==="typeReference"){let E=t.body.value;if(E.type==="named"){let b=this.context.resolveNamedType({typeId:E.value});if(b!=null&&b.type==="object")for(let A of b.properties)this.resolvesToLiteralType(A.typeReference)||l.add(this.context.getPropertyName(A.name.name))}}}if(a.length>0){let E=this.getPathParameters({namedParameters:a,snippet:e}).map(b=>l.has(b.name)?{...b,name:b.name+"_"}:b);i.push(...E)}return this.context.errors.unscope(),this.context.errors.scope(Z.RequestBody),t.body!=null&&i.push(...this.getBodyRequestArgs({body:t.body,value:e.requestBody})),this.context.errors.unscope(),i}getBodyRequestArgs({body:t,value:e}){switch(t.type){case"bytes":return[{name:st,value:this.getBytesBodyRequestTypeInstantiation({value:e})}];case"typeReference":return this.getBodyRequestArgsForTypeReference({typeReference:t.value,value:e});default:C(t)}}getBodyRequestArgsForTypeReference({typeReference:t,value:e}){switch(t.type){case"named":{let i=this.context.resolveNamedType({typeId:t.value});return i==null?[]:this.getBodyRequestArgsForNamedTypeReference({typeReference:t,named:i,value:e})}case"nullable":case"optional":{let i=t.value;return i.type==="named"&&this.context.resolveNamedType({typeId:i.value})?.type==="object"?[{name:st,value:this.context.dynamicTypeLiteralMapper.convert({typeReference:i,value:e})}]:this.getBodyRequestArgsForTypeReference({typeReference:t.value,value:e})}case"list":case"map":case"set":case"literal":case"primitive":case"unknown":return[{name:st,value:this.context.dynamicTypeLiteralMapper.convert({typeReference:t,value:e})}];default:C(t)}}getBodyRequestArgsForNamedTypeReference({typeReference:t,named:e,value:i}){switch(e.type){case"alias":return this.getBodyRequestArgsForTypeReference({typeReference:e.typeReference,value:i});case"enum":case"discriminatedUnion":case"undiscriminatedUnion":return[{name:st,value:this.context.dynamicTypeLiteralMapper.convert({typeReference:t,value:i})}];case"object":return this.context.associateByWireValue({parameters:e.properties,values:this.context.getRecord(i)??{}}).filter(s=>!this.resolvesToLiteralType(s.typeReference)).map(s=>({name:this.context.getPropertyName(s.name.name),value:this.context.dynamicTypeLiteralMapper.convert(s)}));default:C(e)}}resolvesToLiteralType(t){switch(t.type){case"literal":return!0;case"optional":case"nullable":return this.resolvesToLiteralType(t.value);case"named":{let e=this.context.resolveNamedType({typeId:t.value});return e==null?!1:e.type==="alias"?this.resolvesToLiteralType(e.typeReference):!1}case"list":case"map":case"set":case"primitive":case"unknown":return!1;default:C(t)}}getBodyRequestArgsForBytes({body:t,value:e}){let i=this.getBytesBodyRequestTypeInstantiation({value:e});return x.TypeInstantiation.isNop(i)?[]:[{name:this.context.getPropertyName(t.bodyKey),value:i}]}getBytesBodyRequestTypeInstantiation({value:t}){return typeof t!="string"?(this.context.errors.add({severity:P.Critical,message:`Expected bytes value to be a string, got ${typeof t}`}),x.TypeInstantiation.nop()):x.TypeInstantiation.bytes(t)}getBodyPropertyNamesForInlinedRequest(t){if(t.body==null)return new Set;switch(t.body.type){case"referenced":{let e=t.body.bodyType;if(e.type!=="typeReference")return new Set;let i=e.value;if(i.type!=="named")return new Set;let a=this.context.resolveNamedType({typeId:i.value});if(a==null||a.type!=="object")return new Set;let l=new Set;for(let s of a.properties)this.resolvesToLiteralType(s.typeReference)||l.add(this.context.getPropertyName(s.name.name));return l}case"properties":return new Set(t.body.value.filter(e=>!this.resolvesToLiteralType(e.typeReference)).map(e=>this.context.getPropertyName(e.name.name)));case"fileUpload":return new Set(t.body.properties.filter(e=>e.type==="bodyProperty").map(e=>this.context.getPropertyName(e.name.name)));default:C(t.body)}}getMethodArgsForInlinedRequest({request:t,snippet:e}){let i=[],a=this.context.shouldInlinePathParameters();this.context.errors.scope(Z.PathParameters);let l=[];t.pathParameters!=null&&l.push(...this.getPathParameters({namedParameters:t.pathParameters,snippet:e})),this.context.errors.unscope(),this.context.errors.scope(Z.RequestBody);let s=this.getFilePropertyInfo({request:t,snippet:e});this.context.errors.unscope();let E=this.getBodyPropertyNamesForInlinedRequest(t),b=l.map(A=>E.has(A.name)?{...A,name:`${A.name}_`}:A);return this.context.includePathParametersInWrappedRequest({request:t,inlinePathParameters:a})||i.push(...b),this.context.needsRequestParameter({request:t,inlinePathParameters:a,inlineFileProperties:!0})&&i.push(...this.getInlinedRequestArgs({request:t,snippet:e,pathParameterFields:this.context.includePathParametersInWrappedRequest({request:t,inlinePathParameters:a})?b:[],filePropertyInfo:s})),i}getFilePropertyInfo({request:t,snippet:e}){return t.body==null||!this.context.isFileUploadRequestBody(t.body)?{fileFields:[],bodyPropertyFields:[]}:this.context.filePropertyMapper.getFilePropertyInfo({body:t.body,value:e.requestBody})}getInlinedRequestArgs({request:t,snippet:e,pathParameterFields:i,filePropertyInfo:a}){this.context.errors.scope(Z.QueryParameters);let s=this.context.associateQueryParametersByWireValue({parameters:t.queryParameters??[],values:e.queryParameters??{}}).map(R=>({name:this.context.getPropertyName(R.name.name),value:this.context.dynamicTypeLiteralMapper.convert(R)}));this.context.errors.unscope(),this.context.errors.scope(Z.Headers);let b=this.context.associateByWireValue({parameters:t.headers??[],values:e.headers??{}}).map(R=>({name:this.context.getPropertyName(R.name.name),value:this.context.dynamicTypeLiteralMapper.convert(R)}));this.context.errors.unscope(),this.context.errors.scope(Z.RequestBody);let A=t.body!=null?this.getInlinedRequestBodyObjectFields({body:t.body,value:e.requestBody,filePropertyInfo:a}):[];return this.context.errors.unscope(),[...i,...s,...b,...A]}getInlinedRequestBodyObjectFields({body:t,value:e,filePropertyInfo:i}){switch(t.type){case"properties":return this.getInlinedRequestBodyPropertyObjectFields({parameters:t.value,value:e});case"referenced":return this.getReferencedRequestBodyPropertyTypeInstantiation({body:t,value:e});case"fileUpload":return this.getFileUploadRequestBodyObjectFields({filePropertyInfo:i});default:C(t)}}getFileUploadRequestBodyObjectFields({filePropertyInfo:t}){return[...t.fileFields,...t.bodyPropertyFields]}getReferencedRequestBodyPropertyTypeInstantiation({body:t,value:e}){let i=t.bodyType;switch(i.type){case"bytes":return this.getBodyRequestArgsForBytes({body:t,value:e});case"typeReference":return this.getBodyRequestArgsForTypeReference({typeReference:i.value,value:e});default:C(i)}}getInlinedRequestBodyPropertyObjectFields({parameters:t,value:e}){let i=[],l=this.context.associateByWireValue({parameters:t,values:this.context.getRecord(e)??{}}).filter(s=>!this.resolvesToLiteralType(s.typeReference));for(let s of l)i.push({name:this.context.getPropertyName(s.name.name),value:this.context.dynamicTypeLiteralMapper.convert(s)});return i}getPathParameters({namedParameters:t,snippet:e}){let i=[],a=t.filter(s=>!this.resolvesToLiteralType(s.typeReference)),l=this.context.associateByWireValue({parameters:a,values:e.pathParameters??{},ignoreMissingParameters:!0});for(let s of l)i.push({name:this.context.getPropertyName(s.name.name),value:this.context.dynamicTypeLiteralMapper.convert(s)});return i}getMethod({endpoint:t}){return t.declaration.fernFilepath.allParts.length>0?`${t.declaration.fernFilepath.allParts.map(e=>this.context.getMethodName(e)).join(".")}.${this.context.getMethodName(t.declaration.name)}`:this.context.getMethodName(t.declaration.name)}getEnvironmentOptionName({environment:t}){return t!=null?"environment":"base_url"}};var pt=class extends $e{constructor({ir:t,config:e}){super(new at({ir:t,config:e}))}async generate(t){return super.generate(t)}generateSync(t){return super.generateSync(t)}async generateSnippetAst(t){return super.generateSnippetAst(t)}generateMethodCallSnippetAst(t){let e=this.context.resolveEndpointLocationOrThrow(t.endpoint);if(e.length===0)throw new Error(`No endpoints found that match "${t.endpoint.method} ${t.endpoint.path}"`);let i;for(let a of e){let l=this.context.clone(),s=this.createSnippetGenerator(l);try{return s.generateMethodCallSnippetAst({endpoint:a,request:t})}catch(E){i=E}}throw i??new Error(`Failed to generate method call snippet AST for endpoint: ${t.endpoint.method} ${t.endpoint.path}`)}createSnippetGenerator(t){return new ut({context:t})}};0&&(module.exports={DynamicSnippetsGenerator});
20
+ `)}),e.writeNoIndent('"""')}escapeString(e){let i=/(?<!\\)(["'\\\t\n\r])/g,a={'"':'\\"',"'":"\\'","\\":"\\\\"," ":"\\t","\n":"\\n","\r":"\\r"};return e.replace(i,l=>a[l]??l)}writeUnknown({writer:e,value:i}){switch(typeof i){case"boolean":e.write(i?"True":"False");return;case"string":e.write(`"${this.escapeString(i)}"`);return;case"number":e.write(i.toString());return;case"object":if(i==null){e.write("None");return}if(Array.isArray(i)){this.writeUnknownArray({writer:e,value:i});return}this.writeUnknownObject({writer:e,value:i});return;default:throw new Error(`Internal error; unsupported unknown type: ${typeof i}`)}}writeUnknownArray({writer:e,value:i}){if(i.length===0){e.write("[]");return}e.writeLine("["),e.indent();for(let a of i)e.writeNode(p.unknown(a)),e.writeLine(",");e.dedent(),e.write("]")}writeUnknownObject({writer:e,value:i}){let a=Object.entries(i);if(a.length===0){e.write("{}");return}e.writeLine("{"),e.indent();for(let[l,s]of a)e.write(`"${l}": `),e.writeNode(p.unknown(s)),e.writeLine(",");e.dedent(),e.write("}")}};function Na({entries:p}){return p.filter(t=>!ce.isNop(t.key)&&!ce.isNop(t.value))}function Sa({entries:p}){return p.filter(t=>!ce.isNop(t.value))}function Tt({values:p}){return p.filter(t=>!ce.isNop(t))}function Ba(p){return new be(p)}function Pa(p){return new rt(p)}function Fa(p){return new ke(p)}function Ca(p){return new se(p)}function ka(p){return new Te(p)}function La(p){return new Ae(p)}function Ua(p){return new Q(p)}function Ma(p){return new fe(p)}function Oa(p){return new ue(p)}function _a(p){return new ne(p)}function Da(p){return new Re(p)}function Va(p){return new Ne(p)}function $a(p){return new Se(p)}function ja(p){return new le(p)}function Wa(p){return new Ee(p)}function qa(p){return new Fe(p)}function Ga(p){return new pe(p)}function za(p){return new Pe(p)}function Ha(p){return new Ce(p)}c();m();d();var Qa="value",it=class{constructor({context:t}){y(this,"context");this.context=t}convert(t){if(t.value===null)return this.context.isNullable(t.typeReference)?x.TypeInstantiation.none():(this.context.errors.add({severity:P.Critical,message:"Expected non-null value, but got null"}),x.TypeInstantiation.nop());if(t.value===void 0)return x.TypeInstantiation.nop();switch(t.typeReference.type){case"list":return this.convertList({list:t.typeReference.value,value:t.value});case"literal":return this.convertLiteral({literalType:t.typeReference.value,value:t.value});case"map":return this.convertMap({map:t.typeReference,value:t.value});case"named":{let e=this.context.resolveNamedType({typeId:t.typeReference.value});return e==null?x.TypeInstantiation.nop():this.convertNamed({named:e,value:t.value,as:t.as})}case"optional":return this.convert({typeReference:t.typeReference.value,value:t.value,as:t.as});case"nullable":return this.convert({typeReference:t.typeReference.value,value:t.value,as:t.as});case"primitive":return this.convertPrimitive({primitive:t.typeReference.value,value:t.value,as:t.as});case"set":return this.convertSet({set:t.typeReference.value,value:t.value});case"unknown":return x.TypeInstantiation.unknown(t.value);default:C(t.typeReference)}}convertLiteral({literalType:t,value:e}){switch(t.type){case"boolean":{let i=this.context.getValueAsBoolean({value:e});return i==null?x.TypeInstantiation.nop():x.TypeInstantiation.bool(i)}case"string":{let i=this.context.getValueAsString({value:e});return i==null?x.TypeInstantiation.nop():x.TypeInstantiation.str(i)}default:C(t)}}convertList({list:t,value:e}){return Array.isArray(e)?x.TypeInstantiation.list(e.map((i,a)=>{this.context.errors.scope({index:a});try{return this.convert({typeReference:t,value:i})}finally{this.context.errors.unscope()}}),{multiline:!0}):(this.context.errors.add({severity:P.Critical,message:`Expected array but got: ${typeof e}`}),x.TypeInstantiation.nop())}convertSet({set:t,value:e}){return Array.isArray(e)?x.TypeInstantiation.list(e.map((i,a)=>{this.context.errors.scope({index:a});try{return this.convert({typeReference:t,value:i})}finally{this.context.errors.unscope()}}),{multiline:!0}):(this.context.errors.add({severity:P.Critical,message:`Expected array but got: ${typeof e}`}),x.TypeInstantiation.nop())}convertMap({map:t,value:e}){return typeof e!="object"||e==null?(this.context.errors.add({severity:P.Critical,message:`Expected object but got: ${e==null?"null":typeof e}`}),x.TypeInstantiation.nop()):x.TypeInstantiation.dict(Object.entries(e).map(([i,a])=>{this.context.errors.scope(i);try{return{key:this.convert({typeReference:t.key,value:i,as:"key"}),value:this.convert({typeReference:t.value,value:a})}}finally{this.context.errors.unscope()}}),{multiline:!0})}convertNamed({named:t,value:e,as:i}){switch(t.type){case"alias":return this.convert({typeReference:t.typeReference,value:e,as:i});case"discriminatedUnion":return this.convertDiscriminatedUnion({discriminatedUnion:t,value:e});case"enum":return this.convertEnum({enum_:t,value:e});case"object":return this.convertObject({object_:t,value:e});case"undiscriminatedUnion":return this.convertUndiscriminatedUnion({undiscriminatedUnion:t,value:e});default:C(t)}}convertDiscriminatedUnion({discriminatedUnion:t,value:e}){let i=this.context.resolveDiscriminatedUnionTypeInstance({discriminatedUnion:t,value:e});if(i==null)return x.TypeInstantiation.nop();let a=i.singleDiscriminatedUnionType,l=this.convertDiscriminatedUnionProperties({discriminatedUnionTypeInstance:i,unionVariant:a});if(l==null)return x.TypeInstantiation.nop();let s={name:this.context.getPropertyName(t.discriminant.name),value:x.TypeInstantiation.str(a.discriminantValue.wireValue)};return x.TypeInstantiation.typedDict([s,...l],{multiline:!0})}convertDiscriminatedUnionProperties({discriminatedUnionTypeInstance:t,unionVariant:e}){let i=this.getBaseFields({discriminatedUnionTypeInstance:t,singleDiscriminatedUnionType:e});switch(e.type){case"samePropertiesAsObject":{let a=this.context.resolveNamedType({typeId:e.typeId});if(a==null)return;let l=this.convertNamed({named:a,value:t.value});if(!l.isTypedDict()){this.context.errors.add({severity:P.Critical,message:"Internal error; expected union value to be an object"});return}let s=l.asTypedDictOrThrow();return[...i,...s.entries]}case"singleProperty":try{this.context.errors.scope(e.discriminantValue.wireValue);let a=this.context.getRecord(t.value);return a==null?[...i,{name:Qa,value:this.convert({typeReference:e.typeReference,value:t.value})}]:[...i,{name:this.context.getPropertyName(e.discriminantValue.name),value:this.convert({typeReference:e.typeReference,value:a[e.discriminantValue.wireValue]})}]}finally{this.context.errors.unscope()}case"noProperties":return i;default:C(e)}}getBaseFields({discriminatedUnionTypeInstance:t,singleDiscriminatedUnionType:e}){return this.context.associateByWireValue({parameters:e.properties??[],values:this.context.getRecord(t.value)??{},ignoreMissingParameters:!0}).map(a=>{this.context.errors.scope(a.name.wireValue);try{return{name:this.context.getPropertyName(a.name.name),value:this.convert(a)}}finally{this.context.errors.unscope()}})}convertObject({object_:t,value:e}){let i=this.context.associateByWireValue({parameters:t.properties,values:this.context.getRecord(e)??{}});return x.TypeInstantiation.typedDict(i.map(a=>{this.context.errors.scope(a.name.wireValue);try{return{name:this.context.getPropertyName(a.name.name),value:this.convert(a)}}finally{this.context.errors.unscope()}}),{multiline:!0})}convertEnum({enum_:t,value:e}){let i=this.getEnumValue({enum_:t,value:e});return i==null?x.TypeInstantiation.nop():x.TypeInstantiation.str(i)}getEnumValue({enum_:t,value:e}){if(typeof e!="string"){this.context.errors.add({severity:P.Critical,message:`Expected enum value string, got: ${typeof e}`});return}if(t.values.find(a=>a.wireValue===e)==null){this.context.errors.add({severity:P.Critical,message:`An enum value named "${e}" does not exist in this context`});return}return e}convertUndiscriminatedUnion({undiscriminatedUnion:t,value:e}){let i=this.findMatchingUndiscriminatedUnionType({undiscriminatedUnion:t,value:e});return i??x.TypeInstantiation.nop()}findMatchingUndiscriminatedUnionType({undiscriminatedUnion:t,value:e}){for(let i of t.types)try{return this.convert({typeReference:i,value:e})}catch{continue}this.context.errors.add({severity:P.Critical,message:`None of the types in the undiscriminated union matched the given "${typeof e}" value`})}convertPrimitive({primitive:t,value:e,as:i}){switch(t){case"INTEGER":case"LONG":case"UINT":case"UINT_64":{let a=this.getValueAsNumber({value:e,as:i});return a==null?x.TypeInstantiation.nop():x.TypeInstantiation.int(a)}case"FLOAT":case"DOUBLE":{let a=this.getValueAsNumber({value:e});return a==null?x.TypeInstantiation.nop():x.TypeInstantiation.float(a)}case"BOOLEAN":{let a=this.getValueAsBoolean({value:e,as:i});return a==null?x.TypeInstantiation.nop():x.TypeInstantiation.bool(a)}case"BASE_64":case"BIG_INTEGER":case"STRING":{let a=this.context.getValueAsString({value:e});return a==null?x.TypeInstantiation.nop():x.TypeInstantiation.str(a)}case"UUID":{let a=this.context.getValueAsString({value:e});return a==null?x.TypeInstantiation.nop():x.TypeInstantiation.uuid(a)}case"DATE":{let a=this.context.getValueAsString({value:e});return a==null?x.TypeInstantiation.nop():x.TypeInstantiation.date(a)}case"DATE_TIME":{let a=this.context.getValueAsString({value:e});return a==null?x.TypeInstantiation.nop():x.TypeInstantiation.datetime(a)}default:C(t)}}getValueAsNumber({value:t,as:e}){let i=e==="key"&&typeof t=="string"?Number(t):t;return this.context.getValueAsNumber({value:i})}getValueAsBoolean({value:t,as:e}){let i=e==="key"?typeof t=="string"?t==="true":t==="false"?!1:t:t;return this.context.getValueAsBoolean({value:i})}};c();m();d();var ot=class{constructor({context:t}){y(this,"context");this.context=t}getFilePropertyInfo({body:t,value:e}){let i={fileFields:[],bodyPropertyFields:[]},a=this.context.getRecord(e)??{};for(let l of t.properties)switch(l.type){case"file":{let s=this.getSingleFileProperty({property:l,record:a});if(x.TypeInstantiation.isNop(s))break;i.fileFields.push({name:this.context.getPropertyName(l.name),value:s});break}case"fileArray":{let s=this.getArrayFileProperty({property:l,record:a});if(x.TypeInstantiation.isNop(s))break;i.fileFields.push({name:this.context.getPropertyName(l.name),value:s});break}case"bodyProperty":{let s=this.getBodyProperty({property:l,record:a});if(x.TypeInstantiation.isNop(s))break;i.bodyPropertyFields.push({name:this.context.getPropertyName(l.name.name),value:s});break}default:C(l)}return i}getSingleFileProperty({property:t,record:e}){let i=this.context.getSingleFileValue({property:t,record:e});return i==null&&(i=`example_${t.wireValue??"file"}`),this.context.getFileFromString(i)}getArrayFileProperty({property:t,record:e}){let i=this.context.getFileArrayValues({property:t,record:e});return i==null?x.TypeInstantiation.nop():x.TypeInstantiation.list(i.map(a=>this.context.getFileFromString(a)))}getBodyProperty({property:t,record:e}){let i=e[t.name.wireValue];return i==null?x.TypeInstantiation.nop():this.context.dynamicTypeLiteralMapper.convert({typeReference:t.typeReference,value:i})}};var Ya=["list","set"],at=class p extends qe{constructor({ir:e,config:i}){super({ir:e,config:i});y(this,"ir");y(this,"customConfig");y(this,"dynamicTypeLiteralMapper");y(this,"filePropertyMapper");this.ir=e,this.customConfig=i.customConfig??{},this.dynamicTypeLiteralMapper=new it({context:this}),this.filePropertyMapper=new ot({context:this})}clone(){return new p({ir:this.ir,config:this.config})}getClassName(e){let i=e.pascalCase.safeName,a=this.getRootClientClassName();return i===a?`${a}Model`:i}getTypedDictClassName(e){return`${this.getClassName(e)}Params`}getPropertyName(e){let i=e.snakeCase.safeName;return i.startsWith("_")?"f_"+i.substring(i.lastIndexOf("_")+1):i}getMethodName(e){return Ya.includes(e.snakeCase.unsafeName)?e.snakeCase.unsafeName:e.snakeCase.safeName}getRootClientClassReference(){return x.reference({name:this.getRootClientClassName(),modulePath:this.getRootModulePath()})}getRootClientClassName(){return this.customConfig.client?.exported_class_name!=null?this.customConfig.client.exported_class_name:this.customConfig.client_class_name!=null?this.customConfig.client_class_name:this.customConfig.client?.class_name!=null?this.customConfig.client.class_name:this.pascalCase(this.config.organization)+this.pascalCase(this.config.workspaceName)}getEnvironmentClassName(){return`${this.getRootClientClassName()}Environment`}getEnvironmentTypeReferenceFromID(e){let i=this.resolveEnvironmentName(e);if(i!=null)return x.accessAttribute({lhs:this.getEnvironmentClassReference(),rhs:x.codeBlock(this.getEnvironmentEnumName(i))})}getEnvironmentClassReference(){return x.reference({name:this.getEnvironmentClassName(),modulePath:this.getEnvironmentModulePath()})}getEnvironmentEnumName(e){return e.screamingSnakeCase.safeName}isPrimitive(e){switch(e.type){case"primitive":return!0;case"optional":case"nullable":return this.isPrimitive(e.value);case"named":{let i=this.resolveNamedType({typeId:e.value});if(i==null)return!1;switch(i.type){case"alias":return this.isPrimitive(i.typeReference);case"discriminatedUnion":case"undiscriminatedUnion":case"object":case"enum":return!1;default:C(i)}break}case"list":case"set":case"map":case"literal":case"unknown":return!1;default:C(e)}}getFileFromString(e){return x.TypeInstantiation.str(e)}getRootModulePath(){if(this.customConfig.package_name!=null)return[this.customConfig.package_name];let e=this.cleanOrganizationName();return this.customConfig.use_api_name_in_package?[e,this.getApiName()]:[e]}getCoreModulePath(){return this.getRootModulePath().concat("core")}getEnvironmentModulePath(){return this.getRootModulePath().concat("environment")}shouldInlinePathParameters(){return!0}cleanOrganizationName(){return this.config.organization.replace(/[^a-zA-Z0-9]/g,"_")}getApiName(){return vt(this.config.workspaceName)}pascalCase(e){let i=wt(e);return i.charAt(0).toUpperCase()+i.slice(1)}};c();m();d();var Za={type:"primitive",value:"STRING"},Ja=["example"],pi="client",st="request",ut=class{constructor({context:t}){y(this,"context");this.context=t}async generateSnippet({endpoint:t,request:e}){return this.buildPythonFile({endpoint:t,snippet:e}).toString()}generateSnippetSync({endpoint:t,request:e}){return this.buildPythonFile({endpoint:t,snippet:e}).toString()}async generateSnippetAst({endpoint:t,request:e}){return this.buildPythonFile({endpoint:t,snippet:e})}generateMethodCallSnippetAst({endpoint:t,request:e}){return this.callMethod({endpoint:t,snippet:e})}buildPythonFile({endpoint:t,snippet:e}){return x.file({path:Ja,statements:[this.constructClient({endpoint:t,snippet:e}),this.callMethod({endpoint:t,snippet:e})]})}constructClient({endpoint:t,snippet:e}){return x.assign({lhs:x.reference({name:pi}),rhs:x.instantiateClass({classReference:this.context.getRootClientClassReference(),arguments_:this.getConstructorArgs({endpoint:t,snippet:e}).map(i=>x.methodArgument({name:i.name,value:i.value})),multiline:!0})})}getConstructorArgs({endpoint:t,snippet:e}){let i=[],a=this.getConstructorEnvironmentArgs({baseUrl:e.baseURL,environment:e.environment});return a.length>0&&i.push(...a),t.auth!=null&&(e.auth!=null?i.push(...this.getConstructorAuthArgs({auth:t.auth,values:e.auth})):this.context.errors.add({severity:P.Warning,message:`Auth with ${t.auth.type} configuration is required for this endpoint`})),this.context.errors.scope(Z.PathParameters),this.context.ir.pathParameters!=null&&i.push(...this.getPathParameters({namedParameters:this.context.ir.pathParameters,snippet:e})),this.context.errors.unscope(),this.context.errors.scope(Z.Headers),this.context.ir.headers!=null&&e.headers!=null&&i.push(...this.getConstructorHeaderArgs({headers:this.context.ir.headers,values:e.headers})),this.context.errors.unscope(),i}getConstructorEnvironmentArgs({baseUrl:t,environment:e}){let i=this.getEnvironmentValue({baseUrl:t,environment:e});return i==null?[]:e!=null&&this.context.isMultiEnvironmentValues(e)?[{name:"environment",value:i}]:[{name:this.getEnvironmentOptionName({environment:e}),value:i}]}getEnvironmentValue({baseUrl:t,environment:e}){if(t!=null&&e!=null){this.context.errors.add({severity:P.Critical,message:"Cannot specify both baseUrl and environment options"});return}if(t!=null)return x.TypeInstantiation.str(t);if(e!=null){if(this.context.isSingleEnvironmentID(e)){let i=this.context.getEnvironmentTypeReferenceFromID(e);if(i==null){this.context.errors.add({severity:P.Warning,message:`Environment ${JSON.stringify(e)} was not found`});return}return x.TypeInstantiation.reference(i)}if(this.context.isMultiEnvironmentValues(e))return this.context.validateMultiEnvironmentUrlValues(e)?x.TypeInstantiation.reference(x.instantiateClass({classReference:this.context.getEnvironmentClassReference(),arguments_:Object.entries(e).map(([i,a])=>({name:i,value:this.context.dynamicTypeLiteralMapper.convert({typeReference:Za,value:a})})).map(i=>x.methodArgument({name:i.name,value:i.value})),multiline:!0})):void 0}}getConstructorAuthArgs({auth:t,values:e}){switch(t.type){case"basic":return e.type!=="basic"?(this.addAuthMismatchError(t,e),[]):this.getConstructorBasicAuthArg({auth:t,values:e});case"bearer":return e.type!=="bearer"?(this.addAuthMismatchError(t,e),[]):this.getConstructorBearerAuthArgs({auth:t,values:e});case"header":return e.type!=="header"?(this.addAuthMismatchError(t,e),[]):this.getConstructorHeaderAuthArgs({auth:t,values:e});case"oauth":return e.type!=="oauth"?(this.addAuthMismatchError(t,e),[]):this.getConstructorOAuthArgs({auth:t,values:e});case"inferred":return e.type!=="inferred"?(this.addAuthMismatchError(t,e),[]):(this.addWarning("The Python SDK Generator does not support Inferred auth scheme yet"),[]);default:C(t)}}addAuthMismatchError(t,e){this.context.errors.add({severity:P.Critical,message:this.context.newAuthMismatchError({auth:t,values:e}).message})}addWarning(t){this.context.errors.add({severity:P.Warning,message:t})}getConstructorBasicAuthArg({auth:t,values:e}){return[{name:this.context.getPropertyName(t.username),value:x.TypeInstantiation.str(e.username)},{name:this.context.getPropertyName(t.password),value:x.TypeInstantiation.str(e.password)}]}getConstructorBearerAuthArgs({auth:t,values:e}){return[{name:this.context.getPropertyName(t.token),value:x.TypeInstantiation.str(e.token)}]}getConstructorHeaderAuthArgs({auth:t,values:e}){return[{name:this.context.getPropertyName(t.header.name.name),value:this.context.dynamicTypeLiteralMapper.convert({typeReference:t.header.typeReference,value:e.value})}]}getConstructorOAuthArgs({auth:t,values:e}){return[{name:this.context.getPropertyName(t.clientId),value:x.TypeInstantiation.str(e.clientId)},{name:this.context.getPropertyName(t.clientSecret),value:x.TypeInstantiation.str(e.clientSecret)}]}getConstructorHeaderArgs({headers:t,values:e}){let i=[];for(let a of t){let l=this.getConstructorHeaderArg({header:a,value:e.value});l!=null&&i.push(l)}return i}getConstructorHeaderArg({header:t,value:e}){let i=this.context.dynamicTypeLiteralMapper.convert({typeReference:t.typeReference,value:e});if(!x.TypeInstantiation.isNop(i))return{name:this.context.getPropertyName(t.name.name),value:i}}callMethod({endpoint:t,snippet:e}){return x.invokeMethod({on:x.reference({name:pi}),method:this.getMethod({endpoint:t}),arguments_:this.getMethodArgs({endpoint:t,snippet:e}).filter(i=>!x.TypeInstantiation.isNop(i.value)).map(i=>x.methodArgument({name:i.name,value:i.value})),multiline:!0})}getMethodArgs({endpoint:t,snippet:e}){switch(t.request.type){case"inlined":return this.getMethodArgsForInlinedRequest({request:t.request,snippet:e});case"body":return this.getMethodArgsForBodyRequest({request:t.request,snippet:e});default:C(t.request)}}getMethodArgsForBodyRequest({request:t,snippet:e}){let i=[];this.context.errors.scope(Z.PathParameters);let a=[...this.context.ir.pathParameters??[],...t.pathParameters??[]],l=new Set;if(t.body!=null){let s=this.getBodyRequestArgs({body:t.body,value:e.requestBody});if(l=new Set(s.map(E=>E.name)),t.body.type==="typeReference"){let E=t.body.value;if(E.type==="named"){let b=this.context.resolveNamedType({typeId:E.value});if(b!=null&&b.type==="object")for(let A of b.properties)this.resolvesToLiteralType(A.typeReference)||l.add(this.context.getPropertyName(A.name.name))}}}if(a.length>0){let E=this.getPathParameters({namedParameters:a,snippet:e}).map(b=>l.has(b.name)?{...b,name:b.name+"_"}:b);i.push(...E)}return this.context.errors.unscope(),this.context.errors.scope(Z.RequestBody),t.body!=null&&i.push(...this.getBodyRequestArgs({body:t.body,value:e.requestBody})),this.context.errors.unscope(),i}getBodyRequestArgs({body:t,value:e}){switch(t.type){case"bytes":return[{name:st,value:this.getBytesBodyRequestTypeInstantiation({value:e})}];case"typeReference":return this.getBodyRequestArgsForTypeReference({typeReference:t.value,value:e});default:C(t)}}getBodyRequestArgsForTypeReference({typeReference:t,value:e}){switch(t.type){case"named":{let i=this.context.resolveNamedType({typeId:t.value});return i==null?[]:this.getBodyRequestArgsForNamedTypeReference({typeReference:t,named:i,value:e})}case"nullable":case"optional":{let i=t.value;return i.type==="named"&&this.context.resolveNamedType({typeId:i.value})?.type==="object"?[{name:st,value:this.context.dynamicTypeLiteralMapper.convert({typeReference:i,value:e})}]:this.getBodyRequestArgsForTypeReference({typeReference:t.value,value:e})}case"list":case"map":case"set":case"literal":case"primitive":case"unknown":return[{name:st,value:this.context.dynamicTypeLiteralMapper.convert({typeReference:t,value:e})}];default:C(t)}}getBodyRequestArgsForNamedTypeReference({typeReference:t,named:e,value:i}){switch(e.type){case"alias":return this.getBodyRequestArgsForTypeReference({typeReference:e.typeReference,value:i});case"enum":case"discriminatedUnion":case"undiscriminatedUnion":return[{name:st,value:this.context.dynamicTypeLiteralMapper.convert({typeReference:t,value:i})}];case"object":return this.context.associateByWireValue({parameters:e.properties,values:this.context.getRecord(i)??{}}).filter(s=>!this.resolvesToLiteralType(s.typeReference)).map(s=>({name:this.context.getPropertyName(s.name.name),value:this.context.dynamicTypeLiteralMapper.convert(s)}));default:C(e)}}resolvesToLiteralType(t){switch(t.type){case"literal":return!0;case"optional":case"nullable":return this.resolvesToLiteralType(t.value);case"named":{let e=this.context.resolveNamedType({typeId:t.value});return e==null?!1:e.type==="alias"?this.resolvesToLiteralType(e.typeReference):!1}case"list":case"map":case"set":case"primitive":case"unknown":return!1;default:C(t)}}getBodyRequestArgsForBytes({body:t,value:e}){let i=this.getBytesBodyRequestTypeInstantiation({value:e});return x.TypeInstantiation.isNop(i)?[]:[{name:this.context.getPropertyName(t.bodyKey),value:i}]}getBytesBodyRequestTypeInstantiation({value:t}){return typeof t!="string"?(this.context.errors.add({severity:P.Critical,message:`Expected bytes value to be a string, got ${typeof t}`}),x.TypeInstantiation.nop()):x.TypeInstantiation.bytes(t)}getBodyPropertyNamesForInlinedRequest(t){if(t.body==null)return new Set;switch(t.body.type){case"referenced":{let e=t.body.bodyType;if(e.type!=="typeReference")return new Set;let i=e.value;if(i.type!=="named")return new Set;let a=this.context.resolveNamedType({typeId:i.value});if(a==null||a.type!=="object")return new Set;let l=new Set;for(let s of a.properties)this.resolvesToLiteralType(s.typeReference)||l.add(this.context.getPropertyName(s.name.name));return l}case"properties":return new Set(t.body.value.filter(e=>!this.resolvesToLiteralType(e.typeReference)).map(e=>this.context.getPropertyName(e.name.name)));case"fileUpload":return new Set(t.body.properties.filter(e=>e.type==="bodyProperty").map(e=>this.context.getPropertyName(e.name.name)));default:C(t.body)}}getMethodArgsForInlinedRequest({request:t,snippet:e}){let i=[],a=this.context.shouldInlinePathParameters();this.context.errors.scope(Z.PathParameters);let l=[];t.pathParameters!=null&&l.push(...this.getPathParameters({namedParameters:t.pathParameters,snippet:e})),this.context.errors.unscope(),this.context.errors.scope(Z.RequestBody);let s=this.getFilePropertyInfo({request:t,snippet:e});this.context.errors.unscope();let E=this.getBodyPropertyNamesForInlinedRequest(t),b=l.map(A=>E.has(A.name)?{...A,name:`${A.name}_`}:A);return this.context.includePathParametersInWrappedRequest({request:t,inlinePathParameters:a})||i.push(...b),this.context.needsRequestParameter({request:t,inlinePathParameters:a,inlineFileProperties:!0})&&i.push(...this.getInlinedRequestArgs({request:t,snippet:e,pathParameterFields:this.context.includePathParametersInWrappedRequest({request:t,inlinePathParameters:a})?b:[],filePropertyInfo:s})),i}getFilePropertyInfo({request:t,snippet:e}){return t.body==null||!this.context.isFileUploadRequestBody(t.body)?{fileFields:[],bodyPropertyFields:[]}:this.context.filePropertyMapper.getFilePropertyInfo({body:t.body,value:e.requestBody})}getInlinedRequestArgs({request:t,snippet:e,pathParameterFields:i,filePropertyInfo:a}){this.context.errors.scope(Z.QueryParameters);let s=this.context.associateQueryParametersByWireValue({parameters:t.queryParameters??[],values:e.queryParameters??{}}).map(R=>({name:this.context.getPropertyName(R.name.name),value:this.context.dynamicTypeLiteralMapper.convert(R)}));this.context.errors.unscope(),this.context.errors.scope(Z.Headers);let b=this.context.associateByWireValue({parameters:t.headers??[],values:e.headers??{}}).map(R=>({name:this.context.getPropertyName(R.name.name),value:this.context.dynamicTypeLiteralMapper.convert(R)}));this.context.errors.unscope(),this.context.errors.scope(Z.RequestBody);let A=t.body!=null?this.getInlinedRequestBodyObjectFields({body:t.body,value:e.requestBody,filePropertyInfo:a}):[];return this.context.errors.unscope(),[...i,...s,...b,...A]}getInlinedRequestBodyObjectFields({body:t,value:e,filePropertyInfo:i}){switch(t.type){case"properties":return this.getInlinedRequestBodyPropertyObjectFields({parameters:t.value,value:e});case"referenced":return this.getReferencedRequestBodyPropertyTypeInstantiation({body:t,value:e});case"fileUpload":return this.getFileUploadRequestBodyObjectFields({filePropertyInfo:i});default:C(t)}}getFileUploadRequestBodyObjectFields({filePropertyInfo:t}){return[...t.fileFields,...t.bodyPropertyFields]}getReferencedRequestBodyPropertyTypeInstantiation({body:t,value:e}){let i=t.bodyType;switch(i.type){case"bytes":return this.getBodyRequestArgsForBytes({body:t,value:e});case"typeReference":return this.getBodyRequestArgsForTypeReference({typeReference:i.value,value:e});default:C(i)}}getInlinedRequestBodyPropertyObjectFields({parameters:t,value:e}){let i=[],l=this.context.associateByWireValue({parameters:t,values:this.context.getRecord(e)??{}}).filter(s=>!this.resolvesToLiteralType(s.typeReference));for(let s of l)i.push({name:this.context.getPropertyName(s.name.name),value:this.context.dynamicTypeLiteralMapper.convert(s)});return i}getPathParameters({namedParameters:t,snippet:e}){let i=[],a=t.filter(s=>!this.resolvesToLiteralType(s.typeReference)),l=this.context.associateByWireValue({parameters:a,values:e.pathParameters??{},ignoreMissingParameters:!0});for(let s of l)i.push({name:this.context.getPropertyName(s.name.name),value:this.context.dynamicTypeLiteralMapper.convert(s)});return i}getMethod({endpoint:t}){return t.declaration.fernFilepath.allParts.length>0?`${t.declaration.fernFilepath.allParts.map(e=>this.context.getMethodName(e)).join(".")}.${this.context.getMethodName(t.declaration.name)}`:this.context.getMethodName(t.declaration.name)}getEnvironmentOptionName({environment:t}){return t!=null?"environment":"base_url"}};var pt=class extends $e{constructor({ir:t,config:e}){super(new at({ir:t,config:e}))}async generate(t){return super.generate(t)}generateSync(t){return super.generateSync(t)}async generateSnippetAst(t){return super.generateSnippetAst(t)}generateMethodCallSnippetAst(t){let e=this.context.resolveEndpointLocationOrThrow(t.endpoint);if(e.length===0)throw new Error(`No endpoints found that match "${t.endpoint.method} ${t.endpoint.path}"`);let i;for(let a of e){let l=this.context.clone(),s=this.createSnippetGenerator(l);try{return s.generateMethodCallSnippetAst({endpoint:a,request:t})}catch(E){i=E}}throw i??new Error(`Failed to generate method call snippet AST for endpoint: ${t.endpoint.method} ${t.endpoint.path}`)}createSnippetGenerator(t){return new ut({context:t})}};0&&(module.exports={DynamicSnippetsGenerator});
21
21
  /*! Bundled license information:
22
22
 
23
23
  @jspm/core/nodelibs/browser/chunk-DtuTasat.js: