flare / flare-ui /src /app /services /api.service.ts
ciyidogan's picture
Update flare-ui/src/app/services/api.service.ts
d37c52a verified
raw
history blame
7.52 kB
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { Router } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class ApiService {
private apiUrl = '/api';
private tokenKey = 'flare_admin_token';
private usernameKey = 'flare_admin_username';
constructor(
private http: HttpClient,
private router: Router
) {}
// ===================== Auth =====================
login(username: string, password: string): Observable<any> {
return this.http.post(`${this.apiUrl}/login`, { username, password }).pipe(
tap((response: any) => {
localStorage.setItem(this.tokenKey, response.token);
localStorage.setItem(this.usernameKey, response.username);
})
);
}
logout(): void {
localStorage.removeItem(this.tokenKey);
localStorage.removeItem(this.usernameKey);
this.router.navigate(['/login']);
}
getToken(): string | null {
return localStorage.getItem(this.tokenKey);
}
getUsername(): string | null {
return localStorage.getItem(this.usernameKey);
}
isAuthenticated(): boolean {
return !!this.getToken();
}
private getAuthHeaders(): HttpHeaders {
const token = this.getToken();
return new HttpHeaders({
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
});
}
// ===================== User =====================
changePassword(currentPassword: string, newPassword: string): Observable<any> {
return this.http.post(
`${this.apiUrl}/change-password`,
{ current_password: currentPassword, new_password: newPassword },
{ headers: this.getAuthHeaders() }
).pipe(
catchError(this.handleError)
);
}
// ===================== Environment =====================
getEnvironment(): Observable<any> {
return this.http.get(`${this.apiUrl}/environment`, {
headers: this.getAuthHeaders()
}).pipe(
catchError(this.handleError)
);
}
updateEnvironment(data: any): Observable<any> {
return this.http.put(`${this.apiUrl}/environment`, data, {
headers: this.getAuthHeaders()
}).pipe(
catchError(this.handleError)
);
}
// ===================== Projects =====================
getProjects(includeDeleted = false): Observable<any[]> {
return this.http.get<any[]>(`${this.apiUrl}/projects`, {
headers: this.getAuthHeaders(),
params: { include_deleted: includeDeleted.toString() }
}).pipe(
catchError(this.handleError)
);
}
createProject(data: any): Observable<any> {
return this.http.post(`${this.apiUrl}/projects`, data, {
headers: this.getAuthHeaders()
}).pipe(
catchError(this.handleError)
);
}
updateProject(id: number, data: any): Observable<any> {
return this.http.put(`${this.apiUrl}/projects/${id}`, data, {
headers: this.getAuthHeaders()
}).pipe(
catchError(this.handleError)
);
}
deleteProject(id: number): Observable<any> {
return this.http.delete(`${this.apiUrl}/projects/${id}`, {
headers: this.getAuthHeaders()
}).pipe(
catchError(this.handleError)
);
}
toggleProject(id: number): Observable<any> {
return this.http.patch(`${this.apiUrl}/projects/${id}/toggle`, {}, {
headers: this.getAuthHeaders()
}).pipe(
catchError(this.handleError)
);
}
exportProject(id: number): Observable<any> {
return this.http.get(`${this.apiUrl}/projects/${id}/export`, {
headers: this.getAuthHeaders()
}).pipe(
catchError(this.handleError)
);
}
importProject(data: any): Observable<any> {
return this.http.post(`${this.apiUrl}/projects/import`, data, {
headers: this.getAuthHeaders()
}).pipe(
catchError(this.handleError)
);
}
// ===================== Versions =====================
createVersion(projectId: number, data: any): Observable<any> {
return this.http.post(`${this.apiUrl}/projects/${projectId}/versions`, data, {
headers: this.getAuthHeaders()
}).pipe(
catchError(this.handleError)
);
}
updateVersion(projectId: number, versionId: number, data: any): Observable<any> {
return this.http.put(`${this.apiUrl}/projects/${projectId}/versions/${versionId}`, data, {
headers: this.getAuthHeaders()
}).pipe(
catchError(this.handleError)
);
}
deleteVersion(projectId: number, versionId: number): Observable<any> {
return this.http.delete(`${this.apiUrl}/projects/${projectId}/versions/${versionId}`, {
headers: this.getAuthHeaders()
}).pipe(
catchError(this.handleError)
);
}
publishVersion(projectId: number, versionId: number): Observable<any> {
return this.http.post(`${this.apiUrl}/projects/${projectId}/versions/${versionId}/publish`, {}, {
headers: this.getAuthHeaders()
}).pipe(
catchError(this.handleError)
);
}
// ===================== APIs =====================
getAPIs(includeDeleted = false): Observable<any[]> {
return this.http.get<any[]>(`${this.apiUrl}/apis`, {
headers: this.getAuthHeaders(),
params: { include_deleted: includeDeleted.toString() }
}).pipe(
catchError(this.handleError)
);
}
createAPI(data: any): Observable<any> {
return this.http.post(`${this.apiUrl}/apis`, data, {
headers: this.getAuthHeaders()
}).pipe(
catchError(this.handleError)
);
}
updateAPI(name: string, data: any): Observable<any> {
return this.http.put(`${this.apiUrl}/apis/${name}`, data, {
headers: this.getAuthHeaders()
}).pipe(
catchError(this.handleError)
);
}
deleteAPI(name: string): Observable<any> {
return this.http.delete(`${this.apiUrl}/apis/${name}`, {
headers: this.getAuthHeaders()
}).pipe(
catchError(this.handleError)
);
}
testAPI(data: any): Observable<any> {
return this.http.post(`${this.apiUrl}/apis/test`, data, {
headers: this.getAuthHeaders()
}).pipe(
catchError(this.handleError)
);
}
// ===================== Tests =====================
runTests(testType: string): Observable<any> {
return this.http.post(`${this.apiUrl}/test/run-all`, { test_type: testType }, {
headers: this.getAuthHeaders()
}).pipe(
catchError(this.handleError)
);
}
// ===================== Activity Log =====================
getActivityLog(limit = 50): Observable<any[]> {
return this.http.get<any[]>(`${this.apiUrl}/activity-log`, {
headers: this.getAuthHeaders(),
params: { limit: limit.toString() }
}).pipe(
catchError(this.handleError)
);
}
// ===================== Validation =====================
validateRegex(pattern: string, testValue: string): Observable<any> {
return this.http.post(`${this.apiUrl}/validate/regex`,
{ pattern, test_value: testValue },
{ headers: this.getAuthHeaders() }
).pipe(
catchError(this.handleError)
);
}
// ===================== Error Handler =====================
private handleError(error: any) {
console.error('API Error:', error);
if (error.status === 401) {
// Token expired or invalid
localStorage.removeItem(this.tokenKey);
localStorage.removeItem(this.usernameKey);
this.router.navigate(['/login']);
}
return throwError(() => error.error || error);
}
}