Skip to content

Commit

Permalink
add: GetProject
Browse files Browse the repository at this point in the history
  • Loading branch information
CorentinB committed Nov 29, 2024
1 parent 560daa7 commit f0a189e
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion add.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (c *Client) Add(URLs []URL, bypassSeencheck bool) (err error) {
}

// build request
req, err := http.NewRequest("POST", c.URLsEndpoint.String(), bytes.NewReader(jsonPayload))
req, err := http.NewRequest(http.MethodPost, c.URLsEndpoint.String(), bytes.NewReader(jsonPayload))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (c *Client) Delete(URLs []URL, localCrawls int) (err error) {
}

// build request
req, err := http.NewRequest("DELETE", c.URLsEndpoint.String(), bytes.NewReader(jsonPayload))
req, err := http.NewRequest(http.MethodDelete, c.URLsEndpoint.String(), bytes.NewReader(jsonPayload))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion get.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (c *Client) Get(size int, strategy string) (URLs []URL, err error) {
emptyStatusCode := 204

// build request
req, err := http.NewRequest("GET", c.URLsEndpoint.String(), nil)
req, err := http.NewRequest(http.MethodGet, c.URLsEndpoint.String(), nil)
if err != nil {
return URLs, err
}
Expand Down
6 changes: 6 additions & 0 deletions gocrawlhq.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ func Init(key, secret, project, address, identifier string) (c *Client, err erro
return c, err
}

c.ProjectEndpoint, err = url.Parse(c.HQAddress)
if err != nil {
return c, err
}

c.URLsEndpoint.Path = path.Join(c.URLsEndpoint.Path, "api", "projects", c.Project, "urls")
c.SeencheckEndpoint.Path = path.Join(c.SeencheckEndpoint.Path, "api", "projects", c.Project, "seencheck")
c.ResetEndpoint.Path = path.Join(c.ResetEndpoint.Path, "api", "projects", c.Project, "reset")
c.ProjectEndpoint.Path = path.Join(c.ProjectEndpoint.Path, "api", "projects", c.Project)

return c, nil
}
1 change: 1 addition & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Client struct {
URLsEndpoint *url.URL
SeencheckEndpoint *url.URL
ResetEndpoint *url.URL
ProjectEndpoint *url.URL
HTTPClient *http.Client
WebsocketConn *net.Conn
}
Expand Down
49 changes: 49 additions & 0 deletions project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package gocrawlhq

import (
"encoding/json"
"net/http"
)

type Project struct {
Paused bool `json:"paused"`
Name string `json:"name"`
Exclusions []string `json:"exclusions"`
SeencheckEnabled bool `json:"seencheck_enabled"`
SeencheckTTL int `json:"seencheck_ttl"`
Stats struct {
Pending int `json:"pending"`
Processing int `json:"processing"`
Completed int `json:"completed"`
} `json:"stats"`
}

func (c *Client) GetProject() (p *Project, err error) {
expectedStatusCodes := 200

req, err := http.NewRequest(http.MethodGet, c.ProjectEndpoint.String(), nil)
if err != nil {
return p, err
}

req.Header.Add("X-Auth-Key", c.Key)
req.Header.Add("X-Auth-Secret", c.Secret)
req.Header.Add("User-Agent", "gocrawlhq/"+Version)

if c.Identifier != "" {
req.Header.Add("X-Identifier", c.Identifier)
}

resp, err := c.HTTPClient.Do(req)
if err != nil {
return p, err
}
defer resp.Body.Close()

if resp.StatusCode != expectedStatusCodes {
return p, err
}

err = json.NewDecoder(resp.Body).Decode(&p)
return p, err
}
2 changes: 1 addition & 1 deletion reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func (c *Client) Reset() (err error) {
expectedStatusCode := 202

req, err := http.NewRequest("POST", c.ResetEndpoint.String(), nil)
req, err := http.NewRequest(http.MethodPost, c.ResetEndpoint.String(), nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion seencheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (c *Client) Seencheck(URLs []URL) (outputURLs []URL, err error) {
return URLs, err
}

req, err := http.NewRequest("POST", c.SeencheckEndpoint.String(), bytes.NewReader(jsonPayload))
req, err := http.NewRequest(http.MethodPost, c.SeencheckEndpoint.String(), bytes.NewReader(jsonPayload))
if err != nil {
return URLs, err
}
Expand Down

0 comments on commit f0a189e

Please sign in to comment.