summaryrefslogtreecommitdiffstats
path: root/client.go
diff options
context:
space:
mode:
authorclsr <clsr@clsr.net>2017-08-18 13:46:10 +0200
committerclsr <clsr@clsr.net>2017-08-18 13:46:10 +0200
commitda77deba78c8a7447b4a38324d2422a5df293b26 (patch)
treebb94b8e18eaf27a86e9fc21c4342d9e0a605baf7 /client.go
downloadcnp-go-da77deba78c8a7447b4a38324d2422a5df293b26.tar.gz
cnp-go-da77deba78c8a7447b4a38324d2422a5df293b26.zip
Initial commitv0.1.0
Diffstat (limited to 'client.go')
-rw-r--r--client.go39
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)
+}