|
| 1 | +import { CodeBuilder } from '../../../helpers/code-builder'; |
| 2 | +import { escapeForSingleQuotes } from '../../../helpers/escape'; |
| 3 | +import { Client } from '../../targets'; |
| 4 | + |
| 5 | +export const faraday: Client = { |
| 6 | + info: { |
| 7 | + key: 'faraday', |
| 8 | + title: 'faraday', |
| 9 | + link: 'https://github.com/lostisland/faraday', |
| 10 | + description: 'Faraday HTTP client', |
| 11 | + }, |
| 12 | + convert: ({ uriObj, queryObj, method: rawMethod, fullUrl, postData, allHeaders }, options = {}) => { |
| 13 | + const { push, blank, join } = new CodeBuilder(); |
| 14 | + |
| 15 | + // To support custom methods we check for the supported methods |
| 16 | + // and if doesn't exist then we build a custom class for it |
| 17 | + const method = rawMethod.toUpperCase(); |
| 18 | + const methods = [ |
| 19 | + 'GET', |
| 20 | + 'POST', |
| 21 | + 'HEAD', |
| 22 | + 'DELETE', |
| 23 | + 'PATCH', |
| 24 | + 'PUT', |
| 25 | + 'OPTIONS', |
| 26 | + 'COPY', |
| 27 | + 'LOCK', |
| 28 | + 'UNLOCK', |
| 29 | + 'MOVE', |
| 30 | + 'TRACE', |
| 31 | + ]; |
| 32 | + |
| 33 | + if(!methods.includes(method)) { |
| 34 | + push(`# Faraday cannot currently run ${method} requests. Please use another client.`) |
| 35 | + return join(); |
| 36 | + } |
| 37 | + |
| 38 | + push("require 'faraday'"); |
| 39 | + blank(); |
| 40 | + |
| 41 | + // Write body to beginning of script |
| 42 | + if(postData.mimeType === 'application/x-www-form-urlencoded') { |
| 43 | + if (postData.params) { |
| 44 | + push(`data = {`); |
| 45 | + postData.params.forEach(param => { |
| 46 | + push(` :${param.name} => ${JSON.stringify(param.value)},`); |
| 47 | + }); |
| 48 | + push(`}`); |
| 49 | + blank(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + push(`conn = Faraday.new(`); |
| 54 | + push(` url: '${uriObj.protocol}//${uriObj.host}',`); |
| 55 | + if(allHeaders['content-type'] || allHeaders['Content-Type']) { |
| 56 | + push(` headers: {'Content-Type' => '${allHeaders['content-type'] || allHeaders['Content-Type']}'}`); |
| 57 | + } |
| 58 | + push(`)`); |
| 59 | + |
| 60 | + blank(); |
| 61 | + push(`response = conn.${method.toLowerCase()}('${uriObj.pathname}') do |req|`); |
| 62 | + |
| 63 | + const headers = Object.keys(allHeaders); |
| 64 | + if (headers.length) { |
| 65 | + headers.forEach(key => { |
| 66 | + if(key.toLowerCase() !== 'content-type') { |
| 67 | + push(` req.headers['${key}'] = '${escapeForSingleQuotes(allHeaders[key])}'`); |
| 68 | + } |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + Object.keys(queryObj).forEach(name => { |
| 73 | + const value = queryObj[name]; |
| 74 | + if (Array.isArray(value)) { |
| 75 | + push(` req.params['${name}'] = ${JSON.stringify(value)}`) |
| 76 | + } else { |
| 77 | + push(` req.params['${name}'] = '${value}'`) |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + switch (postData.mimeType) { |
| 82 | + case 'application/x-www-form-urlencoded': |
| 83 | + if (postData.params) { |
| 84 | + push(` req.body = URI.encode_www_form(data)`); |
| 85 | + } |
| 86 | + break; |
| 87 | + |
| 88 | + case 'application/json': |
| 89 | + if (postData.jsonObj) { |
| 90 | + push(` req.body = ${JSON.stringify(postData.text)}`); |
| 91 | + } |
| 92 | + break; |
| 93 | + |
| 94 | + default: |
| 95 | + if (postData.text) { |
| 96 | + push(` req.body = ${JSON.stringify(postData.text)}`); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + push('end'); |
| 101 | + blank() |
| 102 | + push('puts response.status'); |
| 103 | + push('puts response.body'); |
| 104 | + |
| 105 | + return join(); |
| 106 | + }, |
| 107 | +}; |
0 commit comments