-
Notifications
You must be signed in to change notification settings - Fork 1
API Integration
Simon Gibson edited this page Sep 9, 2024
·
1 revision
NeRF-or-Nothing integrates with a backend API to handle various operations such as user authentication, scene creation, and data retrieval. This guide outlines the approach to API integration in the application.
API calls are centralized in the Fetch
directory, with the main functions defined in CommonApiCalls.ts
.
Key features:
- Uses the Fetch API for making HTTP requests
- Implements error handling and response parsing
- Provides typed responses using TypeScript interfaces
-
fetchLogin
: Handles user login -
fetchRegister
: Handles user registration -
fetchChangeUsername
: Handles username change -
fetchChangePassword
: Handles password change -
FetchDeleteAccount
: Handles account deletion -
FetchDeleteScene
: Handles a single scene deletion -
fetchPostVideo
: Uploads a video for scene creation -
fetchSceneMetadata
: Retrieves metadata for a scene -
fetchSceneName
: Retries scene name -
fetchScenethumbnail
: Fetches a single image representing the trained scene -
fetchSceneOutput
: Fetches the output data for a scene -
fetchUserSceneHistory
: Retrieves the user's scene history
API calls that require authentication use the JWT token stored in the AuthContext
. The token is included in the Authorization
header of the request.
Example:
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${token}`,
},
});
API functions include basic error handling:
- HTTP errors are caught and logged
- Error messages are returned to the calling component for display to the user
The application implements a retry mechanism for API calls using the useAuthFetchRetry
hook. This hook handles:
- Automatic retries for failed requests
- Authentication token refresh (if implemented in the backend)
- Consistent error handling across API calls
Usage:
const authRetryFetchSceneMetadata = useAuthFetchRetry(fetchSceneMetadata);
// Later in your component
const metadata = await authRetryFetchSceneMetadata(sceneId);
- Keep API calls centralized in the
Fetch
directory - Use TypeScript interfaces to define API response types
- Implement consistent error handling across all API calls
- Use the
useAuthFetchRetry
hook for authenticated API calls - Handle loading states in components making API calls
- Consider implementing request cancellation for long-running or frequently changing requests
- Add request and response interceptors for global error handling and token refresh
- Implement API request caching for improved performance
- Consider using OpenAPI/Swagger for API documentation and type generation