Home > database >  How to get Links field in http request GO
How to get Links field in http request GO

Time:10-12

Alright, I found this code snippet

import requests
from urllib.parse import parse_qs, urlparse

def get_commits_count(self, owner_name: str, repo_name: str) -> int:
    """
    Returns the number of commits to a GitHub repository.
    """
    url = f"https://api.github.com/repos/{owner_name}/{repo_name}/commits?per_page=1"
    r = requests.get(url)
    links = r.links
    rel_last_link_url = urlparse(links["last"]["url"])
    rel_last_link_url_args = parse_qs(rel_last_link_url.query)
    rel_last_link_url_page_arg = rel_last_link_url_args["page"][0]
    commits_count = int(rel_last_link_url_page_arg)
    return commits_count

And I need to do the same in GO. As it seems to me, I need to use something like "links" field (or whatever it is) in GO, but I can't find anything like this.

So I'm stuck because I don't know what represents "links" in http requests in GO

Thank you

CodePudding user response:

So, basically we need to take Link response header by doing response.Header["links"]

links := resp.Header["Link"]
if len(links) == 0 {
    return 0
}
var badlyParsedPageNumber = strings.Split(strings.Split(strings.Split(links[0], ", ")[1], "; ")[0],
    "&page=")[1]
lastPage := badlyParsedPageNumber[0 : len(badlyParsedPageNumber)-1]
  •  Tags:  
  • go
  • Related