summaryrefslogtreecommitdiff
path: root/vendor/github.com/utahta/go-openuri/openuri.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/utahta/go-openuri/openuri.go')
-rw-r--r--vendor/github.com/utahta/go-openuri/openuri.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/vendor/github.com/utahta/go-openuri/openuri.go b/vendor/github.com/utahta/go-openuri/openuri.go
new file mode 100644
index 0000000..a1ce966
--- /dev/null
+++ b/vendor/github.com/utahta/go-openuri/openuri.go
@@ -0,0 +1,54 @@
+package openuri
+
+import (
+ "io"
+ "net/http"
+ "os"
+ "strings"
+)
+
+// Client type
+type Client struct {
+ httpClient *http.Client
+}
+
+// ClientOption type
+type ClientOption func(*Client) error
+
+// New returns a Client struct
+func New(options ...ClientOption) (*Client, error) {
+ c := &Client{httpClient: http.DefaultClient}
+ for _, option := range options {
+ if err := option(c); err != nil {
+ return nil, err
+ }
+ }
+ return c, nil
+}
+
+// Open an io.ReadCloser from a local file or URL
+func Open(name string, options ...ClientOption) (io.ReadCloser, error) {
+ c, err := New(options...)
+ if err != nil {
+ return nil, err
+ }
+ return c.Open(name)
+}
+
+func WithHTTPClient(v *http.Client) ClientOption {
+ return func(c *Client) error {
+ c.httpClient = v
+ return nil
+ }
+}
+
+func (c *Client) Open(name string) (io.ReadCloser, error) {
+ if strings.HasPrefix(name, "http://") || strings.HasPrefix(name, "https://") {
+ resp, err := c.httpClient.Get(name)
+ if err != nil {
+ return nil, err
+ }
+ return resp.Body, nil
+ }
+ return os.Open(name)
+}