github.com/mmatczuk/gohan@v0.0.0-20170206152520-30e45d9bdb69/extension/gohanscript/lib/http.go (about)

     1  // Copyright (C) 2016  Juniper Networks, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package lib
    17  
    18  import (
    19  	"bytes"
    20  	"encoding/json"
    21  	"fmt"
    22  	"io"
    23  	"io/ioutil"
    24  	"net/http"
    25  	"time"
    26  )
    27  
    28  //HTTPGet fetch data using HTTP.
    29  func HTTPGet(url string, headers map[string]interface{}) (map[string]interface{}, error) {
    30  	return HTTPRequest(url, "GET", headers, nil)
    31  }
    32  
    33  //HTTPPost posts data using HTTP.
    34  func HTTPPost(url string, headers map[string]interface{}, postData map[string]interface{}) (map[string]interface{}, error) {
    35  	return HTTPRequest(url, "POST", headers, postData)
    36  }
    37  
    38  //HTTPPut puts data using HTTP.
    39  func HTTPPut(url string, headers map[string]interface{}, postData map[string]interface{}) (map[string]interface{}, error) {
    40  	return HTTPRequest(url, "PUT", headers, postData)
    41  }
    42  
    43  //HTTPPatch patches data using HTTP.
    44  func HTTPPatch(url string, headers map[string]interface{}, postData map[string]interface{}) (map[string]interface{}, error) {
    45  	return HTTPRequest(url, "PATCH", headers, postData)
    46  }
    47  
    48  //HTTPDelete deletes data using HTTP.
    49  func HTTPDelete(url string, headers map[string]interface{}) (interface{}, error) {
    50  	return HTTPRequest(url, "DELETE", headers, nil)
    51  }
    52  
    53  //HTTPRequest request HTTP.
    54  func HTTPRequest(url string, method string, headers map[string]interface{}, postData map[string]interface{}) (map[string]interface{}, error) {
    55  	timeout := time.Duration(20 * time.Second)
    56  	client := &http.Client{Timeout: timeout}
    57  	var reader io.Reader
    58  	if postData != nil {
    59  		jsonByte, err := json.Marshal(postData)
    60  		if err != nil {
    61  			return nil, err
    62  		}
    63  		reader = bytes.NewBuffer(jsonByte)
    64  	}
    65  	request, err := http.NewRequest(method, url, reader)
    66  	for key, value := range headers {
    67  		request.Header.Add(key, fmt.Sprintf("%v", value))
    68  	}
    69  
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	resp, err := client.Do(request)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	defer resp.Body.Close()
    78  
    79  	body, err := ioutil.ReadAll(resp.Body)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  	var contents interface{}
    84  	json.Unmarshal(body, &contents)
    85  	return map[string]interface{}{
    86  		"status_code": resp.StatusCode,
    87  		"header":      resp.Header,
    88  		"raw_body":    string(body),
    89  		"contents":    contents,
    90  	}, nil
    91  }