Skip to content

Commit

Permalink
Added the call to the events API
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon committed Mar 31, 2020
1 parent 6c32d0b commit a812b14
Showing 1 changed file with 47 additions and 15 deletions.
62 changes: 47 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,58 @@
package main

import (
"bufio"
"os"
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)

type Log struct {
eventType string `json:"eventType"`
message string `json:"message"`
EventType string `json:"eventType"`
ProjectName string `json:"projectName"`
PipelineName string `json:"pipelineName"`
BuildId string `json:"buildId"`
BuildMessage string `json:"message"`
}

func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter eventType: ")
inputEventType, _ := reader.ReadString('\n')
fmt.Print("Enter message: ")
inputMessage, _ := reader.ReadString('\n')
jsonPayload := Log{
EventType: "Codefresh",
ProjectName: "Knapsack Pro Demo",
PipelineName: "knapsack-pro-demo",
BuildId: "1214416",
BuildMessage: "This is just a test3",
}
jsonValue, err := json.Marshal(jsonPayload)
if err != nil {
fmt.Printf("Formatting JSON failed with error %s\n", err)
}

var currentLog Log;
currentLog.eventType = inputEventType
currentLog.message = inputMessage

fmt.Print("Log: ", currentLog);
}
accountId := "1410943"
baseUrl := "https://insights-collector.newrelic.com/v1/accounts/"
eventUrl := "/events"

extendedUrl := baseUrl + accountId + eventUrl

var byteBuffer bytes.Buffer
g := gzip.NewWriter(&byteBuffer)
g.Write(jsonValue)
g.Close()

// request, _ := http.NewRequest("POST", extendedUrl, bytes.NewBuffer(jsonValue))
request, _ := http.NewRequest("POST", extendedUrl, &byteBuffer)
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Content-Encoding", "gzip")
request.Header.Set("X-Insert-Key", "NRII-v5y7YgtjLAkopJQgAGSPGV-jPriJIgUc")
client := &http.Client{}
response, err := client.Do(request)
// response, err = http.Post("https://httpbin.org/post", "application/json", bytes.NewBuffer(jsonValue))
if err != nil {
fmt.Printf("The Http request failed with error %s\n", err)
} else {
data, _ := ioutil.ReadAll(response.Body)
fmt.Println(string(data))
}
}

0 comments on commit a812b14

Please sign in to comment.