2022-06-08 21:17:08 -07:00
|
|
|
package databaseRest
|
2022-04-02 12:05:32 -07:00
|
|
|
|
|
|
|
import (
|
2022-04-07 14:53:40 -07:00
|
|
|
"errors"
|
2022-04-02 12:05:32 -07:00
|
|
|
"io/ioutil"
|
2022-04-07 14:53:40 -07:00
|
|
|
"log"
|
2022-04-02 12:05:32 -07:00
|
|
|
"net/http"
|
|
|
|
|
2022-04-29 13:02:25 -07:00
|
|
|
"github.com/jtom38/newsbot/collector/services/config"
|
2022-04-02 12:05:32 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type DatabaseClient struct {
|
|
|
|
Diagnosis DiagnosisClient
|
|
|
|
|
|
|
|
Articles ArticlesClient
|
|
|
|
Sources SourcesClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// This will generate a new client to interface with the API Database.
|
|
|
|
func NewDatabaseClient() DatabaseClient {
|
2022-04-29 13:02:25 -07:00
|
|
|
cc := config.New()
|
|
|
|
dbUri := cc.GetConfig(config.DB_URI)
|
2022-04-02 12:05:32 -07:00
|
|
|
|
|
|
|
var client = DatabaseClient{}
|
|
|
|
client.Diagnosis.rootUri = dbUri
|
|
|
|
client.Sources.rootUri = dbUri
|
|
|
|
client.Articles.rootUri = dbUri
|
|
|
|
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
2022-04-07 14:53:40 -07:00
|
|
|
func getContent(url string) ([]byte, error) {
|
2022-04-02 12:05:32 -07:00
|
|
|
client := &http.Client{}
|
2022-04-07 14:53:40 -07:00
|
|
|
var blank []byte
|
2022-04-02 12:05:32 -07:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
2022-04-07 14:53:40 -07:00
|
|
|
if err != nil { return blank, err }
|
2022-04-02 12:05:32 -07:00
|
|
|
|
|
|
|
// set the user agent header to avoid kick backs.. as much
|
|
|
|
req.Header.Set("User-Agent", getUserAgent())
|
|
|
|
|
|
|
|
log.Printf("Requesting content from %v\n", url)
|
|
|
|
resp, err := client.Do(req)
|
2022-04-07 14:53:40 -07:00
|
|
|
if err != nil { return blank, err }
|
|
|
|
if resp.StatusCode == 404 {
|
|
|
|
err = errors.New("404 not found")
|
|
|
|
return blank, err
|
|
|
|
}
|
2022-04-02 12:05:32 -07:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
2022-04-07 14:53:40 -07:00
|
|
|
if err != nil { return blank, err }
|
2022-04-02 12:05:32 -07:00
|
|
|
|
|
|
|
//log.Println(string(body))
|
2022-04-07 14:53:40 -07:00
|
|
|
return body, nil
|
2022-04-02 12:05:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func httpDelete(url string) error {
|
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("DELETE", url, nil)
|
|
|
|
if err != nil { return err }
|
|
|
|
|
|
|
|
//req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.10; rv:75.0) Gecko/20100101 Firefox/75.0")
|
|
|
|
req.Header.Set("User-Agent", getUserAgent())
|
|
|
|
|
|
|
|
_, err = client.Do(req)
|
|
|
|
if err != nil { return err }
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUserAgent() string {
|
|
|
|
return "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.10; rv:75.0) Gecko/20100101 Firefox/75.0"
|
|
|
|
}
|