22
33var util = require ( 'util' ) ;
44
5- module . exports = function ( options ) {
6-
5+ module . exports = function ( source , options ) {
76 // Let's Go!
87 var code = [ ] ;
98
@@ -13,81 +12,66 @@ module.exports = function (options) {
1312 printBody : true
1413 } , options ) ;
1514
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-
2815 var errorPlaceholder = opts . checkErrors ? 'err' : '_' ;
29-
30- var errorCheck = function ( ) {
16+
17+ var errorCheck = function ( ) {
3118 if ( opts . checkErrors ) {
3219 code . push ( '\tif err != nil {' ) ;
3320 code . push ( '\t\tpanic(err)' ) ;
3421 code . push ( '\t}' ) ;
3522 }
3623 } ;
3724
38- // Create boilerplate
25+ // Create boilerplate
3926 code . push ( 'package main\n' ) ;
4027 code . push ( 'import (' ) ;
4128 code . push ( '\t"fmt"' ) ;
42- if ( bodyPresent ) code . push ( '\t"strings"' ) ;
29+
30+ if ( source . postData . text ) {
31+ code . push ( '\t"strings"' ) ;
32+ }
33+
4334 code . push ( '\t"net/http"' ) ;
44- if ( opts . printBody ) code . push ( '\t"io/ioutil"' ) ;
35+
36+ if ( opts . printBody ) {
37+ code . push ( '\t"io/ioutil"' ) ;
38+ }
4539 code . push ( ')\n' ) ;
4640
4741 code . push ( 'func main() {' ) ;
4842
4943 // Create client
5044 code . push ( '\tclient := &http.Client{}' ) ;
51- code . push ( '\turl := "' + req . url + '"' ) ;
45+ code . push ( util . format ( '\turl := "%s"' , source . fullUrl ) ) ;
5246
5347 // 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)' ;
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 ) ) ;
5751 } else {
58- req . body = ' nil' ;
52+ code . push ( util . format ( '\treq, %s := http.NewRequest("%s", url, nil)' , errorPlaceholder , source . method ) ) ;
5953 }
6054
61- code . push ( '\treq, ' + errorPlaceholder + ' := http.NewRequest("' + req . method + '", url, ' + req . body + ')' ) ;
6255 errorCheck ( ) ;
6356
6457 // Add headers
65- var headersPresent = this . source . headers && this . source . headers . length ;
58+ Object . keys ( source . allHeaders ) . map ( function ( key ) {
59+ code . push ( util . format ( '\treq.Header.Add("%s", "%s")' , key , source . allHeaders [ key ] ) ) ;
60+ } ) ;
6661
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- }
62+ // Make request
63+ code . push ( util . format ( '\tres, %s := client.Do(req)' , errorPlaceholder ) ) ;
64+ errorCheck ( ) ;
7465
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 + '")' ) ;
66+ // Get Body
67+ if ( opts . printBody ) {
68+ code . push ( '\tdefer res.Body.Close()' ) ;
8269 }
8370
84- // Make request
85- code . push ( '\tres, ' + errorPlaceholder + ' := client.Do(req)' ) ;
86- errorCheck ( ) ;
71+ if ( opts . printBody ) {
72+ code . push ( util . format ( '\tbody, %s := ioutil.ReadAll(res.Body)' , errorPlaceholder ) ) ;
73+ }
8774
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)' ) ;
9175 errorCheck ( ) ;
9276
9377 // Print it
@@ -96,7 +80,7 @@ module.exports = function (options) {
9680 if ( opts . printBody ) {
9781 code . push ( '\tfmt.Println(string(body))' ) ;
9882 }
99-
83+
10084 // End main block
10185 code . push ( '}' ) ;
10286
0 commit comments