Skip to content

Commit

Permalink
feat: ListObjectPolicies api
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 committed Sep 8, 2023
1 parent 780e2ef commit c7f9ef3
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-dolls-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': patch
---

feat: Add sp `ListObjectPolicies` API
18 changes: 18 additions & 0 deletions examples/nextjs/src/components/object/info/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ export const ObjectInfo = () => {

<br />

<div>
listObjectPolicies by bucket name and object name
<br />
<button
onClick={async () => {
const res = await client.object.listObjectPolicies({
bucketName,
objectName,
actionType: 'ACTION_GET_OBJECT',
});

console.log('res', res);
}}
>
listObjectPolicies
</button>
</div>

<div>
get objects list by bucket name
<br />
Expand Down
30 changes: 30 additions & 0 deletions packages/chain-sdk/src/api/objectt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {
getObjectMetaInfo,
parseGetObjectMetaResponse,
} from '@/clients/spclient/spApis/getObjectMeta';
import {
getListObjectPoliciesMetaInfo,
parseGetListObjectPoliciesResponse,
} from '@/clients/spclient/spApis/listObjectPolicies';
import { parseListObjectsByBucketNameResponse } from '@/clients/spclient/spApis/listObjectsByBucket';
import {
getListObjectsByIDsMetaInfo,
Expand Down Expand Up @@ -65,6 +69,7 @@ import { AuthType, SpClient } from '../clients/spclient/spClient';
import {
CreateObjectApprovalRequest,
CreateObjectApprovalResponse,
GetListObjectPoliciesRequest,
GetPrivewObject,
ListObjectsByBucketNameRequest,
ListObjectsByIDsRequest,
Expand Down Expand Up @@ -161,6 +166,8 @@ export interface IObject {
getObjectMeta(params: GetObjectMetaRequest): Promise<SpResponse<GetObjectMetaResponse>>;

listObjectsByIds(params: ListObjectsByIDsRequest): Promise<SpResponse<ListObjectsByIDsResponse>>;

listObjectPolicies(params: GetListObjectPoliciesRequest): Promise<any>;
// TODO: GetObjectUploadProgress
// TODO: getObjectStatusFromSP
}
Expand Down Expand Up @@ -744,4 +751,27 @@ export class Objectt implements IObject {
};
}
}

public async listObjectPolicies(params: GetListObjectPoliciesRequest) {
let endpoint = params.endpoint;
if (!endpoint) {
endpoint = await this.sp.getSPUrlByBucket(params.bucketName);
}
const { url } = getListObjectPoliciesMetaInfo(endpoint, params);

const result = await this.spClient.callApi(url, {
headers: {},
method: METHOD_GET,
});

const xml = await result.text();
const res = parseGetListObjectPoliciesResponse(xml);

return {
code: 0,
message: 'success',
statusCode: result.status,
body: res,
};
}
}
12 changes: 8 additions & 4 deletions packages/chain-sdk/src/api/storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TxClient } from '@/clients/txClient';
import { getMsgPutPolicySDKTypeEIP712 } from '@/messages/greenfield';
import { MsgDeletePolicySDKTypeEIP712 } from '@/messages/greenfield/storage/MsgDeletePolicy';
import { MsgPutPolicySDKTypeEIP712 } from '@/messages/greenfield/storage/MsgPutPolicy';
import {
QueryGroupMembersExistRequest,
QueryGroupMembersExistResponse,
Expand All @@ -22,9 +23,7 @@ import {
} from '@bnb-chain/greenfield-cosmos-types/greenfield/storage/tx';
import { container, delay, inject, singleton } from 'tsyringe';
import { fromTimestamp, MsgDeletePolicyTypeUrl, MsgPutPolicyTypeUrl, TxResponse } from '..';
import { Basic } from './basic';
import { RpcQueryClient } from '../clients/queryclient';
import { TxClient } from '@/clients/txClient';

export interface IStorage {
params(): Promise<QueryParamsResponse>;
Expand Down Expand Up @@ -69,13 +68,18 @@ export class Storage implements IStorage {
return await this.txClient.tx(
MsgPutPolicyTypeUrl,
msg.operator,
MsgPutPolicySDKTypeEIP712,
getMsgPutPolicySDKTypeEIP712(msg.statements[0].resources),
{
...toSdk,
expiration_time: msg.expirationTime ? fromTimestamp(msg.expirationTime) : '',
statements: toSdk.statements.map((e) => {
// @ts-ignore
e.expiration_time = '';

if (e.resources.length == 0) {
// @ts-ignore
e.resources = null;
}
return e;
}),
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { convertStrToBool, formatObjectInfo } from '@/types/sp/Common';
import {
GetListObjectPoliciesRequest,
GetListObjectPoliciesResponse,
} from '@/types/sp/ListObjectPolicies';
import { generateUrlByBucketName } from '@/utils';
import { actionTypeFromJSON } from '@bnb-chain/greenfield-cosmos-types/greenfield/permission/common';
import { XMLParser } from 'fast-xml-parser';
import { encodePath, getSortQueryParams } from '../auth';

export const getListObjectPoliciesMetaInfo = (
endpoint: string,
params: GetListObjectPoliciesRequest,
) => {
const { actionType, bucketName, objectName, limit = '10', startAfter = '' } = params;
const path = `/${encodePath(objectName)}`;
const queryMap = {
'object-policies': 'null',
'start-after': startAfter,
limit: String(limit),
'action-type': String(actionTypeFromJSON(actionType)),
};

let url = new URL(path, generateUrlByBucketName(endpoint, bucketName));
url = getSortQueryParams(url, queryMap);

return {
url: url.href,
};
};

export const parseGetListObjectPoliciesResponse = (data: string) => {
const xmlParser = new XMLParser({
parseTagValue: false,
});
const res = xmlParser.parse(data) as GetListObjectPoliciesResponse;

let Policies = res.GfSpListObjectPoliciesResponse.Policies || [];

if (Policies) {
if (!Array.isArray(Policies)) {
Policies = [Policies];
}

Policies = Policies.map((item) => {
return {
...item,
PrincipalType: Number(item.ResourceType),
ResourceType: Number(item.ResourceType),
CreateTimestamp: Number(item.CreateTimestamp),
UpdateTimestamp: Number(item.UpdateTimestamp),
ExpirationTime: Number(item.ExpirationTime),
};
});
}

res.GfSpListObjectPoliciesResponse = {
Policies,
};

return res;
};
31 changes: 31 additions & 0 deletions packages/chain-sdk/src/types/sp/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,34 @@ export function formatGroupInfo(o: GroupInfo) {
Id: Number(o.Id),
};
}

export interface PolicyMeta {
/**
* principal_type defines the type of principal
*/
PrincipalType: number;
/**
* principal_value defines the value of principal
*/
PrincipalValue: string;
/**
* resource_type defines the type of resource that grants permission for
*/
ResourceType: number;
/**
* resource_id defines the bucket/object/group id of the resource that grants permission for
*/
ResourceId: string;
/**
* create_timestamp defines the create time of permission
*/
CreateTimestamp: number;
/**
* update_timestamp defines the update time of permission
*/
UpdateTimestamp: number;
/**
* expiration_time defines the expiration time of permission
*/
ExpirationTime: number;
}
19 changes: 19 additions & 0 deletions packages/chain-sdk/src/types/sp/ListObjectPolicies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ActionType } from '@bnb-chain/greenfield-cosmos-types/greenfield/permission/common';
import { PolicyMeta } from './Common';

export type GetListObjectPoliciesRequest = {
endpoint?: string;
bucketName: string;
objectName: string;
actionType: keyof typeof ActionType;
limit?: number;
startAfter?: string;
};

export type GetListObjectPoliciesResponse = {
GfSpListObjectPoliciesResponse: GfSpListObjectPoliciesResponse;
};

export interface GfSpListObjectPoliciesResponse {
Policies: PolicyMeta[];
}
1 change: 1 addition & 0 deletions packages/chain-sdk/src/types/sp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from './ListBucketsByIDs';
export * from './ListBucketsByPaymentAccount';
export * from './ListGroups';
export * from './ListGroupsMembers';
export * from './ListObjectPolicies';
export * from './ListObjectsByBucketName';
export * from './ListObjectsByIDs';
export * from './ListUserGroups';
Expand Down

0 comments on commit c7f9ef3

Please sign in to comment.