Ü7: Github api

Ülesanne eesmärk

Eesmärk: õppida võtma andmeid GitHubi avalikust API-st, kasutades JavaScripti fetch() päringut, ning kuvada saadud kasutajaprofiili andmed veebilehel.

Teooria

API (Application Programming Interface) võimaldab rakendustel omavahel andmeid vahetada. GitHub pakub avalikku REST API-t, millest saab päringutega kätte kasutajate profiiliinfot.

  • fetch() meetod: Kasutatakse veebipäringute tegemiseks ja andmete laadimiseks taustal ilma lehte uuesti laadimata.
  • JSON formaat: API tagastab andmed JSON-objektina, kust saab kätte vajalikud võtmed (nt login, id, html_url, public_repos).
  • Sündmuse kuulaja (change / input): Võimaldab reageerida kasutaja sisestusele tekstikastis ja käivitada andmete uue päringu.

Praktika

1.Loo CodeSandbox keskkonnas uus JS projekt.

2. Sisesta faili index.mjs rakenduse kood koos API päringu ja andmete sidumisega

let givenProfile = "";
let profileName = "";
let profileId = "";
let profileLink = "";
let profileRepos = "";

function renderPage() {
  document.getElementById("app").innerHTML = `
    <div>
      <h1>Github profile viewer</h1>
      <p>Please enter profile name:</p>
      <input type="text" value="${givenProfile}" />
      <div class="content">
        <h1 id="name">Name: ${profileName}</h1>
        <p id="id">Id: ${profileId}</p>
        <p id="repos">Public repos: ${profileRepos}</p>
        <p id="profileurl">Link: <a href="${profileLink}" target="_blank">${profileLink}</a></p>
      </div>
    </div>
  `;

  const input = document.querySelector("input");
  if (input) {
    input.addEventListener("change", updateValue);
  }
}

function updateValue(e) {
  givenProfile = e.target.value;
  fetchProfile();
}

let fetchProfile = async () => {
  let fetchedData;

  await fetch(`https://api.github.com/users/${givenProfile}`)
    .then((response) => response.json())
    .then((data) => (fetchedData = data));

  profileName = fetchedData.name || fetchedData.login || "";
  profileId = fetchedData.id || "";
  profileLink = fetchedData.html_url || "";
  profileRepos = fetchedData.public_repos || 0;

  renderPage();
};

renderPage();

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript Sandbox</title>
    <meta charset="UTF-8" />
    <link rel="stylesheet" href="./styles.css" />
  </head>

  <body>
    <div id="app"></div>

    <script src="./index.mjs" type="module"></script>
  </body>
</html>

styles.css

body {
  font-family: Arial, sans-serif;
  background-color: #f4f4f9;
  display: flex;
  justify-content: center;
  padding: 40px 20px;
  color: #333;
}

#app {
  background: #ffffff;
  padding: 25px;
  border-radius: 8px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

h1 {
  font-size: 22px;
  margin-bottom: 15px;
}

input {
  width: 100%;
  padding: 10px;
  font-size: 14px;
  border: 1px solid #ccc;
  border-radius: 5px;
  box-sizing: border-box;
  margin-bottom: 20px;
}

.content h1 {
  font-size: 20px;
  color: #2c3e50;
}

p {
  margin: 8px 0;
  font-size: 15px;
}

a {
  color: #0066cc;
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

Tulemused

Testi rakendust, sisestades sisendväljale GitHubi kasutajanime. Rakendus teeb päringu GitHubi API-sse ja kuvab profiili andmed.