Code refactoring for streams HandleFunc

This commit is contained in:
Alex X
2024-06-14 12:48:29 +03:00
parent 1ac9d54dab
commit ecfe802065
15 changed files with 53 additions and 93 deletions
+6 -2
View File
@@ -43,8 +43,12 @@ type Client struct {
recv int
}
func NewClient(url string) *Client {
return &Client{url: url}
func Dial(rawURL string) (*Client, error) {
client := &Client{url: rawURL}
if err := client.Dial(); err != nil {
return nil, err
}
return client, nil
}
const (
+6 -2
View File
@@ -23,7 +23,7 @@ type Client struct {
send int
}
func NewClient(rawURL string) (*Client, error) {
func Dial(rawURL string) (*Client, error) {
// check if url is valid url
u, err := url.Parse(rawURL)
if err != nil {
@@ -33,7 +33,11 @@ func NewClient(rawURL string) (*Client, error) {
u.Scheme = "http"
u.Path = ""
return &Client{url: u.String()}, nil
client := &Client{url: u.String()}
if err = client.Dial(); err != nil {
return nil, err
}
return client, err
}
func (c *Client) Dial() (err error) {
+7 -2
View File
@@ -46,8 +46,13 @@ type Client struct {
recv int
}
func NewClient(id string) *Client {
return &Client{ID: id}
func Dial(source string) (*Client, error) {
id := strings.Replace(source[8:], "/", ":", 1)
client := &Client{ID: id}
if err := client.Dial(); err != nil {
return nil, err
}
return client, nil
}
func (c *Client) Dial() (err error) {
+1 -1
View File
@@ -14,7 +14,7 @@ type Client struct {
api *API
}
func NewClient(rawURL string) (*Client, error) {
func Dial(rawURL string) (*Client, error) {
u, err := url.Parse(rawURL)
if err != nil {
return nil, err
+9 -2
View File
@@ -34,8 +34,15 @@ type Client struct {
backchannel bool
}
func NewClient(url string) *Client {
return &Client{url: url}
func Dial(rawURL string) (*Client, error) {
client := &Client{url: rawURL}
if err := client.Dial(); err != nil {
return nil, err
}
if err := client.Connect(); err != nil {
return nil, err
}
return client, nil
}
func (c *Client) Dial() error {