choo-kity-pok / scripts

1 stars 0 forks source link

Download end point API #2

Open choo-kity-pok opened 1 year ago

choo-kity-pok commented 1 year ago

Go get the latest Geth release from GitHub repo [go-ethereum] https://github.com/ethereum/go-ethereum/releases

With a Python code made a request to the Github API releases/latest endpoint.

Documentation for API request: https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#get-the-latest-release

From the JavaScript [Code samples for "Get the latest release"] found on the URL under, convert with ChatGPT the code in Python.

To generate the token: https://docs.github.com/en/rest/guides/getting-started-with-the-rest-api?apiVersion=2022-11-28#about-tokens

The token we want to save it in a local environment variable. The token should not be included in the code, should be taken from the environment variable.

En utilisant Code Zsh-env ajoute la variable token.

I am supposed to have the proper Status 200

alekscp commented 1 year ago

Don't forget the git flow:

Work on a feature branch

Make regular commits

When done with changes on your branch, push it on Github

Assign me as PR reviewer so that I get a notification to check it out

alekscp commented 1 year ago

End goal:

python3 <script_name> should give us a similar output to:

{
  "url": "https://api.github.com/repos/octocat/Hello-World/releases/1",
  "html_url": "https://github.com/octocat/Hello-World/releases/v1.0.0",
  "assets_url": "https://api.github.com/repos/octocat/Hello-World/releases/1/assets",
  "upload_url": "https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}",
  "tarball_url": "https://api.github.com/repos/octocat/Hello-World/tarball/v1.0.0",
  "zipball_url": "https://api.github.com/repos/octocat/Hello-World/zipball/v1.0.0",
  "discussion_url": "https://github.com/octocat/Hello-World/discussions/90",
  "id": 1,
  "node_id": "MDc6UmVsZWFzZTE=",
  "tag_name": "v1.0.0",
  "target_commitish": "master",
  "name": "v1.0.0",
  "body": "Description of the release",
  "draft": false,
  "prerelease": false,
  "created_at": "2013-02-27T19:35:32Z",
  "published_at": "2013-02-27T19:35:32Z",
  "author": {
    "login": "octocat",
    "id": 1,
    "node_id": "MDQ6VXNlcjE=",
    "avatar_url": "https://github.com/images/error/octocat_happy.gif",
    "gravatar_id": "",
    "url": "https://api.github.com/users/octocat",
    "html_url": "https://github.com/octocat",
    "followers_url": "https://api.github.com/users/octocat/followers",
    "following_url": "https://api.github.com/users/octocat/following{/other_user}",
    "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
    "organizations_url": "https://api.github.com/users/octocat/orgs",
    "repos_url": "https://api.github.com/users/octocat/repos",
    "events_url": "https://api.github.com/users/octocat/events{/privacy}",
    "received_events_url": "https://api.github.com/users/octocat/received_events",
    "type": "User",
    "site_admin": false
  },
  "assets": [
    {
      "url": "https://api.github.com/repos/octocat/Hello-World/releases/assets/1",
      "browser_download_url": "https://github.com/octocat/Hello-World/releases/download/v1.0.0/example.zip",
      "id": 1,
      "node_id": "MDEyOlJlbGVhc2VBc3NldDE=",
      "name": "example.zip",
      "label": "short description",
      "state": "uploaded",
      "content_type": "application/zip",
      "size": 1024,
      "download_count": 42,
      "created_at": "2013-02-27T19:35:32Z",
      "updated_at": "2013-02-27T19:35:32Z",
      "uploader": {
        "login": "octocat",
        "id": 1,
        "node_id": "MDQ6VXNlcjE=",
        "avatar_url": "https://github.com/images/error/octocat_happy.gif",
        "gravatar_id": "",
        "url": "https://api.github.com/users/octocat",
        "html_url": "https://github.com/octocat",
        "followers_url": "https://api.github.com/users/octocat/followers",
        "following_url": "https://api.github.com/users/octocat/following{/other_user}",
        "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
        "organizations_url": "https://api.github.com/users/octocat/orgs",
        "repos_url": "https://api.github.com/users/octocat/repos",
        "events_url": "https://api.github.com/users/octocat/events{/privacy}",
        "received_events_url": "https://api.github.com/users/octocat/received_events",
        "type": "User",
        "site_admin": false
      }
    }
  ]
}
choo-kity-pok commented 1 year ago

I started to look at the Code samples for "Get the latest release"

The Code samples uses the Octokit class from the @octokit/core library. So the code samples uses a library which we have to import. When I try to convert the code samples from JavaScript to Python, ChatGPT makes me install requests so I can use import requests (import the necessary Python library).

The problem I see is that import requests allows Python code to send HTTP/1.1 requests but it is not encrypted by default. We have to use SSL/TLS to establish an encrypted connection between the client and the server.

"Whether the communication with the GitHub API is encrypted depends on the protocol used (e.g. HTTPS) and the security measures taken by the GitHub platform."

So considering we want to use this code snippet on a production environment, we have to know if the library we will import is encrypted.

alekscp commented 1 year ago

The request library will infer the correct protocol to make the request with based on the URI we pass it.

For example:

import requests
r = requests.get('https://some-url.com)
r.status_code # 200

See that the URI we pass to the get method of request starts with https. This means that we are requesting to make a request to a server that has SSL certificates (because the https protocol).

For our use case, the main thing to solve for you is to authenticate the request you will make to the Github endpoint using a github token.

See the headers taken from the JS example on Github's docs:

// Octokit.js
// https://github.com/octokit/core.js#readme
const octokit = new Octokit({
  auth: 'YOUR-TOKEN'
})

await octokit.request('GET /repos/{owner}/{repo}/releases/latest', {
  owner: 'OWNER',
  repo: 'REPO',
  headers: {
    'X-GitHub-Api-Version': '2022-11-28'
  }
})
choo-kity-pok commented 1 year ago

For the Token you can choose between "Token (Classic)" and "Fine-grained personal access tokens (Beta)", I chose the latest.

To save the token in a local environment variable, do we have to save it in my local machine or in the local machine of ethpower? On my Macbook terminal I use the shell Z-shell (zsh) but on our ethpower server we use bash shell.

For now I will add the token to my local machine (Macbook) in ~/.zshrc file.

alekscp commented 1 year ago

For the Token you can choose between "Token (Classic)" and "Fine-grained personal access tokens (Beta)", I chose the latest.

To save the token in a local environment variable, do we have to save it in my local machine or in the local machine of ethpower? On my Macbook terminal I use the shell Z-shell (zsh) but on our ethpower server we use bash shell.

For now I will add the token to my local machine (Macbook) in ~/.zshrc file.

The environment variable containing the token value needs to live on the machine where you'll be executing the script from. So if you are to run the script from your MacBook, then yes the token needs to be defined on that machine.

I would define it in your ~/.zshenv instead of your ~/.zshrc. The former is a good practice approach to define your customs ENV vars into.

choo-kity-pok commented 1 year ago

My interrogation is because the Geth update has to be done on the Ethpower server (not my machine). So if the environment variable containing the token live on my machine then it means, I will execute the script from my machine but the operations will be executed on the Ethpower machine?

choo-kity-pok commented 1 year ago

I would define it in your ~/.zshenv instead of your ~/.zshrc. The former is a good practice approach to define your customs ENV vars into.

Okay I moved the local env. variable for the token from ~/.zshrc to ~/.zshenv.

alekscp commented 1 year ago

My interrogation is because the Geth update has to be done on the Ethpower server (not my machine). So if the environment variable containing the token live on my machine then it means, I will execute the script from my machine but the operations will be executed on the Ethpower machine?

You are right indeed. We want the script to ultimately run on the ETHPower machine. But we are at the stage of developing the script. You could consider your Mac as the staging environment where we develop and break stuff until we are ready to deploy what we've built on the production environment (ETHPower server).

Once we are ready to deploy to production we will have to set up the required environment variables and other configs on the ETHPower server