Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Step 1: Setup GitHub REST API Requests 🤙 #17

Open
2 tasks done
nelsonic opened this issue Oct 7, 2022 · 4 comments
Open
2 tasks done

Step 1: Setup GitHub REST API Requests 🤙 #17

nelsonic opened this issue Oct 7, 2022 · 4 comments
Assignees
Labels
enhancement New feature or enhancement of existing functionality priority-2 Second highest priority, should be worked on as soon as the Priority-1 issues are finished tech-debt A feature/requirement implemented in a sub-optimal way & must be re-written technical A technical issue that requires understanding of the code, infrastructure or dependencies

Comments

@nelsonic
Copy link
Member

nelsonic commented Oct 7, 2022

In order to get data from GitHub we need to setup the API.

According to the docs: https://docs.github.com/en/rest/overview/resources-in-the-rest-api 👀
the rate limiting for authenticated requests is 5,000/hour. 5️⃣ 🦘
That should be more than "enough" for what I have in mind. 📈
But if we need more we can easily add more API users. 💭

We've made HTTP Requests to the GitHub API before in previous projects:

So I think I can piece this together from previous code. 🤞

Todo

  • Setup GitHub API access and make a test request
  • Use our existing knowledge/tools to fetch a list of @dwyl Org Members from the GitHub API
@nelsonic nelsonic added the enhancement New feature or enhancement of existing functionality label Oct 7, 2022
@nelsonic nelsonic self-assigned this Oct 7, 2022
@nelsonic
Copy link
Member Author

nelsonic commented Oct 8, 2022

Decided to use https://github.com/edgurgel/tentacat for the GitHub API requests as it appears to be maintained. 👍

@nelsonic
Copy link
Member Author

nelsonic commented Oct 9, 2022

Used tentacat for basic requests and they worked ... 🎉
but then I re-read the GitHub REST API Docs:
https://docs.github.com/en/rest/orgs/members#list-organization-members
github-api-pagination
The GET /orgs/{org}/members request defaults to returning 30 members ...

@dwyl currently has 525 members: https://github.com/orgs/dwyl/people

github-org-users

We definitely need to paginate the request:

github-org-users-pagination-required

if we're unable to paginate the results this approach is a non-starter. 🤷‍♂️

Tentacat.Organizations.Members.list/2 function signature has only client and organisation arguments:
https://github.com/edgurgel/tentacat/blob/dfe89f33350acdf1ddea4a6f4d4338932fca0875/lib/tentacat/organizations/members.ex#L64-L67

 @doc """ 
 List members of a `organization`. 

 The response will differ if the authenticated user is also owner of the 
 organization. 

 ## Example 

     Tentacat.Organizations.Members.list "github" 
     Tentacat.Organizations.Members.list client, "github" 

 More info at: http://developer.github.com/v3/orgs/members/#members-list 
 """ 
 @spec list(Client.t(), binary) :: Tentacat.response() 
 def list(client \\ %Client{}, organization) do 
   get("orgs/#{organization}/members", client) 
 end 

Opened issue to confirm my understanding: edgurgel/tentacat#210 🤞

@nelsonic
Copy link
Member Author

Might be able to use Tentacat.get/3 generic HTTP function to manually get the endpoint.

https://github.com/edgurgel/tentacat/blob/dfe89f33350acdf1ddea4a6f4d4338932fca0875/lib/tentacat.ex#L55-L95

@doc """
Underlying utility retrieval function. The options passed affect both the
return value and, ultimately, the number of requests made to GitHub.

## Options

  * `:pagination` - Can be `:none`, `:manual`, `:stream`, or `:auto`. Defaults to :auto.

      - `:none` will only return the first page. You won't have access to the
        headers to manually paginate.

      - `:auto` will block until all the pages have been retrieved and
        concatenated together. Most of the time, this is what you want. For
        example, `Tentacat.Repositories.list_users("chrismccord")` and
        `Tentacat.Repositories.list_users("octocat")` have the same interface
        though one call will page many times and the other not at all.

      - `:stream` will return a `Stream`, prepopulated with the first page.

      - `:manual` will return a 3 element tuple of `{page_body,
        url_for_next_page, auth_credentials}`, which will allow you to control
        the paging yourself.
"""
@spec get(binary, Client.t()) :: response
@spec get(binary, Client.t(), keyword) :: response
@spec get(binary, Client.t(), keyword, keyword) ::
        response | Enumerable.t() | pagination_response
def get(path, client, params \\ [], options \\ []) do
  url =
    client
    |> url(path)
    |> add_params_to_url(params)


  case pagination(options) do
    nil -> request_stream(:get, url, client.auth)
    :none -> request_stream(:get, url, client.auth, "", :one_page)
    :auto -> request_stream(:get, url, client.auth)
    :stream -> request_stream(:get, url, client.auth, "", :stream)
    :manual -> request_with_pagination(:get, url, client.auth)
  end
end

Thanks to edgurgel/tentacat#190 (comment) (comment on super-stale PR) 👀

Going to take a look at this on the Weekend. 🤞
Need to park this for now as it's lower priority than building the App/MVP. 😞

@nelsonic nelsonic added technical A technical issue that requires understanding of the code, infrastructure or dependencies tech-debt A feature/requirement implemented in a sub-optimal way & must be re-written priority-2 Second highest priority, should be worked on as soon as the Priority-1 issues are finished labels Oct 10, 2022
@nelsonic
Copy link
Member Author

Going to try and cURL my way into figuring out the pagination ...

curl \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer <YOUR-TOKEN>" \
  https://api.github.com/orgs/dwyl/members

Again, one for Saturday. ⏳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or enhancement of existing functionality priority-2 Second highest priority, should be worked on as soon as the Priority-1 issues are finished tech-debt A feature/requirement implemented in a sub-optimal way & must be re-written technical A technical issue that requires understanding of the code, infrastructure or dependencies
Projects
None yet
Development

No branches or pull requests

1 participant