-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathapi.tsx
More file actions
277 lines (251 loc) · 7.87 KB
/
Copy pathapi.tsx
File metadata and controls
277 lines (251 loc) · 7.87 KB
1
2
3
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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import { InteractionRequiredAuthError, PublicClientApplication } from '@azure/msal-browser'
import Axios from 'axios'
import {
DataSource,
Feature,
FeatureLineage,
Role,
UserRole,
NewFeature,
NewDatasource
} from '@/models/model'
import { getMsalConfig } from '@/utils/utils'
const msalInstance = getMsalConfig()
const getApiBaseUrl = () => {
let endpoint = process.env.REACT_APP_API_ENDPOINT
if (!endpoint || endpoint === '') {
endpoint = window.location.protocol + '//' + window.location.host
}
return endpoint + '/api/v1'
}
export const fetchDataSources = async (project: string) => {
const axios = await authAxios(msalInstance)
return axios
.get<DataSource[]>(`${getApiBaseUrl()}/projects/${project}/datasources`, {
headers: {}
})
.then((response) => {
return response.data
})
}
export const fetchDataSource = async (project: string, dataSourceId: string) => {
const axios = await authAxios(msalInstance)
return axios
.get<DataSource & { message: string; detail: string }>(
`${getApiBaseUrl()}/projects/${project}/datasources/${dataSourceId}`,
{
params: { project: project, datasource: dataSourceId }
}
)
.then((response) => {
if (response.data.message || response.data.detail) {
return Promise.reject(response.data.message || response.data.detail)
} else {
return response.data
}
})
}
export const fetchProjects = async () => {
const axios = await authAxios(msalInstance)
return axios
.get<[]>(`${getApiBaseUrl()}/projects`, {
headers: {}
})
.then((response) => {
return response.data
})
}
export const fetchFeatures = async (
project: string,
page: number,
limit: number,
keyword: string
) => {
const axios = await authAxios(msalInstance)
return axios
.get<Feature[]>(`${getApiBaseUrl()}/projects/${project}/features`, {
params: { keyword: keyword, page: page, limit: limit },
headers: {}
})
.then((response) => {
return response.data
})
}
export const fetchFeature = async (project: string, featureId: string) => {
const axios = await authAxios(msalInstance)
return axios
.get<Feature>(`${getApiBaseUrl()}/features/${featureId}`, {
params: { project: project }
})
.then((response) => {
return response.data
})
}
export const fetchProjectLineages = async (project: string) => {
const axios = await authAxios(msalInstance)
return axios
.get<FeatureLineage>(`${getApiBaseUrl()}/projects/${project}`, {})
.then((response) => {
return response.data
})
}
export const fetchFeatureLineages = async (featureId: string) => {
const axios = await authAxios(msalInstance)
return axios
.get<FeatureLineage>(`${getApiBaseUrl()}/features/${featureId}/lineage`, {})
.then((response) => {
return response.data
})
}
// Following are place-holder code
export const createFeature = async (feature: Feature) => {
const axios = await authAxios(msalInstance)
return axios.post(`${getApiBaseUrl()}/features`, feature, {
headers: { 'Content-Type': 'application/json;' },
params: {}
})
}
export const updateFeature = async (feature: Feature, id?: string) => {
const axios = await authAxios(msalInstance)
if (id) {
feature.guid = id
}
return axios.put(`${getApiBaseUrl()}/features/${feature.guid}`, feature, {
headers: { 'Content-Type': 'application/json;' },
params: {}
})
}
export const listUserRole = async () => {
await getIdToken(msalInstance)
const axios = await authAxios(msalInstance)
return await axios.get<UserRole[]>(`${getApiBaseUrl()}/userroles`, {}).then((response) => {
return response.data
})
}
export const getUserRole = async (userName: string) => {
const axios = await authAxios(msalInstance)
return await axios
.get<UserRole>(`${getApiBaseUrl()}/user/${userName}/userroles`, {})
.then((response) => {
return response.data
})
}
export const addUserRole = async (role: Role) => {
const axios = await authAxios(msalInstance)
return await axios
.post(`${getApiBaseUrl()}/users/${role.userName}/userroles/add`, role, {
headers: { 'Content-Type': 'application/json;' },
params: {
project: role.scope,
role: role.roleName,
reason: role.reason
}
})
.then((response) => {
return response
})
}
export const deleteUserRole = async (userrole: UserRole) => {
const axios = await authAxios(msalInstance)
const reason = 'Delete from management UI.'
return await axios
.delete(`${getApiBaseUrl()}/users/${userrole.userName}/userroles/delete`, {
headers: { 'Content-Type': 'application/json;' },
params: {
project: userrole.scope,
role: userrole.roleName,
reason: reason
}
})
.then((response) => {
return response
})
}
export const getIdToken = async (msalInstance: PublicClientApplication): Promise<string> => {
const activeAccount = msalInstance.getActiveAccount() // This will only return a non-null value if you have logic somewhere else that calls the setActiveAccount API
const accounts = msalInstance.getAllAccounts()
const request = {
scopes: ['User.Read'],
account: activeAccount || accounts[0]
}
let idToken = ''
// Silently acquire an token for a given set of scopes. Will use cached token if available, otherwise will attempt to acquire a new token from the network via refresh token.
// A known issue may cause token expire: https://github.com/AzureAD/microsoft-authentication-library-for-js/issues/4206
await msalInstance
.acquireTokenSilent(request)
.then((response) => {
idToken = response.idToken
})
.catch((error) => {
// acquireTokenSilent can fail for a number of reasons, fallback to interaction
if (error instanceof InteractionRequiredAuthError) {
msalInstance.acquireTokenPopup(request).then((response) => {
idToken = response.idToken
})
}
})
return idToken
}
export const authAxios = async (msalInstance: PublicClientApplication) => {
const token = await getIdToken(msalInstance)
const axios = Axios.create({
headers: {
Authorization: 'Bearer ' + token,
'Content-Type': 'application/json'
},
baseURL: getApiBaseUrl()
})
axios.interceptors.response.use(
(response) => {
return response
},
(error) => {
if (error.response?.status === 403) {
const detail = error.response.data.detail
window.location.href = '/responseErrors/403/' + detail
} else {
return Promise.reject(error.response.data)
}
//TODO: handle other response errors
}
)
return axios
}
export const deleteEntity = async (enity: string) => {
const axios = await authAxios(msalInstance)
return axios.delete(`${getApiBaseUrl()}/entity/${enity}`)
}
export const getDependent = async (entity: string) => {
const axios = await authAxios(msalInstance)
return await axios.get(`${getApiBaseUrl()}/dependent/${entity}`).then((response) => {
return response
})
}
export const createAnchorFeature = async (
project: string,
anchor: string,
anchorFeature: NewFeature
) => {
const axios = await authAxios(msalInstance)
return axios
.post(`${getApiBaseUrl()}/projects/${project}/anchors/${anchor}/features`, anchorFeature)
.then((response) => {
return response
})
}
export const createDerivedFeature = async (project: string, derivedFeature: NewFeature) => {
const axios = await authAxios(msalInstance)
return axios
.post(`${getApiBaseUrl()}/projects/${project}/derivedfeatures`, derivedFeature)
.then((response) => {
return response
})
}
export const createSource = async (project: string, datasource: NewDatasource) => {
const axios = await authAxios(msalInstance)
return axios
.post(`${getApiBaseUrl()}/projects/${project}/datasources`, datasource)
.then((response) => {
return response
})
}