opencand.ui/src/api/openCandApi.ts

112 lines
2.6 KiB
TypeScript

import { BaseApiClient } from './base';
import { API_CONFIG } from '../config/api';
// Type definitions based on the API specs
export interface CandidateSearchResult {
candidatos: Candidate[];
}
export interface Candidate {
idCandidato: string;
nome: string;
cpf: string;
dataNascimento: string;
email: string;
estadoCivil: string;
sexo: string;
ocupacao: string;
}
export interface CandidateDetails extends Candidate {
eleicoes: Election[];
}
export interface Election {
sqid: string;
tipoeleicao: string;
siglaUf: string;
nomeue: string;
nrCandidato: string;
nomeCandidato: string;
resultado: string;
}
export interface CandidateAssets {
bens: Asset[];
}
export interface Asset {
idCandidato: string;
ano: number;
tipoBem: string;
descricao: string;
valor: number;
}
export interface PlatformStats {
totalCandidatos: number;
totalBemCandidatos: number;
totalEleicoes: number;
}
/**
* OpenCand API client for interacting with the OpenCand platform
*/
export class OpenCandApi extends BaseApiClient {
constructor(baseUrl: string = '') {
super(baseUrl);
}
/**
* Get platform statistics
* GET /v1/stats
*/
async getStats(): Promise<PlatformStats> {
return this.get<PlatformStats>('/v1/stats');
}
/**
* Search for candidates by name or other attributes
* GET /v1/candidato/search?q={query}
*/
async searchCandidates(query: string): Promise<CandidateSearchResult> {
const encodedQuery = encodeURIComponent(query);
return this.get<CandidateSearchResult>(`/v1/candidato/search?q=${encodedQuery}`);
}
/**
* Get detailed information about a specific candidate by ID
* GET /v1/candidato/{id}
*/
async getCandidateById(id: string): Promise<CandidateDetails> {
return this.get<CandidateDetails>(`/v1/candidato/${id}`);
}
/**
* Get the assets of a specific candidate by ID
* GET /v1/candidato/{id}/bens
*/
async getCandidateAssets(id: string): Promise<CandidateAssets> {
return this.get<CandidateAssets>(`/v1/candidato/${id}/bens`);
}
/**
* Search candidates and get their details (limited to first 10 results to avoid too many requests)
*/
async searchCandidatesWithDetails(
query: string,
limit: number = 10
): Promise<Candidate[]> {
const searchResult = await this.searchCandidates(query);
const candidatesLimit = searchResult.candidatos.slice(0, limit);
return candidatesLimit;
}
}
// Create a default instance for easy usage with proper configuration
export const openCandApi = new OpenCandApi(API_CONFIG.baseUrl);
// Export the API error for error handling
export { ApiError } from './base';