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

Джевелло Алексей, Лапшин Евгений, Сыч Эрнест - ФТ201 #81

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 84 additions & 9 deletions client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export class Client {
* @return {Promise<string | null>} username
* */
async getUser() {
throw new Error("Not implemented");
const {username} = await (await fetch("/api/getUser")).json();
return username;
}

/**
Expand All @@ -17,7 +18,15 @@ export class Client {
* @return {Promise<string | null>} username
* */
async loginUser(username) {
throw new Error("Not implemented");
const response = await fetch("/api/loginUser", {
method: "POST",
body: JSON.stringify({username: username}),
headers: {
"content-type": "application/json"
}
});
username = (await response.json())['username'];
return username;
}

/**
Expand All @@ -26,7 +35,9 @@ export class Client {
* @return {void}
* */
async logoutUser() {
throw new Error("Not implemented");
await fetch('/api/logoutUser', {
method: "DELETE"
});
}

/**
Expand All @@ -50,7 +61,24 @@ export class Client {
* @return {Promise<About>}
* */
async getInfo() {
throw new Error("Not implemented");
const response = await fetch("https://api.spacexdata.com/v4/company");
const data = await response.json();
const about = {
"founder": data.founder,
"founded": data.founded,
"employees": data.employees,
"ceo": data.ceo,
"coo": data.coo,
"cto": data.cto,
"valuation": data.valuation,
"headquarters": {
"address": data.headquarters.address,
"city": data.headquarters.city,
"state": data.headquarters.state
},
"summary": data.summary
};
return about;
}

/**
Expand All @@ -63,7 +91,13 @@ export class Client {
* @return {Promise<EventBrief[]>}
* */
async getHistory() {
throw new Error("Not implemented");
const response = await fetch("https://api.spacexdata.com/v4/history");
const data = await response.json();
const eventBrief = data.map(event => ({
"id": event.id,
"title": event.title
}));
return eventBrief;
}

/**
Expand All @@ -80,7 +114,16 @@ export class Client {
* @return {Promise<EventFull>}
* */
async getHistoryEvent(id) {
throw new Error("Not implemented");
const response = await fetch(`https://api.spacexdata.com/v4/history/${id}`);
const data = await response.json();
const eventFull = {
"id": data.id,
"title": data.title,
"event_date_utc": data.event_date_utc,
"details": data.details,
"links": data.links
};
return eventFull;
}

/**
Expand All @@ -93,7 +136,13 @@ export class Client {
* @return {Promise<RocketBrief[]>}
* */
async getRockets() {
throw new Error("Not implemented");
const response = await fetch("https://api.spacexdata.com/v4/rockets");
const data = await response.json();
const rocketBrief = data.map(rocket => ({
"rocket_id": rocket.id,
"rocket_name": rocket.name
}));
return rocketBrief;
}

/**
Expand All @@ -118,7 +167,23 @@ export class Client {
* @return {Promise<RocketFull>}
* */
async getRocket(id) {
throw new Error("Not implemented");
const response = await fetch(`https://api.spacexdata.com/v4/rockets/${id}`);
const data = await response.json();
const rocketFull = {
"rocket_id": data.id,
"rocket_name": data.name,
"first_flight": data.first_flight,
"description": data.description,
"wikipedia": data.wikipedia,
"flickr_images": data.flickr_images,
"height": data.height,
"diameter": data.diameter,
"mass": data.mass,
"engines": data.engines,
"first_stage": data.first_stage,
"second_stage": data.second_stage
};
return rocketFull;
}

/**
Expand All @@ -135,7 +200,17 @@ export class Client {
* @return {Promise<Roadster>}
* */
async getRoadster() {
throw new Error("Not implemented");
const response = await fetch("https://api.spacexdata.com/v4/roadster");
const data = await response.json();
const roadster = {
"name": data.name,
"launch_date_utc": data.launch_date_utc,
"details": data.details,
"earth_distance_km": data.earth_distance_km,
"mars_distance_km": data.mars_distance_km,
"wikipedia": data.wikipedia
};
return roadster;
}

/**
Expand Down
Loading