Skip to content

Commit

Permalink
feat: Refactor endpoints and add GetStatus method in client
Browse files Browse the repository at this point in the history
Renamed HTTP routes for consistency and clarity (`/twitter/status` to `/status/twitter`). Added `GetStatus` method in the client to retrieve the status of specific services. Adjusted naming in `GetResult` for improved readability.
  • Loading branch information
restevens402 committed Dec 23, 2024
1 parent 87b3ab0 commit 4fd6613
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion internal/api/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func Start(ctx context.Context, listenAddress string, config types.JobConfigurat
}
}()

e.GET("/twitter/status", func(c echo.Context) error {
e.GET("/status/twitter", func(c echo.Context) error {
worker, exists := jobServer.GetWorker(jobs.TwitterScraperType)
if !exists {
return c.JSON(http.StatusNotFound, types.JobError{Error: "Twitter worker not found"})
Expand Down
22 changes: 20 additions & 2 deletions pkg/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (c *Client) Decrypt(encryptedResult string) (string, error) {
return string(body), nil
}

// GetJobResult retrieves the encrypted result of a job.
// GetResult retrieves the encrypted result of a job.
func (c *Client) GetResult(jobUUID string) (string, bool, error) {
resp, err := c.HTTPClient.Get(c.BaseURL + "/job/" + jobUUID)
if err != nil {
Expand All @@ -107,6 +107,24 @@ func (c *Client) GetResult(jobUUID string) (string, bool, error) {
if respErr.Error != "" {
err = fmt.Errorf("error: %s", respErr.Error)
}

return string(body), true, err
}

func (c *Client) GetStatus(serviceType string) (string, error) {
resp, err := c.HTTPClient.Get(fmt.Sprintf("%s/status/%s", c.BaseURL, serviceType))
if err != nil {
return "", fmt.Errorf("error sending GET request: %w", err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Printf("error closing response body: %v", err)
}
}(resp.Body)

body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading response body: %w", err)
}
return string(body), nil
}

0 comments on commit 4fd6613

Please sign in to comment.