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 17 commits into
base: main
Choose a base branch
from
8 changes: 8 additions & 0 deletions .changeset/poor-clocks-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@osdk/generator-converters": patch
"@osdk/shared.test": patch
"@osdk/client": patch
"@osdk/api": patch
---

Add support for reading media reference property in OSDK.
21 changes: 20 additions & 1 deletion etc/api.report.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,23 @@ export type LinkedType<Q extends ObjectOrInterfaceDefinition, L extends LinkName
// @public (undocumented)
export type LinkNames<Q extends ObjectOrInterfaceDefinition> = keyof CompileTimeMetadata<Q>["links"] & string;

// @public (undocumented)
export interface Media {
fetchContents(): Promise<Response>;
fetchMetadata(): Promise<MediaMetadata_2>;
}

// @public
interface MediaMetadata_2 {
// (undocumented)
mediaType: string;
// (undocumented)
path?: string;
// (undocumented)
sizeBytes: number;
}
export { MediaMetadata_2 as MediaMetadata }

// @public (undocumented)
export type NullabilityAdherence = false | "throw" | "drop";

Expand Down Expand Up @@ -765,6 +782,8 @@ export interface PropertyValueWireToClient {
// (undocumented)
marking: string;
// (undocumented)
mediaReference: Media;
// (undocumented)
numericTimeseries: TimeSeriesProperty<number>;
// (undocumented)
sensorTimeseries: TimeSeriesProperty<string | number>;
Expand Down Expand Up @@ -858,7 +877,7 @@ export interface SelectArg<Q extends ObjectOrInterfaceDefinition, L extends Prop
export type SelectArgToKeys<Q extends ObjectOrInterfaceDefinition, A extends SelectArg<Q, any, any>> = A extends SelectArg<Q, never> ? PropertyKeys<Q> : A["$select"] extends readonly string[] ? A["$select"][number] : PropertyKeys<Q>;

// @public (undocumented)
export type SimpleWirePropertyTypes = "string" | "datetime" | "double" | "boolean" | "integer" | "timestamp" | "short" | "long" | "float" | "decimal" | "byte" | "marking" | "numericTimeseries" | "stringTimeseries" | "sensorTimeseries" | "attachment" | "geopoint" | "geoshape" | "geotimeSeriesReference";
export type SimpleWirePropertyTypes = "string" | "datetime" | "double" | "boolean" | "integer" | "timestamp" | "short" | "long" | "float" | "decimal" | "byte" | "marking" | "mediaReference" | "numericTimeseries" | "stringTimeseries" | "sensorTimeseries" | "attachment" | "geopoint" | "geoshape" | "geotimeSeriesReference";

// @public (undocumented)
export interface SingleLinkAccessor<T extends ObjectTypeDefinition> {
Expand Down
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, MediaMetadata } 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 Down Expand Up @@ -74,7 +75,7 @@ export interface PropertyValueClientToWire {
short: number;
string: string;
timestamp: string;

mediaReference: Media;
numericTimeseries: TimeSeriesProperty<number>;
stringTimeseries: TimeSeriesProperty<string>;
sensorTimeseries: TimeSeriesProperty<string | number>;
Expand Down
35 changes: 35 additions & 0 deletions packages/api/src/object/Media.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 reference property
*/
fetchMetadata(): Promise<MediaMetadata>;
/**
* Fetches content of a media reference property
*/
fetchContents(): Promise<Response>;
}

/**
* Metadata of a media item
*/
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 @@ -31,6 +31,7 @@ export type SimpleWirePropertyTypes =
| "decimal"
| "byte"
| "marking"
| "mediaReference"
| "numericTimeseries"
| "stringTimeseries"
| "sensorTimeseries"
Expand Down
56 changes: 56 additions & 0 deletions packages/client/src/createMediaReferenceProperty.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 MediaReferencePropertyImpl 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,
};
}
}
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 @@ -151,6 +151,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 @@ -19,6 +19,7 @@ import type { OntologyObjectV2 } from "@osdk/internal.foundry.core";
import invariant from "tiny-invariant";
import { createAttachmentFromRid } from "../../createAttachmentFromRid.js";
import { GeotimeSeriesPropertyImpl } from "../../createGeotimeSeriesProperty.js";
import { MediaReferencePropertyImpl } from "../../createMediaReferenceProperty.js";
import { TimeSeriesPropertyImpl } from "../../createTimeseriesProperty.js";
import type { MinimalClient } from "../../MinimalClientContext.js";
import type { FetchedObjectTypeDefinition } from "../../ontology/OntologyProvider.js";
Expand All @@ -40,6 +41,7 @@ const specialPropertyTypes = new Set(
[
"attachment",
"geotimeSeriesReference",
"mediaReference",
"numericTimeseries",
"stringTimeseries",
"sensorTimeseries",
Expand Down Expand Up @@ -158,6 +160,14 @@ function createSpecialProperty(
: undefined,
);
}
if (propDef.type === "mediaReference") {
return new MediaReferencePropertyImpl({
client,
objectApiName: objectDef.apiName,
primaryKey: rawObject[objectDef.primaryKeyApiName as string],
propertyName: p as string,
});
}
}
}
}
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 @@ -631,6 +631,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 @@ -18,6 +18,7 @@ export {
FintrafficAis,
GtfsTripTrackObject,
McAirportStruct,
MnayanOsdkMediaObject,
MtaBus,
ObjectTypeWithAllPropertyTypes,
OsdkTestObject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export { Employee } from './objects/Employee.js';
export { FintrafficAis } from './objects/FintrafficAis.js';
export { GtfsTripTrackObject } from './objects/GtfsTripTrackObject.js';
export { McAirportStruct } from './objects/McAirportStruct.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