|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var util = require('util'); |
| 4 | + |
| 5 | +module.exports = function (source, options) { |
| 6 | + // Let's Go! |
| 7 | + var code = []; |
| 8 | + |
| 9 | + // Define Options |
| 10 | + var opts = util._extend({ |
| 11 | + checkErrors: false, |
| 12 | + printBody: true |
| 13 | + }, options); |
| 14 | + |
| 15 | + var errorPlaceholder = opts.checkErrors ? 'err' : '_'; |
| 16 | + |
| 17 | + var errorCheck = function () { |
| 18 | + if (opts.checkErrors) { |
| 19 | + code.push('\tif err != nil {'); |
| 20 | + code.push('\t\tpanic(err)'); |
| 21 | + code.push('\t}'); |
| 22 | + } |
| 23 | + }; |
| 24 | + |
| 25 | + // Create boilerplate |
| 26 | + code.push('package main\n'); |
| 27 | + code.push('import ('); |
| 28 | + code.push('\t"fmt"'); |
| 29 | + |
| 30 | + if (source.postData.text) { |
| 31 | + code.push('\t"strings"'); |
| 32 | + } |
| 33 | + |
| 34 | + code.push('\t"net/http"'); |
| 35 | + |
| 36 | + if (opts.printBody) { |
| 37 | + code.push('\t"io/ioutil"'); |
| 38 | + } |
| 39 | + code.push(')\n'); |
| 40 | + |
| 41 | + code.push('func main() {'); |
| 42 | + |
| 43 | + // Create client |
| 44 | + code.push('\tclient := &http.Client{}'); |
| 45 | + code.push(util.format('\turl := "%s"', source.fullUrl)); |
| 46 | + |
| 47 | + // If we have body content or not create the var and reader or nil |
| 48 | + if (source.postData.text) { |
| 49 | + code.push('\tpayload := ' + JSON.stringify(source.postData.text)); |
| 50 | + code.push(util.format('\treq, ' + errorPlaceholder + ' := http.NewRequest("%s", url, strings.NewReader(payload))', source.method)); |
| 51 | + } else { |
| 52 | + code.push(util.format('\treq, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method)); |
| 53 | + } |
| 54 | + |
| 55 | + errorCheck(); |
| 56 | + |
| 57 | + // Add headers |
| 58 | + Object.keys(source.allHeaders).map(function (key) { |
| 59 | + code.push(util.format('\treq.Header.Add("%s", "%s")', key, source.allHeaders[key])); |
| 60 | + }); |
| 61 | + |
| 62 | + // Make request |
| 63 | + code.push(util.format('\tres, %s := client.Do(req)', errorPlaceholder)); |
| 64 | + errorCheck(); |
| 65 | + |
| 66 | + // Get Body |
| 67 | + if (opts.printBody) { |
| 68 | + code.push('\tdefer res.Body.Close()'); |
| 69 | + } |
| 70 | + |
| 71 | + if (opts.printBody) { |
| 72 | + code.push(util.format('\tbody, %s := ioutil.ReadAll(res.Body)', errorPlaceholder)); |
| 73 | + } |
| 74 | + |
| 75 | + errorCheck(); |
| 76 | + |
| 77 | + // Print it |
| 78 | + code.push('\tfmt.Println(res)'); |
| 79 | + |
| 80 | + if (opts.printBody) { |
| 81 | + code.push('\tfmt.Println(string(body))'); |
| 82 | + } |
| 83 | + |
| 84 | + // End main block |
| 85 | + code.push('}'); |
| 86 | + |
| 87 | + return code.join('\n'); |
| 88 | +}; |
| 89 | + |
| 90 | +module.exports.info = { |
| 91 | + key: 'native', |
| 92 | + title: 'NewRequest', |
| 93 | + link: 'http://golang.org/pkg/net/http/#NewRequest', |
| 94 | + description: 'Golang HTTP client request' |
| 95 | +}; |
0 commit comments