-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Brandon
committed
Mar 31, 2020
1 parent
6c32d0b
commit a812b14
Showing
1 changed file
with
47 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |