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