jx/internal/transport/transport.go
Matthew Rich 1460d2285b
Some checks failed
Declarative Tests / build-ubuntu-focal (push) Waiting to run
Lint / golangci-lint (push) Failing after 15s
Declarative Tests / test (push) Failing after 5s
Declarative Tests / build-fedora (push) Has been cancelled
add support for configuration documents
2024-07-01 14:54:18 -07:00

125 lines
2.1 KiB
Go

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package transport
import (
_ "errors"
_ "fmt"
"net/url"
_ "net/http"
_ "strings"
_ "path/filepath"
"io"
_ "os"
"context"
)
type Handler interface {
URI() *url.URL
ContentType() string
Gzip() bool
Signature() string
Reader() io.ReadCloser
Writer() io.WriteCloser
}
type Reader struct {
uri *url.URL
handle Handler
stream io.ReadCloser
}
func NewReader(u *url.URL) (reader *Reader, e error) {
ctx := context.Background()
reader = &Reader{ uri: u }
switch u.Scheme {
case "http", "https":
reader.handle, e = NewHTTP(u, ctx)
case "file":
fallthrough
default:
reader.handle, e = NewFile(u)
}
reader.stream = reader.handle.Reader()
return
}
func NewReaderURI(uri string) (reader *Reader, e error) {
var u *url.URL
if u, e = url.Parse(uri); e == nil {
return NewReader(u)
}
return
}
type Writer struct {
uri *url.URL
handle Handler
stream io.WriteCloser
}
func NewWriter(u *url.URL) (writer *Writer, e error) {
ctx := context.Background()
writer = &Writer{ uri: u }
switch u.Scheme {
case "http", "https":
writer.handle, e = NewHTTP(u, ctx)
case "file":
fallthrough
default:
writer.handle, e = NewFile(u)
}
writer.stream = writer.handle.Writer()
return writer, e
}
func NewWriterURI(uri string) (writer *Writer, e error) {
var u *url.URL
if u, e = url.Parse(uri); e == nil {
return NewWriter(u)
}
return
}
func (r *Reader) Read(b []byte) (int, error) {
return r.stream.Read(b)
}
func (r *Reader) Close() error {
return r.stream.Close()
}
func (r *Reader) ContentType() string {
return r.handle.ContentType()
}
func (r *Reader) Gzip() bool {
return r.handle.Gzip()
}
func (r *Reader) Signature() string {
return r.handle.Signature()
}
func (w *Writer) Write(b []byte) (int, error) {
return w.stream.Write(b)
}
func (w *Writer) Close() error {
return w.stream.Close()
}
func (w *Writer) ContentType() string {
return w.handle.ContentType()
}
func (w *Writer) Gzip() bool {
return w.handle.Gzip()
}
func (w *Writer) Signature() string {
return w.handle.Signature()
}