Данный раздел содержит документацию Star Wars API AJAX сервера Dan.IT Education. Здесь будут описаны примеры запросов на сервер используя Fetch API, все возможные cущности и как с ними работать.
Всего есть 6 сущностей:
Все они связаны между собой. Все 6 сущностей поддерживают следующие операции:
get https://ajax.test-danit.com/api/swapi/{entity} - получить список всех сущностей данного типа
get https://ajax.test-danit.com/api/swapi/{entity}/{id} - получить одну сущность по уникальному ID
get https://ajax.test-danit.com/api/swapi/{entity}?search={keyword} - получить список всех
сущностей определенного типа, у которых в поле name встречается строка keyword.
Подробнее о поиске Вы можете узнать в разделе "Поиск" ниже.
fetch("http://ajax.test-danit.com/api/swapi/starships/2")
.then(response => response.json())
.then(json => console.log(json))
// Output
{
"id": 2,
"mglt": "60",
"cargoCapacity": 3000000,
"consumables": "1 year",
"costInCredits": 3500000,
"crew": "30-165",
"hyperdriveRating": "2.0",
"length": 150.0,
"manufacturer": "Corellian Engineering Corporation",
"maxAtmosphericSpeed": 950.0,
"model": "CR90 corvette",
"name": "CR90 corvette",
"passengers": 600,
"films": [
"https://ajax.test-danit.com/api/swapi/films/1",
"https://ajax.test-danit.com/api/swapi/films/3",
"https://ajax.test-danit.com/api/swapi/films/6"
],
"pilots": [],
"starshipClass": "corvette",
"url": "https://ajax.test-danit.com/api/swapi/starships/2"
}
fetch("http://ajax.test-danit.com/api/swapi/starships")
.then(response => response.json())
.then(json => console.log(json))
// Output
[
{"id": 1,"mglt": 60, /* ... */},
{"id": 2, "mglt": 70, /* ... */},
/* ... */
]
Давайте сделаем свой первый запрос к Star Wars API от DAN.IT education!
Корневой URL-адрес - это корень всего API. Если Вы когда-нибудь увидите ошибку 404, то стоит сначала проверить Корневой URL-адрес.
Корневой URL-адрес SWAPI сервера DAN.IT education:
https://ajax.test-danit.com/api/swapi
Документация ниже предполагает, что Вы будете использовать упомянутый корневой URL-адрес перед всеми эндпоинтами.
Все ресурсы поддерживают параметр search, который отфильтровывает возвращаемый набор ресурсов по полю name. Это позволяет делать запросы по типу:
https://ajax.test-danit.com/api/swapi/people?search=r2
В результате всех поисковых запросов будут отображены регистро-независимые частичные совпадения в поле name.
Корневой ресурс предоставляет информацию о всех доступных ресурсах в API.
Пример запроса:
fetch("https://ajax.test-danit.com/api/swapi/")
.then(response => response.json())
.then(json => console.log(json))
Пример ответа:
{
"films": "https://ajax.test-danit.com/api/swapi/films",
"people": "https://ajax.test-danit.com/api/swapi/people",
"planets": "https://ajax.test-danit.com/api/swapi/planets",
"species": "https://ajax.test-danit.com/api/swapi/species",
"starships": "https://ajax.test-danit.com/api/swapi/starships",
"vehicles": "https://ajax.test-danit.com/api/swapi/vehicles"
}
Атрибуты:
Ресурс "People" - это отдельный человек или персонаж из вселенной "Звездных войн".
Эндпоинты:
Пример запроса:
fetch("https://ajax.test-danit.com/api/swapi/people/1")
.then(response => response.json())
.then(json => console.log(json))
Пример ответа:
{
"id": 1,
"name": "Luke Skywalker",
"height": 172,
"mass": 77,
"hairColor": "blond",
"skinColor": "fair",
"eyeColor": "blue",
"birthYear": "19BBY",
"gender": "male",
"homeworld": "https://ajax.test-danit.com/api/swapi/planets/1/",
"films": [
"https://ajax.test-danit.com/api/swapi/1/",
"https://ajax.test-danit.com/api/swapi/2/",
"https://ajax.test-danit.com/api/swapi/3/",
"https://ajax.test-danit.com/api/swapi/6/"
],
"species": [],
"vehicles": [
"https://ajax.test-danit.com/api/swapi/vehicles/14/",
"https://ajax.test-danit.com/api/swapi/vehicles/30/"
],
"starships": [
"https://ajax.test-danit.com/api/swapi/starships/12/",
"https://ajax.test-danit.com/api/swapi/starships/22/"
],
"created": "2014-12-09T13:50:51.644000Z",
"edited": "2014-12-20T21:17:56.891000Z",
"url": "https://ajax.test-danit.com/api/swapi/people/1/"
}
Атрибуты:
Ресурс "Films" представляет собой единичный фильм из вселенной "Звездных Войн".
Эндпоинты:
Пример запроса:
fetch("https://ajax.test-danit.com/api/swapi/films/1")
.then(response => response.json())
.then(json => console.log(json))
Пример ответа:
{
"id": 1,
"characters": [
"https://ajax.test-danit.com/api/swapi/people/1",
"https://ajax.test-danit.com/api/swapi/people/2",
/* ... */
],
"director": "George Lucas",
"episodeId": 4,
"openingCrawl": "It is a period of civil war. Rebel spaceships, striking from a hidden base,
have won their first victory against the evil Galactic Empire.
During the battle, Rebel spies managed to steal secret plans
to the Empire`s ultimate weapon, the DEATH STAR, an armored
space station with enough powerto destroy an entire planet.
Pursued by the Empire`s sinister agents, Princess Leia races home aboard her
starship, custodian of the stolen plans that can save her people and
restore freedom to the galaxy...",
"planets": [
"https://ajax.test-danit.com/api/swapi/planets/1",
"https://ajax.test-danit.com/api/swapi/planets/2",
"https://ajax.test-danit.com/api/swapi/planets/3"
],
"producer": "Gary Kurtz, Rick McCallum",
"releaseDate": "1977-05-25",
"species": [
"https://ajax.test-danit.com/api/swapi/species/1",
"https://ajax.test-danit.com/api/swapi/species/2",
/* ... */
],
"starships": [
"https://ajax.test-danit.com/api/swapi/starships/2",
"https://ajax.test-danit.com/api/swapi/starships/3",
/* ... */
],
"name": "A New Hope",
"url": "https://ajax.test-danit.com/api/swapi/films/1",
"vehicles": [
"https://ajax.test-danit.com/api/swapi/vehicles/4",
"https://ajax.test-danit.com/api/swapi/vehicles/6",
/* ... */
]
}
Атрибуты:
Ресурс "Starships" - это единичный транспортный корабль, обладающий гипердвигателем.
Эндпоинты:
Пример запроса:
fetch("https://ajax.test-danit.com/api/swapi/starships/2")
.then(response => response.json())
.then(json => console.log(json))
Пример ответа:
{
"id": 2,
"mglt": "60",
"cargoCapacity": 3000000,
"consumables": "1 year",
"costInCredits": 3500000,
"crew": "30-165",
"hyperdriveRating": "2.0",
"length": 150.0,
"manufacturer": "Corellian Engineering Corporation",
"maxAtmosphericSpeed": 950.0,
"model": "CR90 corvette",
"name": "CR90 corvette",
"passengers": 600,
"films": [
"https://ajax.test-danit.com/api/swapi/films/1",
"https://ajax.test-danit.com/api/swapi/films/3",
"https://ajax.test-danit.com/api/swapi/films/6"
],
"pilots": [],
"starshipClass": "corvette",
"url": "https://ajax.test-danit.com/api/swapi/starships/2"
}
Атрибуты:
Ресурс "Vehicles" - это транспортная еденица, не имеющая гипердвигателя.
Эндпоинты:
Пример запроса:
fetch("https://ajax.test-danit.com/api/swapi/vehicles/4")
.then(response => response.json())
.then(json => console.log(json))
Пример ответа:
{
"id": 4,
"cargoCapacity": 50000,
"consumables": "2 months",
"costInCredits": 150000,
"crew": "46",
"length": 36.8,
"manufacturer": "Corellian Engineering Corporation",
"maxAtmosphericSpeed": 30.0,
"model": "Digger Crawler",
"name": "Sand Crawler",
"passengers": 30,
"films": [
"https://ajax.test-danit.com/api/swapi/films/1",
"https://ajax.test-danit.com/api/swapi/films/5"
],
"pilots": [],
"vehicleClass": "wheeled",
"url": "https://ajax.test-danit.com/api/swapi/vehicles/4"
}
Атрибуты:
Ресурс "Species" - это отдельный вид человека или персонажа из вселенной "Звездных войн".
Эндпоинты:
Пример запроса:
fetch("https://ajax.test-danit.com/api/swapi/species/1")
.then(response => response.json())
.then(json => console.log(json))
Пример ответа:
{
"id": 1,
"averageHeight": 180.0,
"averageLifespan": 120,
"classification": "mammal",
"designation": "sentient",
"eyeColors": "brown, blue, green, hazel, grey, amber",
"hairColors": "blonde, brown, black, red",
"homeworld": "https://ajax.test-danit.com/api/swapi/planets/9",
"language": "Galactic Basic",
"name": "Human",
"people": [
"https://ajax.test-danit.com/api/swapi/people/66",
"https://ajax.test-danit.com/api/swapi/people/67",
/* ... */
],
"films": [
"https://ajax.test-danit.com/api/swapi/films/1",
"https://ajax.test-danit.com/api/swapi/films/2",
/* ... */
],
"skinColors": "caucasian, black, asian, hispanic",
"url": "https://ajax.test-danit.com/api/swapi/species/1"
}
Атрибуты:
Ресурс "Planets" - это массивное космическое тело, планета или планетоид во Вселенной Звездных войн на момент 0 ABY.
Эндпоинты:
Пример запроса:
fetch("https://ajax.test-danit.com/api/swapi/planets/1")
.then(response => response.json())
.then(json => console.log(json))
Пример ответа:
{
"id": 1,
"climate": "arid",
"diameter": 10465,
"films": [
"https://ajax.test-danit.com/api/swapi/films/1",
"https://ajax.test-danit.com/api/swapi/films/3",
/* ... */
],
"gravity": "1 standard",
"name": "Tatooine",
"orbitalPeriod": 304,
"population": 200000,
"residents": [
"https://ajax.test-danit.com/api/swapi/people/1",
"https://ajax.test-danit.com/api/swapi/people/2",
/* ... */
],
"rotationPeriod": 23.0,
"surfaceWater": 1.0,
"terrain": "desert",
"url": "https://ajax.test-danit.com/api/swapi/planets/1"
}
Атрибуты: