From 7d46b602873a485744df9d68c65eafffe6c204ff Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Thu, 11 Jul 2024 08:09:32 -0700 Subject: [PATCH] UI can now add sources to the api --- apiclient/sources.go | 29 +++++++++++++++++++++-- apiclient/util.go | 45 ++++++++++++++++++++++++++++++++++++ internal/handlers/handler.go | 1 + internal/handlers/sources.go | 10 ++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/apiclient/sources.go b/apiclient/sources.go index 5af6d9f..0d9f9b7 100644 --- a/apiclient/sources.go +++ b/apiclient/sources.go @@ -16,6 +16,7 @@ const ( type Sources interface { ListAll(jwt string, page int) (domain.SourcesResponse, error) GetById(jwt string, id int64) (domain.SourcesResponse, error) + NewRss(jwt, name, url, sourceType string) (domain.SourcesResponse, error) } type sourceClient struct { @@ -53,7 +54,7 @@ func (c sourceClient) ListAll(jwt string, page int) (domain.SourcesResponse, err return bind, err } - if (resp.StatusCode != 200) { + if resp.StatusCode != 200 { return bind, errors.New(bind.Message) } @@ -69,9 +70,33 @@ func (c sourceClient) GetById(jwt string, id int64) (domain.SourcesResponse, err return bind, err } - if (statusCode != 200) { + if statusCode != 200 { return bind, errors.New(bind.Message) } 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 +} diff --git a/apiclient/util.go b/apiclient/util.go index 8be09a0..d161eb9 100644 --- a/apiclient/util.go +++ b/apiclient/util.go @@ -58,6 +58,35 @@ func PostUrlAuthorized(client http.Client, endpoint, jwtToken string, t any) err 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 { jsonBody, err := json.Marshal(body) if err != nil { @@ -79,6 +108,22 @@ func PostBodyUrl(client http.Client, endpoint string, body any, t any) error { 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) { req, err := http.NewRequest(http.MethodGet, endpoint, nil) if err != nil { diff --git a/internal/handlers/handler.go b/internal/handlers/handler.go index 500554c..cc2d951 100644 --- a/internal/handlers/handler.go +++ b/internal/handlers/handler.go @@ -65,6 +65,7 @@ func NewServer(ctx context.Context, configs config.Configs, apiClient apiclient. //sources.Use(ValidateJwtMiddleware(configs.JwtSecret)) sources.GET("", s.ListAllSources) sources.GET("/add", s.AddSource) + sources.POST("/add", s.AddSourceAfter) users := router.Group("/users") users.GET("/login", s.UserLogin) diff --git a/internal/handlers/sources.go b/internal/handlers/sources.go index 5a23c69..04e2533 100644 --- a/internal/handlers/sources.go +++ b/internal/handlers/sources.go @@ -36,3 +36,13 @@ func (h *Handler) AddSource(c echo.Context) error { 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)) +} \ No newline at end of file