Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 2.0.0 beta sdk #125

Merged
merged 6 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/js-npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ jobs:
registry-url: https://registry.npmjs.org/
- run: |
yarn
yarn publish --access public
yarn publish --access public --tag beta
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN }}
2 changes: 1 addition & 1 deletion apitable.java/.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.0
2.0.0-beta.4
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,9 @@ public DatasheetApi getDatasheetApi() {
}
return datasheetApi;
}


public boolean isV3(){
return this.apiVersion == ApiVersion.V3;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.apitable.client.api.model.Record;
import com.apitable.client.api.model.Records;
import com.apitable.client.api.model.UpdateRecordRequest;
import com.apitable.core.http.DefaultHttpClient;
import com.apitable.core.http.GenericTypeReference;
import com.apitable.core.http.HttpHeader;
import com.apitable.core.utils.CollectionUtil;
Expand All @@ -62,21 +63,37 @@ public Stream<Record> getRecordsAsStream(String datasheetId) throws ApiException
}

public List<Record> getRecords(String datasheetId, int page, int itemsPerPage) throws ApiException {
return getRecords(datasheetId, page, itemsPerPage, false);
}

public List<Record> getRecords(String datasheetId, int page, int itemsPerPage, boolean isV3) throws ApiException {
if (page < 0 || itemsPerPage < 0) {
throw new ApiException("page or itemsPerPage don't set right");
}
ApiQueryParam queryParam = new ApiQueryParam(page, itemsPerPage);
Map<String, String> uriVariables = queryParam.toMap();
GenericTypeReference<HttpResult<PagerInfo<Record>>> reference = new GenericTypeReference<HttpResult<PagerInfo<Record>>>() {};
String uri = String.format(PATH, datasheetId) + MapUtil.extractKeyToVariables(uriVariables);
HttpResult<PagerInfo<Record>> result = getDefaultHttpClient().get(uri, new HttpHeader(), reference, uriVariables);
DefaultHttpClient client = getDefaultHttpClient();
if (isV3) {
client = getHttpClientWithVersion(ApiHttpClient.ApiVersion.V3);
}
HttpResult<PagerInfo<Record>> result = client.get(uri, new HttpHeader(), reference, uriVariables);
return result.getData().getRecords();
}

public Pager<Record> getRecords(String datasheetId) throws ApiException {
return new Pager<>(this, String.format(PATH, datasheetId), getDefaultPerPage(), Record.class);
}


public Pager<Record> getRecords(String datasheetId, boolean isV3) throws ApiException {
if (isV3) {
return new Pager<>(this, String.format(PATH, datasheetId), getDefaultPerPage(), Record.class, true);
}
return getRecords(datasheetId);
}

public Pager<Record> getRecords(String datasheetId, int itemsPerPage) throws ApiException {
return new Pager<>(this, String.format(PATH, datasheetId), itemsPerPage, Record.class);
}
Expand All @@ -85,6 +102,13 @@ public Pager<Record> getRecords(String datasheetId, ApiQueryParam queryParam) th
return new Pager<>(this, String.format(PATH, datasheetId), queryParam, Record.class);
}

public Pager<Record> getRecords(String datasheetId, ApiQueryParam queryParam, boolean isV3) throws ApiException {
if (isV3) {
return new Pager<>(this, String.format(PATH, datasheetId), queryParam, Record.class, true);
}
return new Pager<>(this, String.format(PATH, datasheetId), queryParam, Record.class);
}

public List<Record> addRecords(String datasheetId, CreateRecordRequest record) throws ApiException {
if (!StringUtil.hasText(datasheetId)) {
throw new ApiException("datasheet id must be not null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class ApiHttpClient {
public enum ApiVersion {
V1,
V2,

V3
;

public String getApiNamespace() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import com.apitable.client.api.Constants;
import com.apitable.client.api.exception.ApiException;
import com.apitable.client.api.http.AbstractApi;
import com.apitable.client.api.http.ApiHttpClient;
import com.apitable.core.http.DefaultHttpClient;
import com.apitable.core.http.GenericTypeReference;
import com.apitable.core.http.HttpHeader;
import com.apitable.core.utils.JacksonConverter;
Expand Down Expand Up @@ -64,8 +66,7 @@ public class Pager<T> implements Iterator<List<T>> {

private JavaType javaType;

public Pager(AbstractApi api, String url, int itemsPerPage, Class<T> type) throws ApiException {
javaType = JacksonConverter.getCollectionJavaType(type);
public Pager(AbstractApi api, String url, int itemsPerPage, Class<T> type, boolean isV3) throws ApiException {javaType = JacksonConverter.getCollectionJavaType(type);

this.api = api;
this.url = url;
Expand All @@ -78,7 +79,11 @@ public Pager(AbstractApi api, String url, int itemsPerPage, Class<T> type) throw
Map<String, String> uriVariables = this.queryParam.toMap();
GenericTypeReference<HttpResult<PagerInfo<T>>> reference = new GenericTypeReference<HttpResult<PagerInfo<T>>>() {};
String uri = url + MapUtil.extractKeyToVariables(uriVariables);
HttpResult<PagerInfo<T>> result = api.getDefaultHttpClient().get(uri, new HttpHeader(), reference, uriVariables);
DefaultHttpClient client = api.getDefaultHttpClient();
if (isV3) {
client = api.getHttpClientWithVersion(ApiHttpClient.ApiVersion.V3);
}
HttpResult<PagerInfo<T>> result = client.get(uri, new HttpHeader(), reference, uriVariables);
if (result.getData().getRecords() != null) {
this.currentItems = JacksonConverter.toGenericBean(result.getData().getRecords(), javaType);
if (this.currentItems == null) {
Expand All @@ -93,15 +98,23 @@ public Pager(AbstractApi api, String url, int itemsPerPage, Class<T> type) throw
this.totalPages = this.totalItems == 0 ? 1 : ((this.totalItems - 1) / this.itemsPerPage + 1);
}

public Pager(AbstractApi api, String url, ApiQueryParam queryParam, Class<T> type) throws ApiException {
public Pager(AbstractApi api, String url, int itemsPerPage, Class<T> type) throws ApiException {
this(api, url, itemsPerPage, type, false);
}

public Pager(AbstractApi api, String url, ApiQueryParam queryParam, Class<T> type, boolean isV3) throws ApiException {
this.api = api;
this.url = url;
this.queryParam = queryParam;
javaType = JacksonConverter.getCollectionJavaType(type);
GenericTypeReference<HttpResult<PagerInfo<T>>> reference = new GenericTypeReference<HttpResult<PagerInfo<T>>>() {};
Map<String, String> uriVariables = this.queryParam.toMap();
String uri = url + MapUtil.extractKeyToVariables(uriVariables);
HttpResult<PagerInfo<T>> result = api.getDefaultHttpClient().get(uri, new HttpHeader(), reference, uriVariables);
DefaultHttpClient client = api.getDefaultHttpClient();
if (isV3) {
client = api.getHttpClientWithVersion(ApiHttpClient.ApiVersion.V3);
}
HttpResult<PagerInfo<T>> result = client.get(uri, new HttpHeader(), reference, uriVariables);
if (result.getData().getRecords() != null) {
this.currentItems = JacksonConverter.toGenericBean(result.getData().getRecords(), javaType);
if (this.currentItems == null) {
Expand All @@ -116,6 +129,10 @@ public Pager(AbstractApi api, String url, ApiQueryParam queryParam, Class<T> typ
this.totalPages = this.totalItems == 0 ? 1 : ((this.totalItems - 1) / this.itemsPerPage + 1);
}

public Pager(AbstractApi api, String url, ApiQueryParam queryParam, Class<T> type) throws ApiException {
this(api, url, queryParam, type, false);
}

public int getCurrentPage() {
return currentPage;
}
Expand Down
2 changes: 1 addition & 1 deletion apitable.java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<url>https://github.com/apitable/apitable-sdks/tree/develop/apitable.java</url>

<properties>
<revision>1.3.0</revision>
<revision>2.0.0-beta.4</revision>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
Expand Down
2 changes: 1 addition & 1 deletion apitable.js/.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.0
2.0.0-beta.4
14 changes: 12 additions & 2 deletions apitable.js/lib/apitable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios, { AxiosInstance } from "axios";
// import mpAdapter from 'axios-miniprogram-adapter';
import qs from "qs";
import { DEFAULT_HOST, DEFAULT_REQUEST_TIMEOUT, DEFAULT_VERSION_PREFIX, FUSION_PATH_PREFIX } from "./const";
import { DEFAULT_HOST, DEFAULT_REQUEST_TIMEOUT, DATA_BUS_VERSION_PREFIX, DEFAULT_VERSION_PREFIX, FUSION_PATH_PREFIX } from "./const";
import { Datasheet } from "./datasheet";
import { IHttpResponse, IAPITableClientConfig } from "./interface";
import { NodeManager } from "./node";
Expand Down Expand Up @@ -75,12 +75,18 @@ export class APITable {
timeout?: number;
}): Promise<IHttpResponse<T>> {
const { path, params, method, data, headers, timeout } = config;
let urlPath = '';
if (params?.isV3){
urlPath = path.includes(FUSION_PATH_PREFIX) ? path : DATA_BUS_VERSION_PREFIX.concat(path);
}else {
urlPath = path.includes(FUSION_PATH_PREFIX) ? path : DEFAULT_VERSION_PREFIX.concat(path);
}
let result: IHttpResponse<T>;
try {
result = (
await this.axios.request<IHttpResponse<T>>({
timeout,
url: path.includes(FUSION_PATH_PREFIX) ? path : DEFAULT_VERSION_PREFIX.concat(path),
url: urlPath,
method,
params: {
fieldKey: this.config.fieldKey,
Expand Down Expand Up @@ -145,4 +151,8 @@ export class APITable {
space(spaceId: string) {
return new SpaceManager(this, spaceId);
}

public isV3() {
return true;
}
}
1 change: 1 addition & 0 deletions apitable.js/lib/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const DST_MAX_RECORDS = 50000; // Maximum number of records in a single d
export const DEFAULT_HOST = 'https://api.apitable.com';
export const FUSION_PATH_PREFIX = '/fusion';
export const DEFAULT_VERSION_PREFIX = '/fusion/v1';
export const DATA_BUS_VERSION_PREFIX = '/fusion/v3';
export const DEFAULT_REQUEST_TIMEOUT = 60000;
export const MAX_WRITE_SIZE_PER_REQ = 10;

Expand Down
5 changes: 5 additions & 0 deletions apitable.js/lib/interface/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ export interface IGetRecordsReqParams {
* return method (using id will avoid code failure due to column name changes).
*/
fieldKey?: 'name' | 'id';

/**
* use databus fusion api
* */
isV3?: boolean;
}


Expand Down
2 changes: 1 addition & 1 deletion apitable.js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apitable",
"version": "1.3.0",
"version": "2.0.0-beta.4",
"description": "APITable JavaScript SDK",
"main": "./es/index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions apitable.py/.version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.0.0-beta.4
13 changes: 12 additions & 1 deletion apitable.py/apitable/datasheet/datasheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ def _record_api_endpoint(self):
return urljoin(self.apitable.api_base,
f"/fusion/v1/datasheets/{self.id}/records")

@property
def _record_api_endpoint_v3(self):
return urljoin(self.apitable.api_base,
f"/fusion/v3/datasheets/{self.id}/records")

@property
@timed_lru_cache(seconds=300)
def fields(self):
Expand Down Expand Up @@ -137,6 +142,12 @@ def get_records(self, **kwargs):
"""
Paginate to get data
"""

is_v3 = bool(kwargs.get("isV3", False))
endpoint = self._record_api_endpoint
if is_v3:
endpoint = self._record_api_endpoint_v3

params = {}
for key in kwargs:
if key in API_GET_DATASHEET_QS_SET:
Expand All @@ -147,7 +158,7 @@ def get_records(self, **kwargs):
else:
raise ErrorSortParams('The format of the sort parameter is incorrect')
params.update({key: params_value})
resp = self.apitable.request.get(self._record_api_endpoint, params=params)
resp = self.apitable.request.get(endpoint, params=params)
return handle_response(resp, GETRecordResponse)

def get_records_all(self, **kwargs) -> List[RawRecord]:
Expand Down
2 changes: 1 addition & 1 deletion apitable.py/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "apitable"
version = "1.4.1"
version = "2.0.0-beta.4"
description = "APITable OpenAPI Python SDK"
authors = ["APITable Ltd. <dev@apitable.com>"]
readme = "README.md"
Expand Down
28 changes: 28 additions & 0 deletions apitable.py/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import setuptools

with open("README.md", "r") as fh:
long_description = fh.read()

with open(".version", "r") as fh:
version_number = fh.read()

setuptools.setup(
name="apitable",
version=version_number,
author="apitable",
author_email="dev@apitable.com",
description="Apitable Python SDK",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/apitable/apitable-sdks",
packages=[
"apitable", "apitable.datasheet", "apitable.types", "apitable.node", "apitable.space", "apitable.unit"
],
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires=">=3.6",
install_requires=["requests<=2.31.0", "pydantic==1.7", "environs<=9.5.0"],
)
Loading