Prompt people if they run hishtory init and already have a bunch of entries + fix tests + add TODOs + add hishtory version to requests

This commit is contained in:
David Dworken
2022-10-01 09:50:06 -07:00
parent 4f94698ca6
commit 757ebb9547
5 changed files with 38 additions and 7 deletions

View File

@ -898,12 +898,21 @@ func getServerHostname() string {
return "https://api.hishtory.dev"
}
func httpClient() *http.Client {
return &http.Client{}
}
func ApiGet(path string) ([]byte, error) {
if os.Getenv("HISHTORY_SIMULATE_NETWORK_ERROR") != "" {
return nil, fmt.Errorf("simulated network error: dial tcp: lookup api.hishtory.dev")
}
start := time.Now()
resp, err := http.Get(getServerHostname() + path)
req, err := http.NewRequest("GET", getServerHostname()+path, nil)
if err != nil {
return nil, fmt.Errorf("failed to create GET: %v", err)
}
req.Header.Set("X-Hishtory-Version", "v0."+Version)
resp, err := httpClient().Do(req)
if err != nil {
return nil, fmt.Errorf("failed to GET %s%s: %v", getServerHostname(), path, err)
}
@ -925,7 +934,13 @@ func ApiPost(path, contentType string, data []byte) ([]byte, error) {
return nil, fmt.Errorf("simulated network error: dial tcp: lookup api.hishtory.dev")
}
start := time.Now()
resp, err := http.Post(getServerHostname()+path, contentType, bytes.NewBuffer(data))
req, err := http.NewRequest("POST", getServerHostname()+path, bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("failed to create POST: %v", err)
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("X-Hishtory-Version", "v0."+Version)
resp, err := httpClient().Do(req)
if err != nil {
return nil, fmt.Errorf("failed to POST %s: %v", path, err)
}