MCPcopy Create free account
hub / github.com/scalar/scalar / generate

Function generate

packages/snippetz/src/plugins/python/python3/python3.ts:179–255  ·  view source on GitHub ↗
(request, configuration)

Source from the content-addressed store, hash-verified

177 client: 'python3',
178 title: 'http.client',
179 generate(request, configuration) {
180 const normalizedRequest = normalizeRequest({
181 url: 'https://example.com',
182 ...request,
183 })
184 const requestUrl = normalizedRequest.url ?? 'https://example.com'
185 const parsedUrl = parseRequestUrl(requestUrl)
186 const queryString = mergeQueryString(parsedUrl, normalizedRequest.queryString)
187 const path = `${parsedUrl.pathname || '/'}${queryString}`
188 const headers = normalizedRequest.headers?.reduce<Record<string, string>>((acc, header) => {
189 if (!(header.name in acc)) {
190 acc[header.name] = header.value
191 }
192
193 return acc
194 }, {})
195
196 const preparedHeaders = headers ?? {}
197
198 if (normalizedRequest.cookies?.length) {
199 const cookieHeader = normalizedRequest.cookies
200 .map((cookie) => `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`)
201 .join('; ')
202
203 if (preparedHeaders.Cookie) {
204 preparedHeaders.Cookie = `${preparedHeaders.Cookie}; ${cookieHeader}`
205 } else {
206 preparedHeaders.Cookie = cookieHeader
207 }
208 }
209
210 if (configuration?.auth?.username && configuration.auth.password) {
211 const authValue = createBasicAuthToken(`${configuration.auth.username}:${configuration.auth.password}`)
212 preparedHeaders.Authorization = `Basic ${authValue}`
213 }
214
215 const imports = new Set(['import http.client'])
216 const setupLines: string[] = []
217 const bodyResult = buildRequestBody(normalizedRequest, preparedHeaders, imports)
218
219 setupLines.push(...bodyResult.payloadLines)
220
221 if (Object.keys(preparedHeaders).length) {
222 setupLines.push(`headers = ${formatPythonValue(preparedHeaders, 0)}`)
223 }
224
225 const requestArguments = [JSON.stringify(normalizedRequest.method), JSON.stringify(path)]
226
227 if (bodyResult.payloadVariable) {
228 requestArguments.push(`body=${bodyResult.payloadVariable}`)
229 }
230
231 if (Object.keys(preparedHeaders).length) {
232 requestArguments.push('headers=headers')
233 }
234
235 const requestCall =
236 requestArguments.length <= 2

Callers

nothing calls this directly

Calls 7

normalizeRequestFunction · 0.90
formatPythonValueFunction · 0.90
parseRequestUrlFunction · 0.85
mergeQueryStringFunction · 0.85
createBasicAuthTokenFunction · 0.85
mapMethod · 0.80
buildRequestBodyFunction · 0.70

Tested by

no test coverage detected