This repository has been archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API and Resource implementation (#241)
* Updates fetch to use API * Removes unnecessary slash * Moves the API service to use the adapter pattern * Cleans up request * Adds options to api * Adds resource * Removes async awaits
- Loading branch information
1 parent
520a87a
commit 603e747
Showing
7 changed files
with
160 additions
and
121 deletions.
There are no files selected for viewing
14 changes: 5 additions & 9 deletions
14
src/components/inscribete/FindVoterCenter/findYourCenterMachine.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import api from "../../services/api" | ||
import { BallotsResponse, OcrResult, VoterInfo } from "./services/types" | ||
import { CDN_URL } from "./services/constants" | ||
|
||
export const VoterInformationResource = { | ||
getVoterInfo(voterId?: string) { | ||
return api.get<VoterInfo>(`/consulta?voterId=${voterId}`) | ||
}, | ||
} | ||
|
||
export const BallotResource = { | ||
getBallotsByPrecint(precint: string) { | ||
return api.get<BallotsResponse>(`/ballots/ByPrecint?precintId=${precint}`) | ||
}, | ||
|
||
getBallotsByTown(town: string) { | ||
return api.get<BallotsResponse>(`/ballots/ByTown?townId=${town}`) | ||
}, | ||
|
||
async getBallot(path: string) { | ||
return api.get<OcrResult[][]>(`/${path}data.json`, { baseUrl: CDN_URL }) | ||
}, | ||
|
||
createBallotPdf(ballot: { | ||
ballotType: string | ||
ballotPath: string | ||
votes: string | ||
}) { | ||
return api.post("/createBallotTask", ballot) | ||
}, | ||
|
||
getBallotPdf(params: string) { | ||
api.get(`/getPdfUrl?${params}`) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { API_URL } from "../packages/practica/services/constants" | ||
|
||
interface RequestOptions { | ||
baseUrl?: string | ||
} | ||
|
||
interface ApiInterface { | ||
get<T>(endpoint: string, options?: RequestOptions): Promise<T> | ||
post<T>(endpoint: string, params: any, options?: RequestOptions): Promise<T> | ||
} | ||
|
||
class FetchAdapter implements ApiInterface { | ||
private baseUrl | ||
|
||
constructor(baseUrl: string) { | ||
this.baseUrl = baseUrl | ||
} | ||
|
||
private async processResponse<T>(res: Response) { | ||
if (!res.ok) { | ||
throw Error("Network request failed.") | ||
} | ||
|
||
const data: T = await res.json() | ||
|
||
return data | ||
} | ||
|
||
private getBaseUrl(options?: RequestOptions) { | ||
if (options && options.baseUrl) { | ||
return options.baseUrl | ||
} | ||
|
||
return this.baseUrl | ||
} | ||
|
||
async get<T>(endpoint: string, options?: RequestOptions) { | ||
const baseUrl = this.getBaseUrl(options) | ||
const res = await fetch(`${baseUrl}${endpoint}`) | ||
const data = await this.processResponse<T>(res) | ||
|
||
return data | ||
} | ||
|
||
async post(endpoint: string, params: any, options?: RequestOptions) { | ||
const baseUrl = this.getBaseUrl(options) | ||
const res = await fetch(`${baseUrl}${endpoint}`, { | ||
method: "POST", | ||
body: JSON.stringify(params), | ||
}) | ||
const data = await this.processResponse<T>(res) | ||
|
||
return data | ||
} | ||
} | ||
|
||
class ApiService { | ||
private adapter | ||
|
||
constructor(adapter: ApiInterface) { | ||
this.adapter = adapter | ||
} | ||
|
||
async get<T>(endpoint: string, options?: RequestOptions) { | ||
const res = await this.adapter.get<T>(endpoint, options) | ||
|
||
return res | ||
} | ||
|
||
async post(endpoint: string, params: any, options?: RequestOptions) { | ||
const res = await this.adapter.post(endpoint, params, options) | ||
|
||
return res | ||
} | ||
} | ||
|
||
const adapter = new FetchAdapter(API_URL) | ||
|
||
export default new ApiService(adapter) |