|
| 1 | +/** |
| 2 | + * @description |
| 3 | + * HTTP code snippet generator for R using httr |
| 4 | + * |
| 5 | + * @author |
| 6 | + * @gabrielakoreeda |
| 7 | + * |
| 8 | + * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author. |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict' |
| 12 | + |
| 13 | +var util = require('util') |
| 14 | +var CodeBuilder = require('../../helpers/code-builder') |
| 15 | + |
| 16 | +module.exports = function (source, options) { |
| 17 | + // Start snippet |
| 18 | + var code = new CodeBuilder() |
| 19 | + |
| 20 | + // Import httr |
| 21 | + code.push('library(httr)') |
| 22 | + .blank() |
| 23 | + |
| 24 | + // Set URL |
| 25 | + code.push('url <- "%s"', source.url) |
| 26 | + .blank() |
| 27 | + |
| 28 | + // Construct query string |
| 29 | + var query |
| 30 | + var qs = source.queryObj |
| 31 | + var queryCount = Object.keys(qs).length |
| 32 | + delete source.queryObj['key'] |
| 33 | + |
| 34 | + if (source.queryString.length === 1) { |
| 35 | + code.push('queryString <- list(%s = "%s")', Object.keys(qs), Object.values(qs).toString()) |
| 36 | + .blank() |
| 37 | + } else if (source.queryString.length > 1) { |
| 38 | + var count = 1 |
| 39 | + |
| 40 | + code.push('queryString <- list(') |
| 41 | + |
| 42 | + for (query in qs) { |
| 43 | + if (count++ !== queryCount - 1) { |
| 44 | + code.push(' %s = "%s",', query, qs[query].toString()) |
| 45 | + } else { |
| 46 | + code.push(' %s = "%s"', query, qs[query].toString()) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + code.push(')') |
| 51 | + .blank() |
| 52 | + } |
| 53 | + |
| 54 | + // Construct payload |
| 55 | + var payload = JSON.stringify(source.postData.text) |
| 56 | + |
| 57 | + if (payload) { |
| 58 | + code.push('payload <- %s', payload) |
| 59 | + .blank() |
| 60 | + } |
| 61 | + |
| 62 | + // Define encode |
| 63 | + if (source.postData.text || source.postData.jsonObj || source.postData.params) { |
| 64 | + switch (source.postData.mimeType) { |
| 65 | + case 'application/x-www-form-urlencoded': |
| 66 | + code.push('encode <- "form"') |
| 67 | + .blank() |
| 68 | + break |
| 69 | + |
| 70 | + case 'application/json': |
| 71 | + code.push('encode <- "json"') |
| 72 | + .blank() |
| 73 | + break |
| 74 | + |
| 75 | + case 'multipart/form-data': |
| 76 | + code.push('encode <- "multipart"') |
| 77 | + .blank() |
| 78 | + break |
| 79 | + |
| 80 | + default: |
| 81 | + code.push('encode <- "raw"') |
| 82 | + .blank() |
| 83 | + break |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Construct headers |
| 88 | + var head |
| 89 | + var headers = source.allHeaders |
| 90 | + var headerCount = Object.keys(headers).length |
| 91 | + var header = '' |
| 92 | + var cookies |
| 93 | + var accept |
| 94 | + |
| 95 | + for (head in headers) { |
| 96 | + if (head === 'accept') { |
| 97 | + accept = ', accept("' + headers[head] + '")' |
| 98 | + headerCount = headerCount - 1 |
| 99 | + } else if (head === 'cookie') { |
| 100 | + cookies = ', set_cookies(`' + headers[head].replace(/;/g, '", `').replace(/` /g, '`').replace(/=/g, '` = "') + '")' |
| 101 | + headerCount = headerCount - 1 |
| 102 | + } else if (head !== 'content-type') { |
| 103 | + header = header + head.replace('-', '_') + " = '" + headers[head] |
| 104 | + if (headerCount > 1) { header = header + "', " } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Construct request |
| 109 | + var method = source.method |
| 110 | + var request = util.format('response <- VERB("%s", url', method) |
| 111 | + |
| 112 | + if (payload) { |
| 113 | + request += ', body = payload' |
| 114 | + } |
| 115 | + |
| 116 | + if (header !== '') { |
| 117 | + request += ', add_headers(' + header + "')" |
| 118 | + } |
| 119 | + |
| 120 | + if (source.queryString.length) { |
| 121 | + request += ', query = queryString' |
| 122 | + } |
| 123 | + |
| 124 | + request += ', content_type("' + source.postData.mimeType + '")' |
| 125 | + |
| 126 | + if (typeof accept !== 'undefined') { |
| 127 | + request += accept |
| 128 | + } |
| 129 | + |
| 130 | + if (typeof cookies !== 'undefined') { |
| 131 | + request += cookies |
| 132 | + } |
| 133 | + |
| 134 | + if (source.postData.text || source.postData.jsonObj || source.postData.params) { |
| 135 | + request += ', encode = encode' |
| 136 | + } |
| 137 | + |
| 138 | + request += ')' |
| 139 | + |
| 140 | + code.push(request) |
| 141 | + .blank() |
| 142 | + |
| 143 | + // Print response |
| 144 | + .push('content(response, "text")') |
| 145 | + |
| 146 | + return code.join() |
| 147 | +} |
| 148 | + |
| 149 | +module.exports.info = { |
| 150 | + key: 'httr', |
| 151 | + title: 'httr', |
| 152 | + link: 'https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html', |
| 153 | + description: 'httr: Tools for Working with URLs and HTTP' |
| 154 | +} |
0 commit comments