Skip to content

Add ingress client #282

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

Merged
merged 1 commit into from
Mar 20, 2024
Merged

Add ingress client #282

merged 1 commit into from
Mar 20, 2024

Conversation

igalshilman
Copy link
Contributor

@igalshilman igalshilman commented Mar 20, 2024

This PR adds the first version of a typed client, that can be used outside of restate to communicate with restate backed services/objects .
See examples/ingress.ts for a usage example.

import * as restate from "../src/public_api";
import type { CounterObject, GreeterService } from "./example";

const Greeter: GreeterService = { path: "greeter" };
const Counter: CounterObject = { path: "counter" };

const ingress = restate.ingress.connect({ url: "http://localhost:8080" });

const simpleCall = async (name: string) => {
  const greeter = ingress.service(Greeter);
  const greeting = await greeter.greet(name);

  console.log(greeting);
};

const objectCall = async (name: string) => {
  const couter = ingress.object(Counter, name);
  const count = await couter.count();

  console.log(`The count for ${name} is ${count}`);
};

const idempotentCall = async (name: string, idempotencyKey: string) => {
  const greeter = ingress.service(Greeter);

  // send the request with the idempotent key, and ask restate
  // to remember that key for 3 seconds.
  const greeting = await greeter.greet(
    name,
    restate.ingress.Opts.from({ idempotencyKey, retain: 3 })
  );

  console.log(greeting);
};

const customHeadersCall = async (name: string) => {
  const greeter = ingress.service(Greeter);

  const greeting = await greeter.greet(
    name,
    restate.ingress.Opts.from({ headers: { "x-bob": "1234" } })
  );

  console.log(greeting);
};

const globalCustomHeaders = async (name: string) => {
  const ingress = restate.ingress.connect({
    url: "http://localhost:8080",
    headers: { Authorization: "Bearer mytoken123" },
  });

  const greeting = await ingress.service(Greeter).greet(name);

  console.log(greeting);
};

// Before running this example, make sure
// to run and register `greeter` and `counter` services.
//
// to run them, run:
//
// 1. npm run example
// 2. make sure that restate is running
// 3. restate deployment add localhost:9080

Promise.resolve()
  .then(() => simpleCall("bob"))
  .then(() => objectCall("bob"))
  .then(() => objectCall("mop"))
  .then(() => idempotentCall("joe", "idemp-1"))
  .then(() => idempotentCall("joe", "idemp-1"))
  .then(() => customHeadersCall("bob"))
  .then(() => globalCustomHeaders("bob"))
  .catch((e) => console.error(e));

Copy link
Contributor

@slinkydeveloper slinkydeveloper left a comment

Choose a reason for hiding this comment

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

Left a couple of comments.


export interface IngresCallOptions {
idempotencyKey?: string;
retain?: number;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you allow to add additional headers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah!, good idea!

Comment on lines +31 to +32
idempotencyKey?: string;
retain?: number;
Copy link
Contributor

Choose a reason for hiding this comment

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

A thing I would have done differently is to group the idempotency options all together, like

export interface IdempotencyOptions {
  key: string;
  retain?: number;
}

export interface IngressCallOptions {
  idempotency?: IdempotencyOptions;
  headers: {[string]: string};
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd rather to keep it flat for now.

fragments.push(params.handler);
if (params.send ?? false) {
if (params.delay) {
fragments.push(`send?delay=${params.delay}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is now number right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is indeed a number, like the retention number. We said that we would like to align both of them once that feature will be implemented.

This commit adds the first version of a typed client.
See examples/ingress.ts for a usage example.
@igalshilman igalshilman merged commit 0218b76 into restatedev:main Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants