James Tombleson
11892b9a7b
* starting the ffxiv reader * working on getting the standard interface for sources based on the work for ffxiv * got more of ffxiv working and updated tests * Author and Description can be extracted and validated with tests * added uuid package * ffxiv core logic is working and testes updated to reflect it. * Updated the scheduler with the current sources and moved them from main * updated reddit to allow modern go to talk to the endpoint with a debug flag * gave the func a better name * cleaned up main * Moved cache to its own package and updated tests" * moved config to its own package and added basic tests * updated imports * minor update" * interface update and cache model update * updated the scheduler for basic services. No DB calls yet * updated db calls * bypassed the reddit test as its flaky in github
101 lines
1.9 KiB
Go
101 lines
1.9 KiB
Go
package database
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/jtom38/newsbot/collector/domain/model"
|
|
)
|
|
|
|
// Generate this struct fr
|
|
type ArticlesClient struct {
|
|
rootUri string
|
|
}
|
|
|
|
func (ac *ArticlesClient) List() ([]model.Articles, error) {
|
|
var items []model.Articles
|
|
url := fmt.Sprintf("%v/api/v1/articles", ac.rootUri)
|
|
resp, err := getContent(url)
|
|
if err != nil {
|
|
return items, err
|
|
}
|
|
|
|
err = json.Unmarshal(resp, &items)
|
|
if err != nil {
|
|
return []model.Articles{}, err
|
|
}
|
|
|
|
return items, nil
|
|
}
|
|
|
|
func (ac *ArticlesClient) FindByID(ID uint) (model.Articles, error) {
|
|
var items model.Articles
|
|
url := fmt.Sprintf("%v/api/v1/articles/%v", ac.rootUri, ID)
|
|
resp, err := getContent(url)
|
|
if err != nil {
|
|
return items, err
|
|
}
|
|
|
|
err = json.Unmarshal(resp, &items)
|
|
if err != nil {
|
|
return items, err
|
|
}
|
|
|
|
return items, nil
|
|
}
|
|
|
|
func (ac *ArticlesClient) FindByUrl(url string) (model.Articles, error) {
|
|
var item model.Articles
|
|
get := fmt.Sprintf("%v/api/v1/articles/url/%v", ac.rootUri, url)
|
|
resp, err := getContent(get)
|
|
if err != nil {
|
|
return item, err
|
|
}
|
|
|
|
err = json.Unmarshal(resp, &item)
|
|
if err != nil {
|
|
return item, err
|
|
}
|
|
|
|
return item, nil
|
|
}
|
|
|
|
func (ac *ArticlesClient) Delete(id int32) error {
|
|
return errors.New("not implemented")
|
|
}
|
|
|
|
func (ac *ArticlesClient) Add(item model.Articles) error {
|
|
//return errors.New("not implemented")
|
|
url := fmt.Sprintf("%v/api/v1/articles/", ac.rootUri)
|
|
|
|
bItem, err := json.Marshal(item)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
client := &http.Client{}
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(bItem))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
return errors.New("failed to post to the DB")
|
|
}
|
|
|
|
return nil
|
|
//body, err := ioutil.ReadAll(resp.Body)
|
|
//if err != nil { return err }
|
|
|
|
}
|