2022-04-02 12:05:32 -07:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/jtom38/newsbot/collector/domain/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SourcesClient struct {
|
|
|
|
rootUri string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *SourcesClient) List() ([]model.Sources, error) {
|
|
|
|
var items []model.Sources
|
2022-04-07 14:53:40 -07:00
|
|
|
url := fmt.Sprintf("%v/api/v1/sources", sb.rootUri)
|
|
|
|
resp, err := getContent(url)
|
|
|
|
if err != nil { return items, err }
|
2022-04-02 12:05:32 -07:00
|
|
|
|
2022-04-07 14:53:40 -07:00
|
|
|
err = json.Unmarshal(resp, &items)
|
2022-04-02 12:05:32 -07:00
|
|
|
if err != nil { return []model.Sources{}, err }
|
|
|
|
|
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *SourcesClient) FindBySource(SourceType string) ([]model.Sources, error) {
|
|
|
|
items, err := sb.List()
|
|
|
|
if err != nil { log.Panicln(err) }
|
|
|
|
|
|
|
|
var res []model.Sources
|
|
|
|
for _, item := range(items) {
|
|
|
|
if item.Source == SourceType {
|
|
|
|
res = append(res, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|