diff options
author | clsr <clsr@clsr.net> | 2017-08-18 13:46:10 +0200 |
---|---|---|
committer | clsr <clsr@clsr.net> | 2017-08-18 13:46:10 +0200 |
commit | da77deba78c8a7447b4a38324d2422a5df293b26 (patch) | |
tree | bb94b8e18eaf27a86e9fc21c4342d9e0a605baf7 /client.go | |
download | cnp-go-da77deba78c8a7447b4a38324d2422a5df293b26.tar.gz cnp-go-da77deba78c8a7447b4a38324d2422a5df293b26.zip |
Initial commitv0.1.0
Diffstat (limited to 'client.go')
-rw-r--r-- | client.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/client.go b/client.go new file mode 100644 index 0000000..b208637 --- /dev/null +++ b/client.go @@ -0,0 +1,39 @@ +package cnp + +import ( + "net" + "strconv" + "strings" +) + +// TODO: make more modular and extensible like net/http + +// Send sends a CNP request to a server and returns the response. +// +// The TCP connection is made using net.Dial. +func Send(r *Request) (*Response, error) { + host := r.Host() + if strings.LastIndexByte(host, ':') <= strings.LastIndexByte(host, ']') { // missing/default port + host = net.JoinHostPort(host, strconv.Itoa(DefaultPort)) + } + conn, err := net.Dial("tcp", host) + if err != nil { + return nil, err + } + if err = r.Write(conn); err != nil { + return nil, err + } + if err = r.Close(); err != nil { + return nil, err + } + return ParseResponse(conn) +} + +// Get sends a body-less request to a given URL. +func Get(url string) (*Response, error) { + req, err := NewRequestURL(url, nil) + if err != nil { + return nil, err + } + return Send(req) +} |