client.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package httpserver
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "strings"
  7. )
  8. /*
  9. Retrieve the site audit config file from Semrush
  10. returns a byte array of the body, the content type of the resp, and an error
  11. */
  12. func (c *Controller) RetrieveStaticResource(path string) ([]byte, string, error) {
  13. url := fmt.Sprintf("https://static.semrush.com%s", path)
  14. req, err := http.NewRequest("GET", url, nil)
  15. if err != nil {
  16. return nil, "", err
  17. }
  18. req.AddCookie(c.Config.PhpSession)
  19. req.AddCookie(c.Config.SsoToken)
  20. resp, err := c.Client.Do(req)
  21. if err != nil {
  22. return nil, "", err
  23. }
  24. defer resp.Body.Close()
  25. b, err := io.ReadAll(resp.Body)
  26. if err != nil {
  27. return nil, "", err
  28. }
  29. return b, resp.Header.Get("content-type"), nil
  30. }
  31. /*
  32. Perform a call against the siteaudit api
  33. :param path: the URI path with the query
  34. :param query: the query to add to the request
  35. :returns a byte array of the response, the content type and an error
  36. */
  37. func (c *Controller) SiteauditApiCall(method string, path string, query string, body io.Reader) ([]byte, string, error) {
  38. query = strings.ReplaceAll(query, "https://sem.bunnytools.shop", "https://www.semrush.com")
  39. url := fmt.Sprintf("https://www.semrush.com%s?%s", path, query)
  40. fmt.Println(url)
  41. req, err := http.NewRequest(method, url, body)
  42. if err != nil {
  43. return nil, "", err
  44. }
  45. req.AddCookie(c.Config.PhpSession)
  46. req.AddCookie(c.Config.SsoToken)
  47. req.Header.Set("Referer", "https://www.semrush.com")
  48. req.Header.Set("Origin", "https://www.semrush.com")
  49. resp, err := c.Client.Do(req)
  50. if err != nil {
  51. return nil, "", err
  52. }
  53. defer resp.Body.Close()
  54. b, err := io.ReadAll(resp.Body)
  55. if err != nil {
  56. return nil, "", err
  57. }
  58. return b, resp.Header.Get("content-type"), nil
  59. }