2019-08-21 15:07:49 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
import { BaseGql } from '@/api/baseGql';
|
2020-11-13 11:41:35 +00:00
|
|
|
import { ErrorUnauthorized } from '@/api/errors/ErrorUnauthorized';
|
2019-10-17 11:57:20 +01:00
|
|
|
import { Bucket, BucketCursor, BucketPage, BucketsApi } from '@/types/buckets';
|
2020-11-13 11:41:35 +00:00
|
|
|
import { HttpClient } from '@/utils/httpClient';
|
2019-08-21 15:07:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* BucketsApiGql is a graphql implementation of Buckets API.
|
2020-02-14 15:35:10 +00:00
|
|
|
* Exposes all bucket-related functionality.
|
2019-08-21 15:07:49 +01:00
|
|
|
*/
|
|
|
|
export class BucketsApiGql extends BaseGql implements BucketsApi {
|
2020-11-13 11:41:35 +00:00
|
|
|
private readonly client: HttpClient = new HttpClient();
|
|
|
|
private readonly ROOT_PATH: string = '/api/v0/buckets';
|
|
|
|
|
2019-08-21 15:07:49 +01:00
|
|
|
/**
|
2020-02-14 15:35:10 +00:00
|
|
|
* Fetch buckets.
|
2019-08-21 15:07:49 +01:00
|
|
|
*
|
|
|
|
* @returns BucketPage
|
|
|
|
* @throws Error
|
|
|
|
*/
|
|
|
|
public async get(projectId: string, before: Date, cursor: BucketCursor): Promise<BucketPage> {
|
|
|
|
const query =
|
|
|
|
`query($projectId: String!, $before: DateTime!, $limit: Int!, $search: String!, $page: Int!) {
|
|
|
|
project(id: $projectId) {
|
|
|
|
bucketUsages(before: $before, cursor: {
|
|
|
|
limit: $limit, search: $search, page: $page
|
|
|
|
}) {
|
|
|
|
bucketUsages {
|
|
|
|
bucketName,
|
|
|
|
storage,
|
|
|
|
egress,
|
|
|
|
objectCount,
|
|
|
|
since,
|
|
|
|
before
|
|
|
|
},
|
|
|
|
search,
|
|
|
|
limit,
|
|
|
|
offset,
|
|
|
|
pageCount,
|
|
|
|
currentPage,
|
2019-09-13 10:48:27 +01:00
|
|
|
totalCount
|
2019-08-21 15:07:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}`;
|
|
|
|
|
|
|
|
const variables = {
|
|
|
|
projectId,
|
|
|
|
before: before.toISOString(),
|
|
|
|
limit: cursor.limit,
|
|
|
|
search: cursor.search,
|
|
|
|
page: cursor.page,
|
|
|
|
};
|
|
|
|
|
|
|
|
const response = await this.query(query, variables);
|
|
|
|
|
2019-10-17 11:57:20 +01:00
|
|
|
return this.getBucketPage(response.data.project.bucketUsages);
|
2019-08-21 15:07:49 +01:00
|
|
|
}
|
|
|
|
|
2020-11-13 11:41:35 +00:00
|
|
|
/**
|
|
|
|
* Fetch all bucket names.
|
|
|
|
*
|
|
|
|
* @returns string[]
|
|
|
|
* @throws Error
|
|
|
|
*/
|
|
|
|
public async getAllBucketNames(projectId: string): Promise<string[]> {
|
|
|
|
const path = `${this.ROOT_PATH}/bucket-names?projectID=${projectId}`;
|
|
|
|
const response = await this.client.get(path);
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
if (response.status === 401) {
|
|
|
|
throw new ErrorUnauthorized();
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:43:02 +00:00
|
|
|
throw new Error('Can not get bucket names');
|
2020-11-13 11:41:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 20:51:36 +00:00
|
|
|
const result = await response.json();
|
|
|
|
|
|
|
|
return result ? result : [];
|
2020-11-13 11:41:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 15:35:10 +00:00
|
|
|
/**
|
|
|
|
* Method for mapping buckets page from json to BucketPage type.
|
|
|
|
*
|
|
|
|
* @param page anonymous object from json
|
|
|
|
*/
|
2021-08-24 14:10:11 +01:00
|
|
|
private getBucketPage(page: any): BucketPage { // eslint-disable-line @typescript-eslint/no-explicit-any
|
2019-10-17 11:57:20 +01:00
|
|
|
if (!page) {
|
|
|
|
return new BucketPage();
|
|
|
|
}
|
|
|
|
|
2020-03-18 17:07:29 +00:00
|
|
|
const buckets: Bucket[] = page.bucketUsages.map(key =>
|
|
|
|
new Bucket(
|
|
|
|
key.bucketName,
|
|
|
|
key.storage,
|
|
|
|
key.egress,
|
|
|
|
key.objectCount,
|
|
|
|
new Date(key.since),
|
|
|
|
new Date(key.before)));
|
2019-10-17 11:57:20 +01:00
|
|
|
|
|
|
|
return new BucketPage(buckets, page.search, page.limit, page.offset, page.pageCount, page.currentPage, page.totalCount);
|
2019-08-21 15:07:49 +01:00
|
|
|
}
|
|
|
|
}
|