Przykłady kodu
Przykłady cURL
Żądanie logowania
curl -X POST https://landlordkeeper.com/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"email": "[email protected]", "password": "YourPassword123"}'
Uzyskaj profil
curl -X GET https://landlordkeeper.com/api/v1/user/profile \
-H "Authorization: Bearer YOUR_JWT_TOKEN"
Aktualizuj profil
curl -X PUT https://landlordkeeper.com/api/v1/user/profile \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d '{"name": "New Name", "phone": "+380501234567"}'
Przykłady JavaScript
Funkcja logowania
// Login and get token
async function login(email, password) {
const response = await fetch('/api/v1/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email, password }),
});
if (!response.ok) {
throw new Error('Login failed');
}
const data = await response.json();
localStorage.setItem('token', data.token);
return data.token;
}
Pomocnik uwierzytelnionego żądania
// Make authenticated request
async function apiRequest(endpoint, options = {}) {
const token = localStorage.getItem('token');
const response = await fetch(`/api${endpoint}`, {
...options,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
...options.headers,
},
});
if (response.status === 401) {
// Token expired - redirect to login
localStorage.removeItem('token');
window.location.href = '/login';
return;
}
return response.json();
}
// Usage
const profile = await apiRequest('/user/profile');
console.log(profile);
Przykłady PHP
Korzystanie z klienta HTTP Guzzle
<?php
use GuzzleHttp\Client;
$client = new Client(['base_uri' => 'https://landlordkeeper.com']);
// Login
$response = $client->post('/api/v1/auth/login', [
'json' => [
'email' => '[email protected]',
'password' => 'YourPassword123',
],
]);
$data = json_decode($response->getBody(), true);
$token = $data['token'];
// Get profile
$response = $client->get('/api/v1/user/profile', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
$profile = json_decode($response->getBody(), true);
print_r($profile);
Przykłady Pythona
Korzystanie z biblioteki żądań
import requests
BASE_URL = "https://landlordkeeper.com"
# Login
response = requests.post(f"{BASE_URL}/api/v1/auth/login", json={
"email": "[email protected]",
"password": "YourPassword123"
})
token = response.json()["token"]
# Get profile
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(f"{BASE_URL}/api/v1/user/profile", headers=headers)
profile = response.json()
print(profile)
# Update profile
response = requests.put(
f"{BASE_URL}/api/v1/user/profile",
headers=headers,
json={"name": "New Name"}
)
print(response.json())
Typowy przepływ pracy
Oto typowy przepływ pracy podczas korzystania z API:
1
Zarejestruj się lub zaloguj
Utwórz nowe konto lub uwierzytelnij się przy użyciu istniejących danych, aby otrzymać token JWT.
2
Przechowuj token
Przechowuj token JWT w bezpieczny sposób w swojej aplikacji (localStorage, bezpieczny plik cookie itp.).
3
Twórz żądania API
Dołącz token do nagłówka autoryzacji dla wszystkich chronionych punktów końcowych.
4
Obsługa wygaśnięcia tokena
Gdy pojawi się błąd 401, przekieruj użytkownika na stronę logowania w celu uzyskania nowego tokena.