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

ФТ-202 Красиков Городничая #75

Open
wants to merge 10 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
160 changes: 130 additions & 30 deletions client.mjs
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
export class Client {
/**
* Должен возвращать имя пользователя или null
* если пользователь не залогинен
*
* @return {Promise<string | null>} username
* */
constructor() {
this.username = null;
}

async getUser() {
throw new Error("Not implemented");
const response = await fetch("/api/user", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
const data = await response.json();
this.username = data.username;
return this.username;
}

/**
* Должен логинить пользователя с именем username
* и возвращать его имя
*
* @param {string} username
* @return {Promise<string | null>} username
* */
async loginUser(username) {
throw new Error("Not implemented");
const response = await fetch("/api/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ username }),
});
const data = await response.json();
this.username = username;
return this.username;
}

/**
* Должен разлогинивать текущего пользователя
*
* @return {void}
* */
async logoutUser() {
throw new Error("Not implemented");
const response = await fetch("/api/logout", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
});
const data = await response.json();
this.username = null;
}

// Добавляем middleware для разбора JSON-тела запроса
handleJSON() {
return express.json();
}

/**
Expand All @@ -50,7 +65,23 @@ 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();
return {
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
};
}

/**
Expand All @@ -63,7 +94,12 @@ 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();
return data.map(event => ({
id: event.id,
title: event.title
}));
}

/**
Expand All @@ -80,7 +116,15 @@ 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();
return {
id: data.id,
title: data.title,
event_date_utc: data.event_date_utc,
details: data.details,
links: data.links
};
}

/**
Expand All @@ -93,7 +137,12 @@ 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();
return data.map(rocket => ({
rocket_id: rocket.id,
rocket_name: rocket.name
}));
}

/**
Expand All @@ -118,7 +167,22 @@ 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();
return {
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
};
}

/**
Expand All @@ -135,7 +199,16 @@ 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();
return {
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
};
}

/**
Expand All @@ -152,7 +225,10 @@ export class Client {
* @return {Promise<Item[]>}
* */
async getSentToMars() {
throw new Error("Not implemented");
const response = await fetch(`/api/list`);
if (response.ok) {
return response.json();
}
}

/**
Expand All @@ -170,7 +246,19 @@ export class Client {
* @return {Promise<Item[]>}
* */
async sendToMars(item) {
throw new Error("Not implemented");
const response = await fetch(`/api/send`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
item: item
}),
});
if (response.ok) {
return response.json();
}
}

/**
Expand All @@ -181,6 +269,18 @@ export class Client {
* @return {Promise<Item[]>}
* */
async cancelSendingToMars(item) {
throw new Error("Not implemented");
const response = await fetch(`/api/cancel`, {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: item.id,
}),
});
if (response.ok) {
return response.json();
}
}
}
Loading