Get Data as Byte with Data Type from anywhere which providers are defined.
Always we need to get data from anywhere. In a lot of application or libraries we want to find data by the given environment and options.
- Provider Factory
type ProviderFactory func(opts ...interface{}) (Provider, error)
- Provider
type Provider interface{
Get(input interface{}) ([]byte, string, error)
}
- Create(provider type)
Create is take provider type like
file
orhttp
var p, err = provider.Create("http")
- Setup if provider needs options for setup then you can use this method.
var p, err = provider.Create("http").Setup(opts...interface{})
- Build Provides to create provider.
var p, err = provider.Create("http").Build()
file
provider Provides to get data from absolute or relative file path.
p, err := provider.Create("file").Build()
if err != nil {
fmt.Println("couldn't create file provider !")
} else {
data, ext, err := p.Get("./file.go")
}
http
provider
Provides to get data from absolute or relative file path.
- Standard Http Provider
p, err := provider.Create("http").Build()
if err != nil {
fmt.Println("couldn't create http provider !")
} else {
data, ext, err := p.Get("https://www.google.com.tr/logos/2017/hiphop/cta_bg.jpg")
}
- Custom Http Provider if you need to apply some security rules then
// get file by http provider
result, contentType, err = p.Get(&provider.HttpInput{
Method: "GET",
Url:"https://www.google.com.tr/logos/2017/hiphop/cta_bg.jpg",
Headers: map[string]string{},
})
- Create Provider
type MyProvider struct {
opts interface{}
}
- Create
Get
method for your provider
func (provider *MyProvider) Get(input interface{}) ([]byte, string, error) {
...
result is must be data as []byte
contentType is extension of the data.
return result, contentType, err
}
- Create Provider Factory Function.
func MyProviderFactory(opts ...interface{}) (Provider, error) {
return &MyProvider{
opts: nil,
}, nil
}
import (
"github.com/kbukum/dataprovider"
)
func main() {
provider.Add("myProvider", MyProviderFactory)
}