Skip to content

Commit

Permalink
build: allow forced rmf server version, add script to generate api-cl…
Browse files Browse the repository at this point in the history
…ient openapi

Signed-off-by: Teo Koon Peng <[email protected]>
  • Loading branch information
Teo Koon Peng committed Apr 12, 2021
1 parent bf4b808 commit 6f8d4a3
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 87 deletions.
20 changes: 1 addition & 19 deletions packages/api-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,6 @@ client.on('door_states', console.log);
Requirements:
* java >= 8


Download the swagger codegen
```bash
wget https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.25/swagger-codegen-cli-3.0.25.jar -O .bin/swagger-codegen-cli.jar
```

Run the api-server locally
```bash
cd ../api_server
npm start
```

Generate code
```bash
java -jar .bin/swagger-codegen-cli.jar generate -ihttp://localhost:8000/openapi.json -ltypescript-axios -olib/openapi
```

There is a bug with `ModelObject` type being missing, workaround it by adding a type to the generated models.
```bash
echo 'export type ModelObject = Record<string, any>;' >> lib/openapi/models/index.ts
./generate-openapi.sh
```
9 changes: 9 additions & 0 deletions packages/api-client/generate-openapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import json
import os

from api_server.app import app

here = os.path.realpath(os.path.dirname(__file__))
os.makedirs(f"{here}/build", exist_ok=True)
with open(f"{here}/build/openapi.json", "w") as f:
json.dump(app.openapi(), f)
44 changes: 44 additions & 0 deletions packages/api-client/generate-openapi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash
set -e

function usage() {
echo "Usage: generate-models.sh"
}

cd $(dirname $0)

source ../../scripts/version.sh

expected_sha='cde6255246cf76b9e4c20e85968d36505930700f8adad68e76c0bc5a6721bfe5'

if [[ ! -f '.bin/swagger-codegen-cli.jar' ]]; then
wget https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.25/swagger-codegen-cli-3.0.25.jar -O .bin/swagger-codegen-cli.jar
fi

sha=$(sha256sum .bin/swagger-codegen-cli.jar | awk '{print $1}')

if [[ $sha != $expected_sha ]]; then
echo "ERR: .bin/swagger-codegen-cli.jar sha doesn't match"
exit 1
fi

swagger_ver=$(java -jar .bin/swagger-codegen-cli.jar version)
java -jar .bin/swagger-codegen-cli.jar generate -i'build/openapi.json' -ltypescript-axios -olib/openapi
# There is a bug with `ModelObject` type being missing, workaround it by adding a type to the generated models.
echo 'export type ModelObject = Record<string, any>;' >> lib/openapi/models/index.ts

rmf_server_ver=$(getVersion .)

cat << EOF > lib/version.ts
// THIS FILE IS GENERATED
import { version as rmfModelVer } from 'rmf-models';
export const version = {
rmfModels: rmfModelVer,
rmfServer: '$rmf_server_ver',
swaggerCodegen: '$swagger_ver',
};
EOF

../../node_modules/.bin/prettier -w lib
21 changes: 13 additions & 8 deletions packages/api-client/lib/openapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,35 @@
This generator creates TypeScript/JavaScript client that utilizes [axios](https://github.com/axios/axios). The generated Node module can be used in the following environments:

Environment
* Node.js
* Webpack
* Browserify

- Node.js
- Webpack
- Browserify

Language level
* ES5 - you must have a Promises/A+ library installed
* ES6

- ES5 - you must have a Promises/A+ library installed
- ES6

Module system
* CommonJS
* ES6 module system

- CommonJS
- ES6 module system

It can be used in both TypeScript and JavaScript. In TypeScript, the definition should be automatically resolved via `package.json`. ([Reference](http://www.typescriptlang.org/docs/handbook/typings-for-npm-packages.html))

### Building

To build and compile the typescript sources to javascript use:

```
npm install
npm run build
```

### Publishing

First build the package then run ```npm publish```
First build the package then run `npm publish`

### Consuming

Expand All @@ -43,3 +47,4 @@ _unPublished (not recommended):_

```
npm install PATH_TO_GENERATED_PACKAGE --save
```
2 changes: 1 addition & 1 deletion packages/api-client/lib/openapi/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/ export * from './apis/default-api';
*/ export * from './apis/building-api';
export * from './apis/doors-api';
export * from './apis/lifts-api';
export * from './apis/tasks-api';
139 changes: 139 additions & 0 deletions packages/api-client/lib/openapi/apis/building-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/* tslint:disable */
/* eslint-disable */
/**
* FastAPI
* No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
*
* OpenAPI spec version: 0.1.0
*
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
import globalAxios, { AxiosPromise, AxiosInstance } from 'axios';
import { Configuration } from '../configuration';
// Some imports not used depending on template conditions
// @ts-ignore
import { BASE_PATH, COLLECTION_FORMATS, RequestArgs, BaseAPI, RequiredError } from '../base';
import { BuildingMap } from '../models';
/**
* BuildingApi - axios parameter creator
* @export
*/
export const BuildingApiAxiosParamCreator = function (configuration?: Configuration) {
return {
/**
*
* @summary Get Building Map
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
getBuildingMapBuildingMapGet: async (options: any = {}): Promise<RequestArgs> => {
const localVarPath = `/building_map`;
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, 'https://example.com');
let baseOptions;
if (configuration) {
baseOptions = configuration.baseOptions;
}
const localVarRequestOptions = { method: 'GET', ...baseOptions, ...options };
const localVarHeaderParameter = {} as any;
const localVarQueryParameter = {} as any;

const query = new URLSearchParams(localVarUrlObj.search);
for (const key in localVarQueryParameter) {
query.set(key, localVarQueryParameter[key]);
}
for (const key in options.query) {
query.set(key, options.query[key]);
}
localVarUrlObj.search = new URLSearchParams(query).toString();
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {
...localVarHeaderParameter,
...headersFromBaseOptions,
...options.headers,
};

return {
url: localVarUrlObj.pathname + localVarUrlObj.search + localVarUrlObj.hash,
options: localVarRequestOptions,
};
},
};
};

/**
* BuildingApi - functional programming interface
* @export
*/
export const BuildingApiFp = function (configuration?: Configuration) {
return {
/**
*
* @summary Get Building Map
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async getBuildingMapBuildingMapGet(
options?: any,
): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<BuildingMap>> {
const localVarAxiosArgs = await BuildingApiAxiosParamCreator(
configuration,
).getBuildingMapBuildingMapGet(options);
return (axios: AxiosInstance = globalAxios, basePath: string = BASE_PATH) => {
const axiosRequestArgs = {
...localVarAxiosArgs.options,
url: basePath + localVarAxiosArgs.url,
};
return axios.request(axiosRequestArgs);
};
},
};
};

/**
* BuildingApi - factory interface
* @export
*/
export const BuildingApiFactory = function (
configuration?: Configuration,
basePath?: string,
axios?: AxiosInstance,
) {
return {
/**
*
* @summary Get Building Map
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
getBuildingMapBuildingMapGet(options?: any): AxiosPromise<BuildingMap> {
return BuildingApiFp(configuration)
.getBuildingMapBuildingMapGet(options)
.then((request) => request(axios, basePath));
},
};
};

/**
* BuildingApi - object-oriented interface
* @export
* @class BuildingApi
* @extends {BaseAPI}
*/
export class BuildingApi extends BaseAPI {
/**
*
* @summary Get Building Map
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof BuildingApi
*/
public getBuildingMapBuildingMapGet(options?: any) {
return BuildingApiFp(this.configuration)
.getBuildingMapBuildingMapGet(options)
.then((request) => request(this.axios, this.basePath));
}
}
14 changes: 3 additions & 11 deletions packages/api-client/lib/openapi/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,8 @@
"noImplicitAny": true,
"outDir": "dist",
"rootDir": ".",
"lib": [
"es6",
"dom"
],
"typeRoots": [
"node_modules/@types"
]
"lib": ["es6", "dom"],
"typeRoots": ["node_modules/@types"]
},
"exclude": [
"dist",
"node_modules"
]
"exclude": ["dist", "node_modules"]
}
8 changes: 8 additions & 0 deletions packages/api-client/lib/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// THIS FILE IS GENERATED
import { version as rmfModelVer } from 'rmf-models';

export const version = {
rmfModels: rmfModelVer,
rmfServer: 'bf4b8085153d73ab5094504e34fbac0d1fbeb6f4',
swaggerCodegen: '3.0.25',
};
11 changes: 0 additions & 11 deletions packages/rmf-models/README.md

This file was deleted.

17 changes: 9 additions & 8 deletions packages/rmf-models/generate-models.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ console.log('generate models:');
path.join(tortoiseDir, 'GENERATED'),
'THIS DIRECTORY IS GENERATED, DO NOT EDIT!!',
);
})();

execSync(`pipenv run python -m ts_ros -o lib/ros ${rmfMsgs.join(' ')}`, {
stdio: 'inherit',
});
fs.writeFileSync(
path.join(__dirname, 'lib', 'ros', 'GENERATED'),
'THIS DIRECTORY IS GENERATED, DO NOT EDIT!!',
);
execSync(`pipenv run python -m ts_ros -o lib/ros ${rmfMsgs.join(' ')}`, {
stdio: 'inherit',
});
fs.writeFileSync(
path.join(__dirname, 'lib', 'ros', 'GENERATED'),
'THIS DIRECTORY IS GENERATED, DO NOT EDIT!!',
);
execSync(`../../node_modules/.bin/prettier -w lib/ros lib/tortoise`, { stdio: 'inherit' });
})();
Loading

0 comments on commit 6f8d4a3

Please sign in to comment.