Skip to content

Commit

Permalink
Refactor methods in BackendService/ConfigService
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamaguchi committed Apr 24, 2024
1 parent 5a9619d commit 70843ec
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 117 deletions.
147 changes: 37 additions & 110 deletions frontend/src/app/backend.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,166 +11,93 @@ export class BackendService {
constructor(private http: HttpClient, private configService: ConfigService) {}

getBlocks(page: number, perPage: number): Observable<any> {
return this.getConfig().pipe(
mergeMap((config: Config) => {
return this.http.get(`${config.backendUrl}/api/blocks`, {
params: new HttpParams({
fromObject: { page: page.toString(), perPage: perPage.toString() }
})
});
})
);
return this.request(`/api/blocks`);
}

searchBlock(query: string): Observable<any> {
return this.getConfig().pipe(
mergeMap((config: Config) => {
return this.http.get(`${config.backendUrl}/api/block/${query}`);
})
);
return this.request(`/api/block/${query}`);
}

getBlock(blockHash: string): Observable<any> {
return this.getConfig().pipe(
mergeMap((config: Config) => {
return this.http.get(`${config.backendUrl}/api/block/${blockHash}`);
})
);
return this.request(`/api/block/${blockHash}`);
}

getBlockByHeight(height: number): Observable<any> {
return this.getConfig().pipe(
mergeMap((config: Config) => {
return this.http.get(`${config.backendUrl}/api/block/height/${height}`);
})
);
return this.request(`/api/block/height/${height}`);
}

getRawBlock(blockHash: string): Observable<any> {
return this.getConfig().pipe(
mergeMap((config: Config) => {
return this.http.get(`${config.backendUrl}/api/block/${blockHash}/raw`);
})
);
return this.request(`/api/block/${blockHash}/raw`);
}

getBlockTransactions(
blockHash: string,
page: number,
perPage: number
): Observable<any> {
return this.getConfig().pipe(
mergeMap((config: Config) => {
return this.http.get(
`${config.backendUrl}/api/block/${blockHash}/txns`,
{
params: new HttpParams({
fromObject: { page: page.toString(), perPage: perPage.toString() }
})
}
);
})
);
return this.request(`/api/block/${blockHash}/txns`, new HttpParams({
fromObject: { page: page.toString(), perPage: perPage.toString() }
}));
}

getTransactions(page: number, perPage: number): Observable<any> {
return this.getConfig().pipe(
mergeMap((config: Config) => {
return this.http.get(`${config.backendUrl}/api/transactions`, {
params: new HttpParams({
fromObject: { page: page.toString(), perPage: perPage.toString() }
})
});
})
);
return this.request("/api/transactions", new HttpParams({
fromObject: { page: page.toString(), perPage: perPage.toString() }
}));
}

getTransaction(txId: string): Observable<any> {
return this.getConfig().pipe(
mergeMap((config: Config) => {
return this.http.get(`${config.backendUrl}/api/tx/${txId}`);
})
);
return this.request(`/api/tx/${txId}`);
}

getRawTransaction(txId: string): Observable<any> {
return this.getConfig().pipe(
mergeMap((config: Config) => {
return this.http.get(`${config.backendUrl}/api/tx/${txId}/rawData`);
})
);
return this.request(`/api/tx/${txId}/rawData`);
}

getAddressInfo(address: string, lastSeenTxid?: string): Observable<any> {
return this.getConfig().pipe(
mergeMap((config: Config) => {
return this.http.get(`${config.backendUrl}/api/address/${address}`, {
params: new HttpParams({
fromObject: {
lastSeenTxid: (lastSeenTxid || '').toString()
}
})
});
})
);
return this.request(`/api/address/${address}`, new HttpParams({
fromObject: {
lastSeenTxid: (lastSeenTxid || '').toString()
}
}));
}

searchTransaction(query: string): Observable<any> {
return this.getConfig().pipe(
mergeMap((config: Config) => {
return this.http.get(`${config.backendUrl}/api/tx/${query}/get`);
})
);
searchTransaction(query: string): Observable<any> {]
return this.request(`/api/tx/${query}/get`);
}

getColors(lastSeenColorId?: string): Observable<any> {
return this.getConfig().pipe(
mergeMap((config: Config) => {
return this.http.get(`${config.backendUrl}/api/colors`, {
params: new HttpParams({
fromObject: {
lastSeenColorId: (lastSeenColorId || '').toString()
}
})
});
})
);
return this.request("/api/colors", new HttpParams({
fromObject: {
lastSeenColorId: (lastSeenColorId || '').toString()
}
}));
}

getColor(colorId: string, lastSeenTxid?: string): Observable<any> {
return this.getConfig().pipe(
mergeMap((config: Config) => {
return this.http.get(`${config.backendUrl}/api/color/${colorId}`, {
params: new HttpParams({
fromObject: {
lastSeenTxid: (lastSeenTxid || '').toString()
}
})
});
})
);
return this.request(`/api/color/${colorId}`, new HttpParams({
fromObject: {
lastSeenTxid: (lastSeenTxid || '').toString()
}
}));
}

validateOpenedValue(opened_value: string): Observable<any> {
return this.getConfig().pipe(
mergeMap((config: Config) => {
return this.http.get(
`${config.backendUrl}/api/validate/${opened_value}`
);
})
);
return this.request(`/api/validate/${opened_value}`);
}

checkMaterialTrackingTransaction(txId: string): Observable<any> {
return this.request(`/api/check_material_tracking_balance/${txId}`);
}

private request(url: string, params?: HttpParams): Observable<any> {
return this.getConfig().pipe(
mergeMap((config: Config) => {
return this.http.get(
`${config.backendUrl}/api/check_material_tracking_balance/${txId}`
);
return this.http.get(`${config.backendUrl}/${url}`, { params: params });
})
);
}

private getConfig(): Observable<Config> {
return this.configService.getConfig();
}
Expand Down
19 changes: 12 additions & 7 deletions frontend/src/app/config.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { HttpClient, HttpParams } from '@angular/common/http';
import { HttpErrorResponse } from '@angular/common/http';

import { Observable, throwError } from 'rxjs';
import { catchError, retry } from 'rxjs/operators';
import { catchError, mergeMap, retry } from 'rxjs/operators';

/***
* Config service to supply configuration items from assets/config.json file.
Expand All @@ -21,6 +21,7 @@ export interface Config {
export class ConfigService {
configUrl = 'assets/config.json';
config: Config;
observable: Observable<Config>;

constructor(private http: HttpClient) {}

Expand All @@ -34,19 +35,23 @@ export class ConfigService {
}

getConfig(): Observable<Config> {
if (this.config) {
if (this.config && this.observable) {
return new Observable(observer => {
observer.next(this.config);
observer.complete();
});
} else {
return this.http.get<Config>(this.configUrl).pipe(
retry(3), // retry a failed request up to 3 times
catchError(this.handleError) // then handle the error
);
return this.observable = this.loadConfig();
}
}

private loadConfig(): Observable<Config> {
return this.http.get<Config>(this.configUrl).pipe(
retry(3), // retry a failed request up to 3 times
catchError(this.handleError) // then handle the error
);
}

private handleError(error: HttpErrorResponse) {
if (error.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
Expand Down

0 comments on commit 70843ec

Please sign in to comment.