UI can now add sources to the api

This commit is contained in:
James Tombleson 2024-07-11 08:09:32 -07:00
parent 574695bfb0
commit 7d46b60287
4 changed files with 83 additions and 2 deletions

View File

@ -16,6 +16,7 @@ const (
type Sources interface { type Sources interface {
ListAll(jwt string, page int) (domain.SourcesResponse, error) ListAll(jwt string, page int) (domain.SourcesResponse, error)
GetById(jwt string, id int64) (domain.SourcesResponse, error) GetById(jwt string, id int64) (domain.SourcesResponse, error)
NewRss(jwt, name, url, sourceType string) (domain.SourcesResponse, error)
} }
type sourceClient struct { type sourceClient struct {
@ -53,7 +54,7 @@ func (c sourceClient) ListAll(jwt string, page int) (domain.SourcesResponse, err
return bind, err return bind, err
} }
if (resp.StatusCode != 200) { if resp.StatusCode != 200 {
return bind, errors.New(bind.Message) return bind, errors.New(bind.Message)
} }
@ -69,9 +70,33 @@ func (c sourceClient) GetById(jwt string, id int64) (domain.SourcesResponse, err
return bind, err return bind, err
} }
if (statusCode != 200) { if statusCode != 200 {
return bind, errors.New(bind.Message) return bind, errors.New(bind.Message)
} }
return bind, nil return bind, nil
} }
func (c sourceClient) NewRss(jwt, name, url, sourceType string) (domain.SourcesResponse, error) {
param := domain.NewSourceParamRequest{
Name: name,
Url: url,
Tags: "",
}
bind := domain.SourcesResponse{}
var endpoint string
if sourceType == domain.SourceCollectorRss {
endpoint = fmt.Sprintf("%s/%s/new/rss", c.serverAddress, SourcesBaseRoute)
}
statusCode, err := PostBodyUrlAuthorized(c.client, endpoint, jwt, param, &bind)
if err != nil {
return bind, err
}
if statusCode != 200 {
return bind, errors.New("got the wrong status code back from the API")
}
return bind, nil
}

View File

@ -58,6 +58,35 @@ func PostUrlAuthorized(client http.Client, endpoint, jwtToken string, t any) err
return nil return nil
} }
func PostBodyUrlAuthorized(client http.Client, endpoint, jwtToken string, body any, t any) (int, error) {
jsonBody, err := json.Marshal(body)
if err != nil {
return 0, err
}
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(jsonBody))
if err != nil {
return 0, err
}
req.Header.Add(HeaderAuthorization, fmt.Sprintf("%s %s", "Bearer", jwtToken))
req.Header.Add(HeaderContentType, ApplicationJson)
//response, err := http.Post(endpoint, ApplicationJson, bytes.NewBuffer(jsonBody))
response, err := client.Do(req)
if err != nil {
return response.StatusCode, err
}
defer response.Body.Close()
decoder := json.NewDecoder(response.Body)
err = decoder.Decode(&t)
if err != nil {
return response.StatusCode, err
}
return response.StatusCode, nil
}
func PostBodyUrl(client http.Client, endpoint string, body any, t any) error { func PostBodyUrl(client http.Client, endpoint string, body any, t any) error {
jsonBody, err := json.Marshal(body) jsonBody, err := json.Marshal(body)
if err != nil { if err != nil {
@ -79,6 +108,22 @@ func PostBodyUrl(client http.Client, endpoint string, body any, t any) error {
return nil return nil
} }
func PostQuery(client http.Client, endpoint string, t any) (int, error) {
response, err := http.Post(endpoint, ApplicationJson, nil)
if err != nil {
return response.StatusCode, err
}
defer response.Body.Close()
decoder := json.NewDecoder(response.Body)
err = decoder.Decode(&t)
if err != nil {
return response.StatusCode, err
}
return response.StatusCode, nil
}
func Get(client http.Client, endpoint, jwt string, t any) (int, error) { func Get(client http.Client, endpoint, jwt string, t any) (int, error) {
req, err := http.NewRequest(http.MethodGet, endpoint, nil) req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil { if err != nil {

View File

@ -65,6 +65,7 @@ func NewServer(ctx context.Context, configs config.Configs, apiClient apiclient.
//sources.Use(ValidateJwtMiddleware(configs.JwtSecret)) //sources.Use(ValidateJwtMiddleware(configs.JwtSecret))
sources.GET("", s.ListAllSources) sources.GET("", s.ListAllSources)
sources.GET("/add", s.AddSource) sources.GET("/add", s.AddSource)
sources.POST("/add", s.AddSourceAfter)
users := router.Group("/users") users := router.Group("/users")
users.GET("/login", s.UserLogin) users.GET("/login", s.UserLogin)

View File

@ -36,3 +36,13 @@ func (h *Handler) AddSource(c echo.Context) error {
return Render(c, http.StatusOK, sources.Add(models.AddSourcePayloadModel{})) return Render(c, http.StatusOK, sources.Add(models.AddSourcePayloadModel{}))
} }
func (h *Handler) AddSourceAfter(c echo.Context) error {
name := c.FormValue("name")
url := c.FormValue("url")
resp, err := h.api.Sources.NewRss(GetJwtToken(c), name, url, "rss")
if err != nil {
return Render(c, http.StatusOK, sources.AddAfter(err.Error(), true))
}
return Render(c, http.StatusOK, sources.AddAfter(resp.Message, false))
}