Spaces:
Building
Building
Update flare-ui/src/app/services/api.service.ts
Browse files- flare-ui/src/app/services/api.service.ts +258 -204
flare-ui/src/app/services/api.service.ts
CHANGED
@@ -1,205 +1,259 @@
|
|
1 |
-
import { Injectable
|
2 |
-
import { HttpClient } from '@angular/common/http';
|
3 |
-
import { Observable } from 'rxjs';
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
}
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
}
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
}
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
}
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
return this.http.
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
}
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
return this.http.
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
}
|
150 |
-
|
151 |
-
|
152 |
-
return this.http.
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
}
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
}
|
166 |
-
|
167 |
-
publishVersion(projectId: number, versionId: number): Observable<any> {
|
168 |
-
return this.http.post(`${this.
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
}
|
174 |
-
|
175 |
-
// APIs
|
176 |
-
getAPIs(includeDeleted = false): Observable<
|
177 |
-
return this.http.get<
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
}
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
}
|
|
|
1 |
+
import { Injectable } from '@angular/core';
|
2 |
+
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
3 |
+
import { Observable, throwError } from 'rxjs';
|
4 |
+
import { catchError, tap } from 'rxjs/operators';
|
5 |
+
import { Router } from '@angular/router';
|
6 |
+
|
7 |
+
@Injectable({
|
8 |
+
providedIn: 'root'
|
9 |
+
})
|
10 |
+
export class ApiService {
|
11 |
+
private apiUrl = '/api';
|
12 |
+
private tokenKey = 'flare_admin_token';
|
13 |
+
private usernameKey = 'flare_admin_username';
|
14 |
+
|
15 |
+
constructor(
|
16 |
+
private http: HttpClient,
|
17 |
+
private router: Router
|
18 |
+
) {}
|
19 |
+
|
20 |
+
// ===================== Auth =====================
|
21 |
+
login(username: string, password: string): Observable<any> {
|
22 |
+
return this.http.post(`${this.apiUrl}/login`, { username, password }).pipe(
|
23 |
+
tap((response: any) => {
|
24 |
+
localStorage.setItem(this.tokenKey, response.token);
|
25 |
+
localStorage.setItem(this.usernameKey, response.username);
|
26 |
+
})
|
27 |
+
);
|
28 |
+
}
|
29 |
+
|
30 |
+
logout(): void {
|
31 |
+
localStorage.removeItem(this.tokenKey);
|
32 |
+
localStorage.removeItem(this.usernameKey);
|
33 |
+
this.router.navigate(['/login']);
|
34 |
+
}
|
35 |
+
|
36 |
+
getToken(): string | null {
|
37 |
+
return localStorage.getItem(this.tokenKey);
|
38 |
+
}
|
39 |
+
|
40 |
+
getUsername(): string | null {
|
41 |
+
return localStorage.getItem(this.usernameKey);
|
42 |
+
}
|
43 |
+
|
44 |
+
isAuthenticated(): boolean {
|
45 |
+
return !!this.getToken();
|
46 |
+
}
|
47 |
+
|
48 |
+
private getAuthHeaders(): HttpHeaders {
|
49 |
+
const token = this.getToken();
|
50 |
+
return new HttpHeaders({
|
51 |
+
'Authorization': `Bearer ${token}`,
|
52 |
+
'Content-Type': 'application/json'
|
53 |
+
});
|
54 |
+
}
|
55 |
+
|
56 |
+
// ===================== User =====================
|
57 |
+
changePassword(currentPassword: string, newPassword: string): Observable<any> {
|
58 |
+
return this.http.post(
|
59 |
+
`${this.apiUrl}/change-password`,
|
60 |
+
{ current_password: currentPassword, new_password: newPassword },
|
61 |
+
{ headers: this.getAuthHeaders() }
|
62 |
+
).pipe(
|
63 |
+
catchError(this.handleError)
|
64 |
+
);
|
65 |
+
}
|
66 |
+
|
67 |
+
// ===================== Environment =====================
|
68 |
+
getEnvironment(): Observable<any> {
|
69 |
+
return this.http.get(`${this.apiUrl}/environment`, {
|
70 |
+
headers: this.getAuthHeaders()
|
71 |
+
}).pipe(
|
72 |
+
catchError(this.handleError)
|
73 |
+
);
|
74 |
+
}
|
75 |
+
|
76 |
+
updateEnvironment(data: any): Observable<any> {
|
77 |
+
return this.http.put(`${this.apiUrl}/environment`, data, {
|
78 |
+
headers: this.getAuthHeaders()
|
79 |
+
}).pipe(
|
80 |
+
catchError(this.handleError)
|
81 |
+
);
|
82 |
+
}
|
83 |
+
|
84 |
+
// ===================== Projects =====================
|
85 |
+
getProjects(includeDeleted = false): Observable<any[]> {
|
86 |
+
return this.http.get<any[]>(`${this.apiUrl}/projects`, {
|
87 |
+
headers: this.getAuthHeaders(),
|
88 |
+
params: { include_deleted: includeDeleted.toString() }
|
89 |
+
}).pipe(
|
90 |
+
catchError(this.handleError)
|
91 |
+
);
|
92 |
+
}
|
93 |
+
|
94 |
+
createProject(data: any): Observable<any> {
|
95 |
+
return this.http.post(`${this.apiUrl}/projects`, data, {
|
96 |
+
headers: this.getAuthHeaders()
|
97 |
+
}).pipe(
|
98 |
+
catchError(this.handleError)
|
99 |
+
);
|
100 |
+
}
|
101 |
+
|
102 |
+
updateProject(id: number, data: any): Observable<any> {
|
103 |
+
return this.http.put(`${this.apiUrl}/projects/${id}`, data, {
|
104 |
+
headers: this.getAuthHeaders()
|
105 |
+
}).pipe(
|
106 |
+
catchError(this.handleError)
|
107 |
+
);
|
108 |
+
}
|
109 |
+
|
110 |
+
deleteProject(id: number): Observable<any> {
|
111 |
+
return this.http.delete(`${this.apiUrl}/projects/${id}`, {
|
112 |
+
headers: this.getAuthHeaders()
|
113 |
+
}).pipe(
|
114 |
+
catchError(this.handleError)
|
115 |
+
);
|
116 |
+
}
|
117 |
+
|
118 |
+
toggleProject(id: number): Observable<any> {
|
119 |
+
return this.http.patch(`${this.apiUrl}/projects/${id}/toggle`, {}, {
|
120 |
+
headers: this.getAuthHeaders()
|
121 |
+
}).pipe(
|
122 |
+
catchError(this.handleError)
|
123 |
+
);
|
124 |
+
}
|
125 |
+
|
126 |
+
exportProject(id: number): Observable<any> {
|
127 |
+
return this.http.get(`${this.apiUrl}/projects/${id}/export`, {
|
128 |
+
headers: this.getAuthHeaders()
|
129 |
+
}).pipe(
|
130 |
+
catchError(this.handleError)
|
131 |
+
);
|
132 |
+
}
|
133 |
+
|
134 |
+
importProject(data: any): Observable<any> {
|
135 |
+
return this.http.post(`${this.apiUrl}/projects/import`, data, {
|
136 |
+
headers: this.getAuthHeaders()
|
137 |
+
}).pipe(
|
138 |
+
catchError(this.handleError)
|
139 |
+
);
|
140 |
+
}
|
141 |
+
|
142 |
+
// ===================== Versions =====================
|
143 |
+
createVersion(projectId: number, data: any): Observable<any> {
|
144 |
+
return this.http.post(`${this.apiUrl}/projects/${projectId}/versions`, data, {
|
145 |
+
headers: this.getAuthHeaders()
|
146 |
+
}).pipe(
|
147 |
+
catchError(this.handleError)
|
148 |
+
);
|
149 |
+
}
|
150 |
+
|
151 |
+
updateVersion(projectId: number, versionId: number, data: any): Observable<any> {
|
152 |
+
return this.http.put(`${this.apiUrl}/projects/${projectId}/versions/${versionId}`, data, {
|
153 |
+
headers: this.getAuthHeaders()
|
154 |
+
}).pipe(
|
155 |
+
catchError(this.handleError)
|
156 |
+
);
|
157 |
+
}
|
158 |
+
|
159 |
+
deleteVersion(projectId: number, versionId: number): Observable<any> {
|
160 |
+
return this.http.delete(`${this.apiUrl}/projects/${projectId}/versions/${versionId}`, {
|
161 |
+
headers: this.getAuthHeaders()
|
162 |
+
}).pipe(
|
163 |
+
catchError(this.handleError)
|
164 |
+
);
|
165 |
+
}
|
166 |
+
|
167 |
+
publishVersion(projectId: number, versionId: number): Observable<any> {
|
168 |
+
return this.http.post(`${this.apiUrl}/projects/${projectId}/versions/${versionId}/publish`, {}, {
|
169 |
+
headers: this.getAuthHeaders()
|
170 |
+
}).pipe(
|
171 |
+
catchError(this.handleError)
|
172 |
+
);
|
173 |
+
}
|
174 |
+
|
175 |
+
// ===================== APIs =====================
|
176 |
+
getAPIs(includeDeleted = false): Observable<any[]> {
|
177 |
+
return this.http.get<any[]>(`${this.apiUrl}/apis`, {
|
178 |
+
headers: this.getAuthHeaders(),
|
179 |
+
params: { include_deleted: includeDeleted.toString() }
|
180 |
+
}).pipe(
|
181 |
+
catchError(this.handleError)
|
182 |
+
);
|
183 |
+
}
|
184 |
+
|
185 |
+
createAPI(data: any): Observable<any> {
|
186 |
+
return this.http.post(`${this.apiUrl}/apis`, data, {
|
187 |
+
headers: this.getAuthHeaders()
|
188 |
+
}).pipe(
|
189 |
+
catchError(this.handleError)
|
190 |
+
);
|
191 |
+
}
|
192 |
+
|
193 |
+
updateAPI(name: string, data: any): Observable<any> {
|
194 |
+
return this.http.put(`${this.apiUrl}/apis/${name}`, data, {
|
195 |
+
headers: this.getAuthHeaders()
|
196 |
+
}).pipe(
|
197 |
+
catchError(this.handleError)
|
198 |
+
);
|
199 |
+
}
|
200 |
+
|
201 |
+
deleteAPI(name: string): Observable<any> {
|
202 |
+
return this.http.delete(`${this.apiUrl}/apis/${name}`, {
|
203 |
+
headers: this.getAuthHeaders()
|
204 |
+
}).pipe(
|
205 |
+
catchError(this.handleError)
|
206 |
+
);
|
207 |
+
}
|
208 |
+
|
209 |
+
testAPI(data: any): Observable<any> {
|
210 |
+
return this.http.post(`${this.apiUrl}/apis/test`, data, {
|
211 |
+
headers: this.getAuthHeaders()
|
212 |
+
}).pipe(
|
213 |
+
catchError(this.handleError)
|
214 |
+
);
|
215 |
+
}
|
216 |
+
|
217 |
+
// ===================== Tests =====================
|
218 |
+
runTests(testType: string): Observable<any> {
|
219 |
+
return this.http.post(`${this.apiUrl}/test/run-all`, { test_type: testType }, {
|
220 |
+
headers: this.getAuthHeaders()
|
221 |
+
}).pipe(
|
222 |
+
catchError(this.handleError)
|
223 |
+
);
|
224 |
+
}
|
225 |
+
|
226 |
+
// ===================== Activity Log =====================
|
227 |
+
getActivityLog(limit = 50): Observable<any[]> {
|
228 |
+
return this.http.get<any[]>(`${this.apiUrl}/activity-log`, {
|
229 |
+
headers: this.getAuthHeaders(),
|
230 |
+
params: { limit: limit.toString() }
|
231 |
+
}).pipe(
|
232 |
+
catchError(this.handleError)
|
233 |
+
);
|
234 |
+
}
|
235 |
+
|
236 |
+
// ===================== Validation =====================
|
237 |
+
validateRegex(pattern: string, testValue: string): Observable<any> {
|
238 |
+
return this.http.post(`${this.apiUrl}/validate/regex`,
|
239 |
+
{ pattern, test_value: testValue },
|
240 |
+
{ headers: this.getAuthHeaders() }
|
241 |
+
).pipe(
|
242 |
+
catchError(this.handleError)
|
243 |
+
);
|
244 |
+
}
|
245 |
+
|
246 |
+
// ===================== Error Handler =====================
|
247 |
+
private handleError(error: any) {
|
248 |
+
console.error('API Error:', error);
|
249 |
+
|
250 |
+
if (error.status === 401) {
|
251 |
+
// Token expired or invalid
|
252 |
+
localStorage.removeItem(this.tokenKey);
|
253 |
+
localStorage.removeItem(this.usernameKey);
|
254 |
+
this.router.navigate(['/login']);
|
255 |
+
}
|
256 |
+
|
257 |
+
return throwError(() => error.error || error);
|
258 |
+
}
|
259 |
}
|