-
Notifications
You must be signed in to change notification settings - Fork 2
/
provider.go
47 lines (42 loc) · 1.04 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package getmoe
import (
"fmt"
"net/http"
"sync"
)
var (
providersMu sync.RWMutex
providers = make(map[string]Provider)
)
// Provider describes Board provider
type Provider interface {
New(ProviderConfiguration)
Auth(AuthConfiguration)
BuildRequest(RequestConfiguration)
NextPage()
PageRequest() (*http.Request, error)
Parse([]byte) ([]Post, error)
}
// RegisterProvider registers booru provider
func RegisterProvider(name string, provider Provider) {
providersMu.Lock()
defer providersMu.Unlock()
if provider == nil {
panic("getmoe: unable to register a nil provider")
}
if _, dup := providers[name]; dup {
panic("getmoe: provider '" + name + "' is already registered")
}
providers[name] = provider
}
// NewProvider creates a new provider
func NewProvider(providerName string, conf ProviderConfiguration) (*Provider, error) {
providersMu.RLock()
provider, ok := providers[providerName]
providersMu.RUnlock()
if !ok {
return nil, fmt.Errorf("getmoe: unknown provider %s", providerName)
}
provider.New(conf)
return &provider, nil
}