ciyidogan commited on
Commit
d37c52a
·
verified ·
1 Parent(s): 0627e2d

Update flare-ui/src/app/services/api.service.ts

Browse files
Files changed (1) hide show
  1. 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, inject } from '@angular/core';
2
- import { HttpClient } from '@angular/common/http';
3
- import { Observable } from 'rxjs';
4
-
5
- // Models
6
- export interface Environment {
7
- work_mode: string;
8
- cloud_token: string;
9
- spark_endpoint: string;
10
- }
11
-
12
- export interface Project {
13
- id: number;
14
- name: string;
15
- caption: string;
16
- enabled: boolean;
17
- last_version_number: number;
18
- version_id_counter: number;
19
- versions: Version[];
20
- deleted: boolean;
21
- created_date: string;
22
- created_by: string;
23
- last_update_date: string;
24
- last_update_user: string;
25
- }
26
-
27
- export interface Version {
28
- id: number;
29
- caption: string;
30
- published: boolean;
31
- general_prompt: string;
32
- llm: LLMConfig;
33
- intents: Intent[];
34
- created_date: string;
35
- created_by: string;
36
- last_update_date: string;
37
- last_update_user: string;
38
- publish_date?: string;
39
- published_by?: string;
40
- deleted?: boolean;
41
- }
42
-
43
- export interface LLMConfig {
44
- repo_id: string;
45
- generation_config: {
46
- max_new_tokens: number;
47
- temperature: number;
48
- top_p: number;
49
- repetition_penalty: number;
50
- };
51
- use_fine_tune: boolean;
52
- fine_tune_zip: string;
53
- }
54
-
55
- export interface Intent {
56
- name: string;
57
- caption: string;
58
- locale: string;
59
- detection_prompt: string;
60
- examples: string[];
61
- parameters: Parameter[];
62
- action: string;
63
- fallback_timeout_prompt?: string;
64
- fallback_error_prompt?: string;
65
- }
66
-
67
- export interface Parameter {
68
- name: string;
69
- caption: string;
70
- type: string;
71
- required: boolean;
72
- variable_name: string;
73
- extraction_prompt: string;
74
- validation_regex?: string;
75
- invalid_prompt?: string;
76
- type_error_prompt?: string;
77
- }
78
-
79
- export interface API {
80
- name: string;
81
- url: string;
82
- method: string;
83
- headers: Record<string, string>;
84
- body_template: any;
85
- timeout_seconds: number;
86
- retry: {
87
- retry_count: number;
88
- backoff_seconds: number;
89
- strategy: string;
90
- };
91
- proxy?: string;
92
- auth?: APIAuth;
93
- response_prompt?: string;
94
- deleted: boolean;
95
- created_date: string;
96
- created_by: string;
97
- last_update_date: string;
98
- last_update_user: string;
99
- }
100
-
101
- export interface APIAuth {
102
- enabled: boolean;
103
- token_endpoint?: string;
104
- response_token_path?: string;
105
- token_request_body?: any;
106
- token_refresh_endpoint?: string;
107
- token_refresh_body?: any;
108
- }
109
-
110
- @Injectable({
111
- providedIn: 'root'
112
- })
113
- export class ApiService {
114
- private http = inject(HttpClient);
115
- private baseUrl = '/api';
116
-
117
- // Environment
118
- getEnvironment(): Observable<Environment> {
119
- return this.http.get<Environment>(`${this.baseUrl}/environment`);
120
- }
121
-
122
- updateEnvironment(env: Environment): Observable<any> {
123
- return this.http.put(`${this.baseUrl}/environment`, env);
124
- }
125
-
126
- // Projects
127
- getProjects(includeDeleted = false): Observable<Project[]> {
128
- return this.http.get<Project[]>(`${this.baseUrl}/projects?include_deleted=${includeDeleted}`);
129
- }
130
-
131
- createProject(project: { name: string; caption: string }): Observable<Project> {
132
- return this.http.post<Project>(`${this.baseUrl}/projects`, project);
133
- }
134
-
135
- updateProject(id: number, update: { caption: string; last_update_date: string }): Observable<Project> {
136
- return this.http.put<Project>(`${this.baseUrl}/projects/${id}`, update);
137
- }
138
-
139
- deleteProject(id: number): Observable<any> {
140
- return this.http.delete(`${this.baseUrl}/projects/${id}`);
141
- }
142
-
143
- toggleProject(id: number): Observable<{ enabled: boolean }> {
144
- return this.http.patch<{ enabled: boolean }>(`${this.baseUrl}/projects/${id}/toggle`, {});
145
- }
146
-
147
- exportProject(id: number): Observable<any> {
148
- return this.http.get(`${this.baseUrl}/projects/${id}/export`);
149
- }
150
-
151
- importProject(data: any): Observable<any> {
152
- return this.http.post(`${this.baseUrl}/projects/import`, data);
153
- }
154
-
155
- // Versions
156
- createVersion(projectId: number, sourceVersionId: number, caption: string): Observable<Version> {
157
- return this.http.post<Version>(`${this.baseUrl}/projects/${projectId}/versions`, {
158
- source_version_id: sourceVersionId,
159
- caption
160
- });
161
- }
162
-
163
- updateVersion(projectId: number, versionId: number, update: any): Observable<Version> {
164
- return this.http.put<Version>(`${this.baseUrl}/projects/${projectId}/versions/${versionId}`, update);
165
- }
166
-
167
- publishVersion(projectId: number, versionId: number): Observable<any> {
168
- return this.http.post(`${this.baseUrl}/projects/${projectId}/versions/${versionId}/publish`, {});
169
- }
170
-
171
- deleteVersion(projectId: number, versionId: number): Observable<any> {
172
- return this.http.delete(`${this.baseUrl}/projects/${projectId}/versions/${versionId}`);
173
- }
174
-
175
- // APIs
176
- getAPIs(includeDeleted = false): Observable<API[]> {
177
- return this.http.get<API[]>(`${this.baseUrl}/apis?include_deleted=${includeDeleted}`);
178
- }
179
-
180
- createAPI(api: Partial<API>): Observable<API> {
181
- return this.http.post<API>(`${this.baseUrl}/apis`, api);
182
- }
183
-
184
- updateAPI(name: string, update: any): Observable<API> {
185
- return this.http.put<API>(`${this.baseUrl}/apis/${name}`, update);
186
- }
187
-
188
- deleteAPI(name: string): Observable<any> {
189
- return this.http.delete(`${this.baseUrl}/apis/${name}`);
190
- }
191
-
192
- testAPI(api: Partial<API>): Observable<any> {
193
- return this.http.post(`${this.baseUrl}/apis/test`, api);
194
- }
195
-
196
- // Testing
197
- runTests(testType: string): Observable<any> {
198
- return this.http.post(`${this.baseUrl}/test/run-all`, { test_type: testType });
199
- }
200
-
201
- // Validation
202
- validateRegex(pattern: string, testValue: string): Observable<{ valid: boolean; matches?: boolean; error?: string }> {
203
- return this.http.post<any>(`${this.baseUrl}/validate/regex`, { pattern, test_value: testValue });
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
  }