-
Notifications
You must be signed in to change notification settings - Fork 9
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
1 parent
1fa6b1c
commit 64114a3
Showing
3 changed files
with
63 additions
and
55 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
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
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package testclient | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"fmt" | ||
"mime/multipart" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
func Ingest(addr string, urlParams map[string]string, jfr string) error { | ||
data, err := os.ReadFile(jfr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
body := new(bytes.Buffer) | ||
|
||
mw := multipart.NewWriter(body) | ||
part, err := mw.CreateFormFile("jfr", "jfr") | ||
if err != nil { | ||
return fmt.Errorf("failed to create form file: %w", err) | ||
} | ||
gw := gzip.NewWriter(part) | ||
if _, err := gw.Write(data); err != nil { | ||
return err | ||
} | ||
gw.Close() | ||
mw.Close() | ||
|
||
req, err := http.NewRequest("POST", fmt.Sprintf("%s/ingest", addr), body) | ||
if err != nil { | ||
return err | ||
} | ||
req.Header.Add("Content-Type", mw.FormDataContentType()) | ||
|
||
q := req.URL.Query() | ||
for k, v := range urlParams { | ||
q.Add(k, v) | ||
} | ||
req.URL.RawQuery = q.Encode() | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp.Body.Close() | ||
if resp.StatusCode < 200 || resp.StatusCode >= 300 { | ||
return fmt.Errorf("failed to upload profile; http status code: %d", resp.StatusCode) | ||
} | ||
return nil | ||
} |