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

Support getting metadata and contents for a Media reference property #1040

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export type {
FetchPageResult,
SingleOsdkResult,
} from "./object/FetchPageResult.js";
export type { Media } from "./object/Media.js";
export { isOk } from "./object/Result.js";
export type { Result } from "./object/Result.js";
export type { BaseObjectSet } from "./objectSet/BaseObjectSet.js";
Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/mapping/PropertyValueMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import type { Attachment, AttachmentUpload } from "../object/Attachment.js";
import type { Media } from "../object/Media.js";
import type {
GeotimeSeriesProperty,
TimeSeriesProperty,
Expand All @@ -36,10 +37,10 @@ export interface PropertyValueWireToClient {
integer: number;
long: string;
marking: string;
mediaReference: Media;
short: number;
string: string;
timestamp: string;

numericTimeseries: TimeSeriesProperty<number>;
stringTimeseries: TimeSeriesProperty<string>;
sensorTimeseries: TimeSeriesProperty<string | number>;
Expand All @@ -65,7 +66,7 @@ export interface PropertyValueClientToWire {
short: number;
string: string;
timestamp: string;

mediaReference: Media;
numericTimeseries: TimeSeriesProperty<number>;
stringTimeseries: TimeSeriesProperty<string>;
sensorTimeseries: TimeSeriesProperty<string | number>;
Expand Down
36 changes: 36 additions & 0 deletions packages/api/src/object/Media.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2024 Palantir Technologies, Inc. All rights reserved.
*
* Licensed 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.
*/

export interface Media {
/**
* Fetches metadata for media property
mnayan marked this conversation as resolved.
Show resolved Hide resolved
*/
fetchMetadata(): Promise<MediaMetadata>;
/**
* Fetches content of a media property
*/
fetchContents(): Promise<Response>;
}

/**
* Metadata of a media item
* @param
*/
export interface MediaMetadata {
path?: string;
sizeBytes: number;
mediaType: string;
}
1 change: 1 addition & 0 deletions packages/api/src/ontology/WirePropertyTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type WirePropertyTypes =
| "decimal"
| "byte"
| "marking"
| "mediaReference"
| "numericTimeseries"
| "stringTimeseries"
| "sensorTimeseries"
Expand Down
4 changes: 2 additions & 2 deletions packages/cli.cmd.typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"@arethetypeswrong/cli": "^0.15.2",
"@osdk/cli.common": "workspace:~",
"@osdk/generator": "workspace:~",
"@osdk/internal.foundry.core": "2.6.0-beta.1",
"@osdk/internal.foundry.ontologiesv2": "2.6.0-beta.1",
"@osdk/internal.foundry.core": "2.7.0",
"@osdk/internal.foundry.ontologiesv2": "2.7.0",
"@osdk/shared.client.impl": "workspace:~",
"consola": "^3.2.3",
"fast-deep-equal": "^3.1.3",
Expand Down
4 changes: 2 additions & 2 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"@osdk/api": "workspace:~",
"@osdk/client.unstable": "workspace:*",
"@osdk/generator-converters": "workspace:*",
"@osdk/internal.foundry.core": "2.6.0-beta.1",
"@osdk/internal.foundry.ontologiesv2": "2.6.0-beta.1",
"@osdk/internal.foundry.core": "2.7.0",
"@osdk/internal.foundry.ontologiesv2": "2.7.0",
"@osdk/shared.client": "^1.0.1",
"@osdk/shared.client.impl": "workspace:~",
"@osdk/shared.client2": "^1.0.0",
Expand Down
56 changes: 56 additions & 0 deletions packages/client/src/createMediaProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2024 Palantir Technologies, Inc. All rights reserved.
*
* Licensed 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 type { Media } from "@osdk/api";
import * as OntologiesV2 from "@osdk/internal.foundry.ontologiesv2";
import type { MinimalClient } from "./MinimalClientContext.js";

export class MediaPropertyImpl implements Media {
#triplet: [string, any, string];
#client: MinimalClient;

constructor(args: {
client: MinimalClient;
objectApiName: string;
primaryKey: any;
propertyName: string;
}) {
const { client, objectApiName, primaryKey, propertyName } = args;
this.#client = client;
this.#triplet = [objectApiName, primaryKey, propertyName];
}

public async fetchContents() {
return OntologiesV2.MediaReferenceProperties.getMediaContent(
this.#client,
await this.#client.ontologyRid,
...this.#triplet,
);
}

public async fetchMetadata() {
const r = await OntologiesV2.MediaReferenceProperties.getMediaMetadata(
this.#client,
await this.#client.ontologyRid,
...this.#triplet,
);
return {
path: r.path as string,
sizeBytes: Number(r.sizeBytes),
mediaType: r.mediaType,
};
}
}
6 changes: 0 additions & 6 deletions packages/client/src/createTimeseriesProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,8 @@ import type {
TimeSeriesProperty,
TimeSeriesQuery,
} from "@osdk/api";
import { TimeseriesDurationMapping } from "@osdk/api";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just deleting unused stuff here right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup.

import type { TimeRange } from "@osdk/internal.foundry.core";
import * as OntologiesV2 from "@osdk/internal.foundry.ontologiesv2";
import type { MinimalClient } from "./MinimalClientContext.js";
import {
iterateReadableStream,
parseStreamedResponse,
} from "./util/streamutils.js";
import { asyncIterPointsHelper, getTimeRange } from "./util/timeseriesUtils.js";

export class TimeSeriesPropertyImpl<T extends number | string>
Expand Down
27 changes: 26 additions & 1 deletion packages/client/src/object/convertWireToOsdkObjects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type { Attachment, Osdk, PropertyKeys } from "@osdk/api";
import type { Attachment, Media, Osdk, PropertyKeys } from "@osdk/api";
import {
$ontologyRid,
Employee,
Expand Down Expand Up @@ -150,6 +150,31 @@ describe("convertWireToOsdkObjects", () => {
expect(emptyAttachmentArray).toBeUndefined();
});

it("converts media as expected", async () => {
const withValues = await client(
objectTypeWithAllPropertyTypes,
)
.where({ id: 1 })
.fetchPage();
expect(withValues.data.length).toBeGreaterThanOrEqual(1);

const { mediaReference } = withValues.data[0];

expectTypeOf(mediaReference).toMatchTypeOf<
Media | undefined
>;
expect(mediaReference).toBeDefined();

const withoutValues = await client(
objectTypeWithAllPropertyTypes,
).where({ id: 2 }).fetchPage();

const {
mediaReference: emptyMedia,
} = withoutValues.data[0];
expect(emptyMedia).toBeUndefined();
});

it("creates immutable objects", async () => {
const employees = await client(Employee).fetchPage();
expect(employees.data.length).toBeGreaterThanOrEqual(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@
* limitations under the License.
*/

import type {
GeotimeSeriesProperty,
ObjectTypeDefinition,
Osdk,
TimeSeriesPoint,
} from "@osdk/api";
import type { ObjectTypeDefinition, Osdk } from "@osdk/api";
import type { OntologyObjectV2 } from "@osdk/internal.foundry.core";
import { OntologiesV2 } from "@osdk/internal.foundry.ontologiesv2";
import { createAttachmentFromRid } from "../../createAttachmentFromRid.js";
import { GeotimeSeriesPropertyImpl } from "../../createGeotimeSeriesProperty.js";
import { MediaPropertyImpl } from "../../createMediaProperty.js";
import { TimeSeriesPropertyImpl } from "../../createTimeseriesProperty.js";
import type { MinimalClient } from "../../MinimalClientContext.js";
import type { FetchedObjectTypeDefinition } from "../../ontology/OntologyProvider.js";
Expand Down Expand Up @@ -223,6 +218,15 @@ export function createOsdkObject<
target[CachedPropertiesRef].set(p, geotimeProp);
return geotimeProp;
}
if (propDef.type === "mediaReference") {
return new MediaPropertyImpl({
client,
objectApiName: objectDef.apiName,
primaryKey:
target[RawObject][objectDef.primaryKeyApiName as string],
propertyName: p as string,
});
}
}
return rawValue;
}
Expand Down
69 changes: 69 additions & 0 deletions packages/client/src/object/media.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2024 Palantir Technologies, Inc. All rights reserved.
*
* Licensed 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 {
$ontologyRid,
objectTypeWithAllPropertyTypes,
} from "@osdk/client.test.ontology";
import { apiServer, stubData } from "@osdk/shared.test";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import type { Client } from "../Client.js";
import { createClient } from "../createClient.js";

describe("media", () => {
let client: Client;

beforeAll(async () => {
apiServer.listen();
client = createClient(
"https://stack.palantir.com",
$ontologyRid,
async () => "myAccessToken",
);
});

afterAll(() => {
apiServer.close();
});

it("reads media metadata successfully", async () => {
const result = await client(
objectTypeWithAllPropertyTypes,
)
.where({ id: stubData.objectWithAllPropertyTypes1.id }).fetchPage();

const object1 = result.data[0];
expect(object1.mediaReference).toBeDefined();
const mediaMetadata = await object1.mediaReference?.fetchMetadata();
expect(mediaMetadata).toBeDefined();
expect(mediaMetadata?.path).toEqual("file1.txt");
expect(mediaMetadata?.mediaType).toEqual("application/json");
expect(mediaMetadata?.sizeBytes).toEqual(20);
});

it("reads media content successfully", async () => {
const result = await client(objectTypeWithAllPropertyTypes)
.where({ id: stubData.objectWithAllPropertyTypes1.id }).fetchPage();

const object1 = result.data[0];
expect(object1.mediaReference).toBeDefined();
const mediaContent = await object1?.mediaReference?.fetchContents();
const mediaText = await mediaContent!.text();
expect(JSON.parse(mediaText)).toEqual({
content: "Hello World",
});
});
});
2 changes: 1 addition & 1 deletion packages/e2e.generated.1.1.x/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"peerDependencies": {
"@osdk/api": "^1.10.0-beta.1",
"@osdk/legacy-client": "^2.6.0-beta.1"
"@osdk/legacy-client": "^2.7.0"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.15.2",
Expand Down
35 changes: 35 additions & 0 deletions packages/e2e.generated.catchall/ontology.json
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,41 @@
},
"linkTypes": []
},
"MnayanOsdkMediaObject": {
"objectType": {
"apiName": "MnayanOsdkMediaObject",
"primaryKey": "id",
"displayName": "MnayanOSDKMediaObject",
"description": "Media OT for OSDK e2e testing",
"properties": {
"id": {
"dataType": {
"type": "string"
}
},
"path": {
"dataType": {
"type": "string"
}
},
"mediaReference": {
"dataType": {
"type": "mediaReference"
}
}
},
"status": "ACTIVE",
"rid": "rid.a.b.c.d",
"icon": {
"type": "blueprint",
"name": "object",
"color": "color"
},
"titleProperty": "path",
"pluralDisplayName": "Mnayan OSDKMedia Objects"
},
"linkTypes": []
},
"SotSensor": {
"objectType": {
"apiName": "SotSensor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export {
Employee,
FintrafficAis,
GtfsTripTrackObject,
MnayanOsdkMediaObject,
MtaBus,
ObjectTypeWithAllPropertyTypes,
OsdkTestObject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { DherlihyComplexObject } from './objects/DherlihyComplexObject.js';
export { Employee } from './objects/Employee.js';
export { FintrafficAis } from './objects/FintrafficAis.js';
export { GtfsTripTrackObject } from './objects/GtfsTripTrackObject.js';
export { MnayanOsdkMediaObject } from './objects/MnayanOsdkMediaObject.js';
export { MtaBus } from './objects/MtaBus.js';
export { ObjectTypeWithAllPropertyTypes } from './objects/ObjectTypeWithAllPropertyTypes.js';
export { OsdkTestObject } from './objects/OsdkTestObject.js';
Expand Down
Loading
Loading