feat: Create api abstraction layer for dataset network calls (#11855)

* add datasets api file

* Update dataset.ts

* Update dataset.ts

* Update dataset.ts
This commit is contained in:
Hugh A. Miles II 2020-11-30 13:27:27 -08:00 committed by GitHub
parent a0b81357cd
commit 3cd94d6da9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,61 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { SupersetClient, JsonResponse } from '@superset-ui/core';
import rison from 'rison';
export const getByUser = async (userId: number) => {
const queryParams = rison.encode({
filters: [
{
col: 'owners',
opr: 'rel_m_m',
value: userId,
},
],
order_column: 'changed_on_delta_humanized',
order_direction: 'desc',
});
const endpoint = `/api/v1/dataset?q=${queryParams}`;
const data: JsonResponse = await SupersetClient.get({
endpoint,
});
return data.json.result;
};
export const put = async (
datasetId: number,
sql: string,
columns: Array<Record<string, any>>,
overrideColumns: boolean,
) => {
const endpoint = `api/v1/dataset/${datasetId}?override_columns=${overrideColumns}`;
const headers = { 'Content-Type': 'application/json' };
const body = JSON.stringify({
sql,
columns,
});
const data: JsonResponse = await SupersetClient.put({
endpoint,
headers,
body,
});
return data.json.result;
};