Spaces:
Running
Running
Update flare-ui/src/app/services/api.service.ts
Browse files- flare-ui/src/app/services/api.service.ts +303 -258
flare-ui/src/app/services/api.service.ts
CHANGED
@@ -1,259 +1,304 @@
|
|
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 |
-
|
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 |
-
|
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 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
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 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
)
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
return
|
258 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
259 |
}
|
|
|
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 |
+
import { AuthService } from './auth.service';
|
7 |
+
|
8 |
+
// Interfaces
|
9 |
+
export interface Environment {
|
10 |
+
work_mode: string;
|
11 |
+
cloud_token: string;
|
12 |
+
spark_endpoint: string;
|
13 |
+
}
|
14 |
+
|
15 |
+
export interface API {
|
16 |
+
name: string;
|
17 |
+
url: string;
|
18 |
+
method: string;
|
19 |
+
headers?: any;
|
20 |
+
body_template?: any;
|
21 |
+
timeout_seconds: number;
|
22 |
+
retry?: {
|
23 |
+
retry_count: number;
|
24 |
+
backoff_seconds: number;
|
25 |
+
strategy: string;
|
26 |
+
};
|
27 |
+
auth?: {
|
28 |
+
enabled: boolean;
|
29 |
+
token_endpoint?: string;
|
30 |
+
response_token_path?: string;
|
31 |
+
token_request_body?: any;
|
32 |
+
token_refresh_endpoint?: string;
|
33 |
+
token_refresh_body?: any;
|
34 |
+
};
|
35 |
+
response_prompt?: string;
|
36 |
+
deleted?: boolean;
|
37 |
+
last_update_date?: string;
|
38 |
+
last_update_user?: string;
|
39 |
+
}
|
40 |
+
|
41 |
+
export interface Version {
|
42 |
+
id: number;
|
43 |
+
version_number: number;
|
44 |
+
caption: string;
|
45 |
+
published: boolean;
|
46 |
+
general_prompt?: string;
|
47 |
+
llm?: any;
|
48 |
+
intents?: any[];
|
49 |
+
created_date?: string;
|
50 |
+
published_by?: string;
|
51 |
+
publish_date?: string;
|
52 |
+
last_update_date?: string;
|
53 |
+
last_update_user?: string;
|
54 |
+
}
|
55 |
+
|
56 |
+
export interface Project {
|
57 |
+
id: number;
|
58 |
+
name: string;
|
59 |
+
caption: string;
|
60 |
+
enabled: boolean;
|
61 |
+
versions: Version[];
|
62 |
+
last_update_date?: string;
|
63 |
+
deleted?: boolean;
|
64 |
+
created_date?: string;
|
65 |
+
created_by?: string;
|
66 |
+
last_update_user?: string;
|
67 |
+
}
|
68 |
+
|
69 |
+
@Injectable({
|
70 |
+
providedIn: 'root'
|
71 |
+
})
|
72 |
+
export class ApiService {
|
73 |
+
private apiUrl = '/api';
|
74 |
+
|
75 |
+
constructor(
|
76 |
+
private http: HttpClient,
|
77 |
+
private router: Router,
|
78 |
+
private authService: AuthService
|
79 |
+
) {}
|
80 |
+
|
81 |
+
// ===================== Auth =====================
|
82 |
+
login(username: string, password: string): Observable<any> {
|
83 |
+
return this.http.post(`${this.apiUrl}/login`, { username, password }).pipe(
|
84 |
+
tap((response: any) => {
|
85 |
+
this.authService.setToken(response.token);
|
86 |
+
this.authService.setUsername(response.username);
|
87 |
+
})
|
88 |
+
);
|
89 |
+
}
|
90 |
+
|
91 |
+
logout(): void {
|
92 |
+
this.authService.logout();
|
93 |
+
}
|
94 |
+
|
95 |
+
private getAuthHeaders(): HttpHeaders {
|
96 |
+
const token = this.authService.getToken();
|
97 |
+
return new HttpHeaders({
|
98 |
+
'Authorization': `Bearer ${token}`,
|
99 |
+
'Content-Type': 'application/json'
|
100 |
+
});
|
101 |
+
}
|
102 |
+
|
103 |
+
// ===================== User =====================
|
104 |
+
changePassword(currentPassword: string, newPassword: string): Observable<any> {
|
105 |
+
return this.http.post(
|
106 |
+
`${this.apiUrl}/change-password`,
|
107 |
+
{ current_password: currentPassword, new_password: newPassword },
|
108 |
+
{ headers: this.getAuthHeaders() }
|
109 |
+
).pipe(
|
110 |
+
catchError(this.handleError)
|
111 |
+
);
|
112 |
+
}
|
113 |
+
|
114 |
+
// ===================== Environment =====================
|
115 |
+
getEnvironment(): Observable<Environment> {
|
116 |
+
return this.http.get<Environment>(`${this.apiUrl}/environment`, {
|
117 |
+
headers: this.getAuthHeaders()
|
118 |
+
}).pipe(
|
119 |
+
catchError(this.handleError)
|
120 |
+
);
|
121 |
+
}
|
122 |
+
|
123 |
+
updateEnvironment(data: Environment): Observable<any> {
|
124 |
+
return this.http.put(`${this.apiUrl}/environment`, data, {
|
125 |
+
headers: this.getAuthHeaders()
|
126 |
+
}).pipe(
|
127 |
+
catchError(this.handleError)
|
128 |
+
);
|
129 |
+
}
|
130 |
+
|
131 |
+
// ===================== Projects =====================
|
132 |
+
getProjects(includeDeleted = false): Observable<Project[]> {
|
133 |
+
return this.http.get<Project[]>(`${this.apiUrl}/projects`, {
|
134 |
+
headers: this.getAuthHeaders(),
|
135 |
+
params: { include_deleted: includeDeleted.toString() }
|
136 |
+
}).pipe(
|
137 |
+
catchError(this.handleError)
|
138 |
+
);
|
139 |
+
}
|
140 |
+
|
141 |
+
createProject(data: any): Observable<any> {
|
142 |
+
return this.http.post(`${this.apiUrl}/projects`, data, {
|
143 |
+
headers: this.getAuthHeaders()
|
144 |
+
}).pipe(
|
145 |
+
catchError(this.handleError)
|
146 |
+
);
|
147 |
+
}
|
148 |
+
|
149 |
+
updateProject(id: number, data: any): Observable<any> {
|
150 |
+
return this.http.put(`${this.apiUrl}/projects/${id}`, data, {
|
151 |
+
headers: this.getAuthHeaders()
|
152 |
+
}).pipe(
|
153 |
+
catchError(this.handleError)
|
154 |
+
);
|
155 |
+
}
|
156 |
+
|
157 |
+
deleteProject(id: number): Observable<any> {
|
158 |
+
return this.http.delete(`${this.apiUrl}/projects/${id}`, {
|
159 |
+
headers: this.getAuthHeaders()
|
160 |
+
}).pipe(
|
161 |
+
catchError(this.handleError)
|
162 |
+
);
|
163 |
+
}
|
164 |
+
|
165 |
+
toggleProject(id: number): Observable<any> {
|
166 |
+
return this.http.patch(`${this.apiUrl}/projects/${id}/toggle`, {}, {
|
167 |
+
headers: this.getAuthHeaders()
|
168 |
+
}).pipe(
|
169 |
+
catchError(this.handleError)
|
170 |
+
);
|
171 |
+
}
|
172 |
+
|
173 |
+
exportProject(id: number): Observable<any> {
|
174 |
+
return this.http.get(`${this.apiUrl}/projects/${id}/export`, {
|
175 |
+
headers: this.getAuthHeaders()
|
176 |
+
}).pipe(
|
177 |
+
catchError(this.handleError)
|
178 |
+
);
|
179 |
+
}
|
180 |
+
|
181 |
+
importProject(data: any): Observable<any> {
|
182 |
+
return this.http.post(`${this.apiUrl}/projects/import`, data, {
|
183 |
+
headers: this.getAuthHeaders()
|
184 |
+
}).pipe(
|
185 |
+
catchError(this.handleError)
|
186 |
+
);
|
187 |
+
}
|
188 |
+
|
189 |
+
// ===================== Versions =====================
|
190 |
+
createVersion(projectId: number, data: any): Observable<any> {
|
191 |
+
return this.http.post(`${this.apiUrl}/projects/${projectId}/versions`, data, {
|
192 |
+
headers: this.getAuthHeaders()
|
193 |
+
}).pipe(
|
194 |
+
catchError(this.handleError)
|
195 |
+
);
|
196 |
+
}
|
197 |
+
|
198 |
+
updateVersion(projectId: number, versionId: number, data: any): Observable<any> {
|
199 |
+
return this.http.put(`${this.apiUrl}/projects/${projectId}/versions/${versionId}`, data, {
|
200 |
+
headers: this.getAuthHeaders()
|
201 |
+
}).pipe(
|
202 |
+
catchError(this.handleError)
|
203 |
+
);
|
204 |
+
}
|
205 |
+
|
206 |
+
deleteVersion(projectId: number, versionId: number): Observable<any> {
|
207 |
+
return this.http.delete(`${this.apiUrl}/projects/${projectId}/versions/${versionId}`, {
|
208 |
+
headers: this.getAuthHeaders()
|
209 |
+
}).pipe(
|
210 |
+
catchError(this.handleError)
|
211 |
+
);
|
212 |
+
}
|
213 |
+
|
214 |
+
publishVersion(projectId: number, versionId: number): Observable<any> {
|
215 |
+
return this.http.post(`${this.apiUrl}/projects/${projectId}/versions/${versionId}/publish`, {}, {
|
216 |
+
headers: this.getAuthHeaders()
|
217 |
+
}).pipe(
|
218 |
+
catchError(this.handleError)
|
219 |
+
);
|
220 |
+
}
|
221 |
+
|
222 |
+
// ===================== APIs =====================
|
223 |
+
getAPIs(includeDeleted = false): Observable<API[]> {
|
224 |
+
return this.http.get<API[]>(`${this.apiUrl}/apis`, {
|
225 |
+
headers: this.getAuthHeaders(),
|
226 |
+
params: { include_deleted: includeDeleted.toString() }
|
227 |
+
}).pipe(
|
228 |
+
catchError(this.handleError)
|
229 |
+
);
|
230 |
+
}
|
231 |
+
|
232 |
+
createAPI(data: any): Observable<any> {
|
233 |
+
return this.http.post(`${this.apiUrl}/apis`, data, {
|
234 |
+
headers: this.getAuthHeaders()
|
235 |
+
}).pipe(
|
236 |
+
catchError(this.handleError)
|
237 |
+
);
|
238 |
+
}
|
239 |
+
|
240 |
+
updateAPI(name: string, data: any): Observable<any> {
|
241 |
+
return this.http.put(`${this.apiUrl}/apis/${name}`, data, {
|
242 |
+
headers: this.getAuthHeaders()
|
243 |
+
}).pipe(
|
244 |
+
catchError(this.handleError)
|
245 |
+
);
|
246 |
+
}
|
247 |
+
|
248 |
+
deleteAPI(name: string): Observable<any> {
|
249 |
+
return this.http.delete(`${this.apiUrl}/apis/${name}`, {
|
250 |
+
headers: this.getAuthHeaders()
|
251 |
+
}).pipe(
|
252 |
+
catchError(this.handleError)
|
253 |
+
);
|
254 |
+
}
|
255 |
+
|
256 |
+
testAPI(data: any): Observable<any> {
|
257 |
+
return this.http.post(`${this.apiUrl}/apis/test`, data, {
|
258 |
+
headers: this.getAuthHeaders()
|
259 |
+
}).pipe(
|
260 |
+
catchError(this.handleError)
|
261 |
+
);
|
262 |
+
}
|
263 |
+
|
264 |
+
// ===================== Tests =====================
|
265 |
+
runTests(testType: string): Observable<any> {
|
266 |
+
return this.http.post(`${this.apiUrl}/test/run-all`, { test_type: testType }, {
|
267 |
+
headers: this.getAuthHeaders()
|
268 |
+
}).pipe(
|
269 |
+
catchError(this.handleError)
|
270 |
+
);
|
271 |
+
}
|
272 |
+
|
273 |
+
// ===================== Activity Log =====================
|
274 |
+
getActivityLog(limit = 50): Observable<any[]> {
|
275 |
+
return this.http.get<any[]>(`${this.apiUrl}/activity-log`, {
|
276 |
+
headers: this.getAuthHeaders(),
|
277 |
+
params: { limit: limit.toString() }
|
278 |
+
}).pipe(
|
279 |
+
catchError(this.handleError)
|
280 |
+
);
|
281 |
+
}
|
282 |
+
|
283 |
+
// ===================== Validation =====================
|
284 |
+
validateRegex(pattern: string, testValue: string): Observable<any> {
|
285 |
+
return this.http.post(`${this.apiUrl}/validate/regex`,
|
286 |
+
{ pattern, test_value: testValue },
|
287 |
+
{ headers: this.getAuthHeaders() }
|
288 |
+
).pipe(
|
289 |
+
catchError(this.handleError)
|
290 |
+
);
|
291 |
+
}
|
292 |
+
|
293 |
+
// ===================== Error Handler =====================
|
294 |
+
private handleError(error: any) {
|
295 |
+
console.error('API Error:', error);
|
296 |
+
|
297 |
+
if (error.status === 401) {
|
298 |
+
// Token expired or invalid
|
299 |
+
this.authService.logout();
|
300 |
+
}
|
301 |
+
|
302 |
+
return throwError(() => error.error || error);
|
303 |
+
}
|
304 |
}
|