48 lines
811 B
Go
48 lines
811 B
Go
package client
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
type DemoApiClient struct {
|
|
serverAddress string
|
|
client http.Client
|
|
}
|
|
|
|
func newDemoClient(serverAddress string) DemoApiClient {
|
|
return DemoApiClient{
|
|
serverAddress: serverAddress,
|
|
client: http.Client{},
|
|
}
|
|
}
|
|
|
|
type HelloBodyParam struct {
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// This is an example route to demo passing a body in
|
|
func (d DemoApiClient) Hello() error {
|
|
endpoint := fmt.Sprintf("%s/api/v1/demo/hello", d.serverAddress)
|
|
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := d.client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
//defer resp.Body.Close()
|
|
|
|
content, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log.Println(string(content))
|
|
|
|
return nil
|
|
}
|