Skip to content

Commit

Permalink
Update from fluxninja/aperture
Browse files Browse the repository at this point in the history
  • Loading branch information
fluxninjaops committed Nov 21, 2023
1 parent 10645c1 commit 8ba902a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
21 changes: 16 additions & 5 deletions example/routes/use_aperture.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import express from "express";
import { ApertureClient, Flow, FlowStatusEnum, LookupStatus } from "@fluxninja/aperture-js";
import {
ApertureClient,
Flow,
FlowStatusEnum,
LookupStatus,
} from "@fluxninja/aperture-js";
import grpc from "@grpc/grpc-js";


// Create aperture client
export const apertureClient = new ApertureClient({
address:
process.env.APERTURE_AGENT_ADDRESS !== undefined
? process.env.APERTURE_AGENT_ADDRESS
: "localhost:8089",
agentAPIKey: process.env.APERTURE_AGENT_API_KEY || undefined,
apiKey: process.env.APERTURE_API_KEY || undefined,
// if process.env.APERTURE_AGENT_INSECURE set channelCredentials to insecure
channelCredentials:
process.env.APERTURE_AGENT_INSECURE !== undefined
Expand Down Expand Up @@ -43,14 +47,21 @@ apertureRoute.get("/", async (_: express.Request, res: express.Response) => {
if (flow.CachedValue().GetLookupStatus() === LookupStatus.Hit) {
console.log("Cache hit:", flow.CachedValue().GetValue()?.toString());
} else {
console.log("Cache miss:", flow.CachedValue().GetOperationStatus(), flow.CachedValue().GetError()?.message);
console.log(
"Cache miss:",
flow.CachedValue().GetOperationStatus(),
flow.CachedValue().GetError()?.message,
);
const resString = "awesomeString";

// create a new buffer
const buffer = Buffer.from(resString);

// set cache value
const setResult = await flow.SetCachedValue(buffer, { seconds: 30, nanos: 0 })
const setResult = await flow.SetCachedValue(buffer, {
seconds: 30,
nanos: 0,
});
if (setResult?.error) {
console.log(`Error setting cache value: ${setResult.error}`);
}
Expand Down
27 changes: 21 additions & 6 deletions sdk/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@ export class ApertureClient {

constructor({
address,
agentAPIKey,
apiKey,
channelCredentials = grpc.credentials.createSsl(),
channelOptions = {},
}: {
address: string;
channelCredentials?: ChannelCredentials;
channelOptions?: ChannelOptions;
agentAPIKey?: string;
apiKey?: string;
}) {
if (!address) {
throw new Error("address is required");
}

if (agentAPIKey) {
if (apiKey) {
channelCredentials = grpc.credentials.combineChannelCredentials(
channelCredentials,
grpc.credentials.createFromMetadataGenerator(
(_params: any, callback: any) => {
const metadata = new grpc.Metadata();
metadata.add("x-api-key", agentAPIKey);
metadata.add("x-api-key", apiKey);
callback(null, metadata);
},
),
Expand Down Expand Up @@ -104,7 +104,19 @@ export class ApertureClient {
let startDate = Date.now();

const resolveFlow = (response: any, err: any) => {
resolve(new Flow(this.fcsClient, params.grpcCallOptions ?? {}, controlPoint, span, startDate, params.rampMode, params.cacheKey, response, err));
resolve(
new Flow(
this.fcsClient,
params.grpcCallOptions ?? {},
controlPoint,
span,
startDate,
params.rampMode,
params.cacheKey,
response,
err,
),
);
};

try {
Expand All @@ -126,7 +138,10 @@ export class ApertureClient {
cacheKey: params.cacheKey,
};

const cb: grpc.requestCallback<CheckResponse__Output> = (err: any, response: any) => {
const cb: grpc.requestCallback<CheckResponse__Output> = (
err: any,
response: any,
) => {
resolveFlow(err ? null : response, err);
return;
};
Expand Down

0 comments on commit 8ba902a

Please sign in to comment.