forked from canonical/pebble
-
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.
client.Do option 1: Do with RequestOptions and RequestResponse per Fred
- Loading branch information
Showing
3 changed files
with
170 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/gorilla/websocket" | ||
|
||
"github.com/canonical/pebble/client" | ||
) | ||
|
||
func main() { | ||
config := &client.Config{ | ||
Socket: os.Getenv("PEBBLE_SOCKET"), | ||
} | ||
pebble, err := client.New(config) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
myClient := NewMyClient(pebble) | ||
services, err := myClient.UpperServices() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
for _, info := range services { | ||
fmt.Printf("%s: startup %q, current %q\n", info.Name, info.Startup, info.Current) | ||
} | ||
fmt.Println("---") | ||
fmt.Printf("Server version: %s\n", myClient.ServerVersion) | ||
} | ||
|
||
type MyClient struct { | ||
ServerVersion string | ||
|
||
pebble *client.Client | ||
} | ||
|
||
func NewMyClient(pebble *client.Client) *MyClient { | ||
c := &MyClient{pebble: pebble} | ||
c.pebble.SetDecodeHook(c.decodeHook) | ||
return c | ||
} | ||
|
||
func (c *MyClient) decodeHook(data []byte, opts *client.RequestOptions) error { | ||
// Demonstrate use of opts: only do custom decode on v1 requests. | ||
if !strings.HasPrefix(opts.Path, "/v1/") { | ||
return nil | ||
} | ||
var frame struct { | ||
ServerVersion string `json:"server-version"` | ||
} | ||
err := json.Unmarshal(data, &frame) | ||
if err != nil { | ||
return err | ||
} | ||
if frame.ServerVersion != "" { | ||
c.ServerVersion = frame.ServerVersion | ||
} | ||
return nil | ||
} | ||
|
||
func (c *MyClient) UpperServices() ([]UpperServiceInfo, error) { | ||
var infos []UpperServiceInfo | ||
_, err := c.pebble.Do(context.Background(), &client.RequestOptions{ | ||
Method: "GET", | ||
Path: "/v1/services", | ||
}, &infos) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, info := range infos { | ||
info.Name = strings.ToUpper(info.Name) | ||
info.Startup = strings.ToUpper(info.Startup) | ||
info.Current = strings.ToUpper(info.Current) | ||
} | ||
return infos, nil | ||
} | ||
|
||
func (c *MyClient) GetWebsocket(url string) (*websocket.Conn, error) { | ||
dialer := websocket.Dialer{ | ||
NetDial: c.pebble.Transport().Dial, | ||
Proxy: c.pebble.Transport().Proxy, | ||
TLSClientConfig: c.pebble.Transport().TLSClientConfig, | ||
HandshakeTimeout: 5 * time.Second, | ||
} | ||
conn, _, err := dialer.Dial(url, nil) | ||
return conn, err | ||
} | ||
|
||
type UpperServiceInfo struct { | ||
Name string `json:"name"` | ||
Startup string `json:"startup"` | ||
Current string `json:"current"` | ||
} |
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