Beispiel #1
0
func getHttpStream(uri string, eChan chan Event) {
	fmt.Println("Dialing...")

	tlsConfig, err := lib.GetTLSConfig(nil, cfg.SslCert, cfg.SslKey)
	if err != nil {
		log.Fatal("Error getting TLS config.", err)
	}
	tlsConfig.InsecureSkipVerify = true

	transport := http.Transport{
		//Dial:            lib.DialTimeout,
		TLSClientConfig: tlsConfig,
		//ResponseHeaderTimeout: time.Second * 15,
	}
	//status := 0
	client := http.Client{
		Transport: &transport,
	}
	fmt.Println("Requesting stream...")
	req, err := http.NewRequest("GET", uri, nil)
	res, err := client.Do(req)
	fmt.Println("Reading stream...")
	go func(res *http.Response, client *http.Client) {
		defer res.Body.Close()
		decoder := json.NewDecoder(res.Body)
		for {
			var event Event
			fmt.Println("loop start: %+v", event)
			err = decoder.Decode(&event)
			fmt.Println("loop decoding: %+v", event)
			if err != nil {
				//if err == io.EOF || err == io.ErrUnexpectedEOF {
				// if c.eventMonitor.isEnabled() {
				// 	// Signal that we're exiting.
				// 	eventChan <- EOFEvent
				// }
				//fmt.Println("...broken...")
				//break
				//}
				//errChan <- err
				fmt.Println("decoder err", err)
				close(eChan)
				break
			}
			// if event.Time == 0 {
			// 	fmt.Println(".")
			// 	continue
			// }
			//if !c.eventMonitor.isEnabled() {
			//	return
			//}
			//eventChan <- &event
			fmt.Printf("event fired: %+v\n", event)
			eChan <- event
			fmt.Println("event enqueued")
		}
	}(res, &client)
}
Beispiel #2
0
func postHttp(uri string, data string, headers map[string]string) string { // TODO: change 'data' type.

	fmt.Println("Dialing...   for post")

	tlsConfig, err := lib.GetTLSConfig(nil, cfg.SslCert, cfg.SslKey)
	if err != nil {
		log.Fatal("Error getting TLS config.", err)
	}
	tlsConfig.InsecureSkipVerify = true

	transport := http.Transport{
		Dial:                  lib.DialTimeout,
		TLSClientConfig:       tlsConfig,
		ResponseHeaderTimeout: time.Second * 45,
	}
	status := 0
	client := http.Client{
		Transport: &transport,
	}
	postBody := bytes.NewBuffer([]byte(data))
	req, err := http.NewRequest("POST", uri, postBody)
	if err != nil {
		log.Fatal("Error creating new POST request.")
	}
	// Add any headers.
	for k, v := range headers {
		req.Header.Add(k, v)
	}
	fmt.Printf("REQUEST : %+v\n", req)
	resp, err := client.Do(req)
	if err != nil {
		log.Fatal("Error getting http resource.", err)
	} else {
		defer resp.Body.Close()
		status = resp.StatusCode
	}

	body := ""
	if status >= 200 && status < 300 {
		bodyBuf, err := lib.ReadHttpResponseBody(resp)
		if err != nil {
			fmt.Println("err reading body:", err)
			bodyStr := "{ \"success\": false, \"error\": \"" + err.Error() + "\" }"
			bodyBuf = []byte(bodyStr)
		}
		body = string(bodyBuf)
	} else {
		b, err := json.Marshal(resp)
		if err != nil {
			fmt.Println("Error marshalling to json.")
			fmt.Printf("Object to marshal : %+v\n", resp)
			body = "{ success: false, error: '" + err.Error() + "' }"
		} else {
			body = string(b)
		}
	}
	return body
}
Beispiel #3
0
func deleteHttp(uri string) string {

	fmt.Println("Dialing...   for delete")

	tlsConfig, err := lib.GetTLSConfig(nil, cfg.SslCert, cfg.SslKey)
	if err != nil {
		log.Fatal("Error getting TLS config.", err)
	}
	tlsConfig.InsecureSkipVerify = true

	transport := http.Transport{
		Dial:                  lib.DialTimeout,
		TLSClientConfig:       tlsConfig,
		ResponseHeaderTimeout: time.Second * 45,
	}
	status := 0
	client := http.Client{
		Transport: &transport,
	}
	req, err := http.NewRequest("DELETE", uri, nil)
	if err != nil {
		log.Fatal("Error creating new DELETE request.")
	}
	resp, err := client.Do(req)
	if err != nil {
		log.Fatal("Error getting http resource.", err)
	} else {
		defer resp.Body.Close()
		status = resp.StatusCode
	}

	body := ""
	if status >= 200 && status < 300 {
		bodyBuf, err := lib.ReadHttpResponseBody(resp)
		if err != nil {
			fmt.Println("err reading body:", err)
			bodyStr := "{ \"success\": false, \"error\": \"" + err.Error() + "\" }"
			bodyBuf = []byte(bodyStr)
		}
		body = string(bodyBuf)
		if strings.TrimSpace(body) == "" {
			body = "{\"StatusCode\": \"OK\"}"
		}
	} else {
		b, err := json.Marshal(resp)
		if err != nil {
			body = "{ success: false, error: '" + err.Error() + "' }"
		} else {
			body = string(b)
		}
	}
	return body
}
Beispiel #4
0
func getHttpString(uri string) string {
	fmt.Println("Dialing...")

	tlsConfig, err := lib.GetTLSConfig(nil, cfg.SslCert, cfg.SslKey)
	if err != nil {
		log.Fatal("Error getting TLS config.", err)
	}
	tlsConfig.InsecureSkipVerify = true

	transport := http.Transport{
		Dial:                  lib.DialTimeout,
		TLSClientConfig:       tlsConfig,
		ResponseHeaderTimeout: time.Second * 45,
	}
	status := 0
	client := http.Client{
		Transport: &transport,
	}
	resp, err := client.Get(uri)
	if err != nil {
		body := fmt.Sprintf("{ \"success\": false, \"error\": \"Error getting http resource. %s\" }", err)
		log.Println(body)
		return body
	} else {
		defer resp.Body.Close()
		status = resp.StatusCode
	}
	body := ""
	if status >= 200 && status < 300 {
		bodyBuf, err := lib.ReadHttpResponseBody(resp)
		if err != nil {
			fmt.Println("err reading body:", err)
			bodyStr := "{ \"success\": false, \"error\": \"" + err.Error() + "\" }"
			bodyBuf = []byte(bodyStr)
		}
		body = string(bodyBuf)
	} else {
		b, err := json.Marshal(resp)
		if err != nil {
			body = "{ success: false, error: '" + err.Error() + "' }"
		} else {
			body = string(b)
		}
	}
	return body
}