Skip to content

feat: add predictEndpointMiddleware for Machinelearning #553

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 4 commits into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ public List<RuntimeClientPlugin> getClientPlugins() {
.servicePredicate((m, s) -> testServiceId(s, "S3"))
.operationPredicate((m, s, o) -> o.getId().getName().equals("CreateBucket"))
.build(),
RuntimeClientPlugin.builder()
.withConventions(AwsDependency.MACHINELEARNING_MIDDLEWARE.dependency, "PredictEndpoint",
HAS_MIDDLEWARE)
.servicePredicate((m, s) -> testServiceId(s, "Machine Learning"))
.operationPredicate((m, s, o) -> o.getId().getName().equals("Predict"))
.build(),
RuntimeClientPlugin.builder()
.withConventions(AwsDependency.BODY_CHECKSUM.dependency, "ApplyMd5BodyChecksum",
HAS_MIDDLEWARE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public enum AwsDependency implements SymbolDependencyContainer {
VALIDATE_BUCKET_NAME(NORMAL_DEPENDENCY, "@aws-sdk/middleware-sdk-s3", "^0.1.0-preview.2"),
ADD_EXPECT_CONTINUE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-expect-continue", "^0.1.0-preview.5"),
GLACIER_MIDDLEWARE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-sdk-glacier", "^0.1.0-preview.7"),
MACHINELEARNING_MIDDLEWARE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-sdk-machinelearning", "^0.1.0-preview.1"),
SSEC_MIDDLEWARE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-ssec", "^0.1.0-preview.5"),
LOCATION_CONSTRAINT(NORMAL_DEPENDENCY, "@aws-sdk/middleware-location-constraint", "^0.1.0-preview.5"),
MD5_BROWSER(NORMAL_DEPENDENCY, "@aws-sdk/md5-js", "^0.1.0-preview.8"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/build/
/coverage/
/docs/
*.tsbuildinfo
*.tgz
*.log
package-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/coverage/
/docs/
tsconfig.test.json
*.tsbuildinfo

*.spec.js
*.spec.d.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Copyright 2019 Amazon.com, Inc. or its affiliates. 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.
Expand Down
4 changes: 4 additions & 0 deletions packages/middleware-sdk-machinelearning/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# @aws-sdk/@aws-sdk/middleware-sdk-machinelearning

[![NPM version](https://img.shields.io/npm/v/@aws-sdk/@aws-sdk/middleware-sdk-machinelearning/preview.svg)](https://www.npmjs.com/package/@aws-sdk/@aws-sdk/middleware-sdk-machinelearning)
[![NPM downloads](https://img.shields.io/npm/dm/@aws-sdk/@aws-sdk/middleware-sdk-machinelearning.svg)](https://www.npmjs.com/package/@aws-sdk/@aws-sdk/middleware-sdk-machinelearning)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aws-sdk/modeled-endpoint-middleware",
"version": "0.1.0-preview.6",
"name": "@aws-sdk/middleware-sdk-machinelearning",
"version": "0.1.0-preview.1",
"scripts": {
"prepublishOnly": "tsc",
"pretest": "tsc -p tsconfig.test.json",
Expand All @@ -14,11 +14,10 @@
},
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/types": "^0.1.0-preview.5",
"tslib": "^1.8.0"
},
"devDependencies": {
"@aws-sdk/url-parser-universal": "^0.1.0-preview.6",
"@aws-sdk/types": "^0.1.0-preview.5",
"@types/jest": "^24.0.12",
"jest": "^24.7.1",
"typescript": "~3.4.0"
Expand Down
19 changes: 19 additions & 0 deletions packages/middleware-sdk-machinelearning/src/configurations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { UrlParser } from "@aws-sdk/types";

export interface PredictEndpointMiddlewareInputConfig {}

interface PreviouslyResolved {
urlParser: UrlParser;
}

export interface ResolvedPredictEndpointMiddlewareConfig {
urlParser: UrlParser;
}

export function resolvePredictEndpointMiddlewareConfig<T>(
input: T & PreviouslyResolved & PredictEndpointMiddlewareInputConfig
): T & ResolvedPredictEndpointMiddlewareConfig {
return {
...input
};
}
11 changes: 11 additions & 0 deletions packages/middleware-sdk-machinelearning/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { predictEndpointMiddleware, getPredictEndpointPlugin } from "./index";

describe("middleware-sdk-machinelearning package exports", () => {
it("predictEndpointMiddleware", () => {
expect(typeof predictEndpointMiddleware).toBe("function");
});

it("getPredictEndpointPlugin", () => {
expect(typeof getPredictEndpointPlugin).toBe("function");
});
});
1 change: 1 addition & 0 deletions packages/middleware-sdk-machinelearning/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./predict-endpoint";
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { predictEndpointMiddleware } from "./predict-endpoint";
import { HttpRequest } from "@aws-sdk/protocol-http";

describe("predictEndpointMiddleware", () => {
const mockUrlParser = jest.fn().mockReturnValue({
hostname: "api.example.com",
path: "/foo/bar",
protocol: "http:"
});
const next = jest.fn();

beforeEach(() => {
mockUrlParser.mockClear();
next.mockClear();
});

it("should set the request endpoint using the PredictEndpoint input", async () => {
const input = { PredictEndpoint: "http://api.example.com/foo/bar" };
const request = new HttpRequest({});
const handler = predictEndpointMiddleware({
urlParser: mockUrlParser
})(next, {} as any);
await handler({ input, request });

const {
input: forwardedInput,
request: { hostname, path, protocol }
} = next.mock.calls[0][0];

expect(forwardedInput).toBe(input);
expect(hostname).toBe("api.example.com");
expect(path).toBe("/foo/bar");
expect(protocol).toBe("http:");
});
});
58 changes: 58 additions & 0 deletions packages/middleware-sdk-machinelearning/src/predict-endpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {
BuildHandler,
BuildHandlerArguments,
BuildHandlerOptions,
BuildHandlerOutput,
BuildMiddleware,
MetadataBearer,
Pluggable
} from "@aws-sdk/types";
import { HttpRequest } from "@aws-sdk/protocol-http";
import { ResolvedPredictEndpointMiddlewareConfig } from "./configurations";

export function predictEndpointMiddleware(
options: ResolvedPredictEndpointMiddlewareConfig
): BuildMiddleware<any, any> {
return <Output extends MetadataBearer>(
next: BuildHandler<any, Output>
): BuildHandler<any, Output> => async (
args: BuildHandlerArguments<any>
): Promise<BuildHandlerOutput<Output>> => {
const { input } = args;
let { request } = args;
if (HttpRequest.isInstance(request)) {
if (input.PredictEndpoint) {
const endpoint = options.urlParser(input.predictEndpoint);
request = {
...request,
hostname: endpoint.hostname,
path: endpoint.path,
port: endpoint.port,
protocol: endpoint.protocol,
query: endpoint.query
};
}
}
return next({
...args,
request
});
};
}

export const predictEndpointMiddlewareOptions: BuildHandlerOptions = {
step: "build",
tags: ["PREDICT_ENDPOINT"],
name: "predictEndpointMiddleware"
};

export const getPredictEndpointPlugin = (
config: ResolvedPredictEndpointMiddlewareConfig
): Pluggable<any, any> => ({
applyToStack: clientStack => {
clientStack.add(
predictEndpointMiddleware(config),
predictEndpointMiddlewareOptions
);
}
});
82 changes: 0 additions & 82 deletions packages/modeled-endpoint-middleware/CHANGELOG.md

This file was deleted.

4 changes: 0 additions & 4 deletions packages/modeled-endpoint-middleware/README.md

This file was deleted.

Loading