diff --git a/.github/workflows/4-release.yaml b/.github/workflows/4-release.yaml index 177590a6b..4c6c15254 100644 --- a/.github/workflows/4-release.yaml +++ b/.github/workflows/4-release.yaml @@ -67,6 +67,7 @@ jobs: permissions: contents: write packages: write + pull-requests: write with: # We cannot use environment variables here due to workflow limitation. # https://docs.github.com/en/enterprise-cloud@latest/actions/using-workflows/reusing-workflows#limitations diff --git a/.github/workflows/4.a-generate-webapi-clients.yaml b/.github/workflows/4.a-generate-webapi-clients.yaml index d14602218..52f0c8c9e 100644 --- a/.github/workflows/4.a-generate-webapi-clients.yaml +++ b/.github/workflows/4.a-generate-webapi-clients.yaml @@ -31,6 +31,7 @@ on: permissions: contents: write packages: write + pull-requests: write jobs: detect-api-version: @@ -46,3 +47,43 @@ jobs: with: image: ${{ needs.detect-api-version.outputs.image }} apiver: ${{ needs.detect-api-version.outputs.apiver }} + + generate-npm-client: + needs: detect-api-version + uses: ./.github/workflows/4.a.1-generate-webapi-client-npm.yaml + with: + image: ${{ needs.detect-api-version.outputs.image }} + apiver: ${{ needs.detect-api-version.outputs.apiver }} + + push-apidocs: + runs-on: ubuntu-latest + needs: + - detect-api-version + - generate-java-client + - generate-npm-client + env: + DOCPATH: casdk-docs/static/client-apidocs/${{ needs.detect-api-version.outputs.apiver }} + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + ref: dev + - name: Extract Javadoc + uses: actions/download-artifact@v4 + with: + name: javadoc + path: ${{ env.DOCPATH }}/java + - name: Extract TypeDoc + uses: actions/download-artifact@v4 + with: + name: typedoc + path: ${{ env.DOCPATH }}/npm + - name: Create PR for Client API docs + uses: peter-evans/create-pull-request@v7 + with: + commit-message: "Add Client API documents for WebAPI ${{ needs.detect-api-version.outputs.apiver }}" + branch: gha/pr/webapidoc-${{ needs.detect-api-version.outputs.apiver }} + delete-branch: true + title: "Add Client API documents for WebAPI ${{ needs.detect-api-version.outputs.apiver }}" + body: | + This PR has been created by 4.a-generate-webapi-clients.yaml due to WebAPI version update. diff --git a/.github/workflows/4.a.1-generate-webapi-client-java.yaml b/.github/workflows/4.a.1-generate-webapi-client-java.yaml index c7b4ceead..08a24863a 100644 --- a/.github/workflows/4.a.1-generate-webapi-client-java.yaml +++ b/.github/workflows/4.a.1-generate-webapi-client-java.yaml @@ -54,31 +54,3 @@ jobs: with: name: javadoc path: work/target/apidocs - - push-javadoc: - needs: generate-java-client - concurrency: push-to-doc-website - runs-on: ubuntu-latest - env: - DOCPATH: casdk-docs/static/client-apidocs/${{ inputs.apiver }}/java - permissions: - contents: write - steps: - - name: Checkout - uses: actions/checkout@v4 - - name: Prepare - run: mkdir -p ${{ env.DOCPATH }} - - name: Download Javadoc - uses: actions/download-artifact@v4 - with: - name: javadoc - path: ${{ env.DOCPATH }} - - name: Push Javadoc - run: | - git config --global user.name "github-actions[bot]" - git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" - git add ${{ env.DOCPATH }} - git commit -m "Add Javadoc for WebAPI ${{ inputs.apiver }}" - git push origin ${{ github.ref_name }} - env: - GITHUB_TOKEN: ${{ github.token }} diff --git a/.github/workflows/4.a.1-generate-webapi-client-npm.yaml b/.github/workflows/4.a.1-generate-webapi-client-npm.yaml new file mode 100644 index 000000000..e456f4b05 --- /dev/null +++ b/.github/workflows/4.a.1-generate-webapi-client-npm.yaml @@ -0,0 +1,65 @@ +name: 4.a.1-Generate WebAPI client library for NPM + +on: + workflow_call: + inputs: + image: + required: true + type: string + apiver: + required: true + type: string + +jobs: + generate-npm-client: + runs-on: ubuntu-latest + services: + webapi: + image: ${{ inputs.image }} + ports: + - 8080:8080 + options: >- + --health-cmd "curl -sS http://localhost:8080/health" + --health-interval 3s + --health-timeout 5s + --health-retries 5 + permissions: + packages: write + env: + API: http://localhost:8080/api/v1/swagger.yaml + steps: + - name: Setup Node.js 22 + uses: actions/setup-node@v4 + with: + node-version: 22.x + registry-url: https://npm.pkg.github.com + - name: Prepare + run: | + mkdir work + npm install -g @openapitools/openapi-generator-cli@2.5.2 + - name: Generate client library + run: | + echo -n '{"npmName": "@${{ github.repository_owner }}/casdk-client", "npmVersion": "${{ inputs.apiver }}", "licenseName": "MIT", "gitUserId": "${{ github.repository_owner }}", "gitRepoId": "' > config.json + echo -n "${{ github.repository }}" | sed -e 's|^.\+/||' >> config.json + echo -n '", "npmRepository": "https://npm.pkg.github.com"}' >> config.json + openapi-generator-cli generate -i ${{ env.API }} -g typescript-axios -o work -c config.json + shell: bash + - name: Publish NPM library + working-directory: work + run: | + npm install + npm publish + env: + NODE_AUTH_TOKEN: ${{ github.token }} + - name: Setup TypeDoc + run: npm install -g typedoc + - name: Generate documents + working-directory: work + run: | + echo '{"out": "docs", "excludePrivate": true, "excludeProtected": true, "excludeExternals": true, "includeVersion": true}' > ../typedoc.json + npx typedoc --options ../typedoc.json --entryPoints index.ts + - name: Upload TypeDoc + uses: actions/upload-artifact@v4 + with: + name: typedoc + path: docs diff --git a/.gitignore b/.gitignore index b941bd543..61d62e144 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,5 @@ src/data/location-sources/custom-azure-zones.json # exclude artifacts on java-client example samples/java-client/target -casdk-docs/docs/samples/* \ No newline at end of file +casdk-docs/docs/samples/* +node_modules/ diff --git a/casdk-docs/sidebars.js b/casdk-docs/sidebars.js index 8365a9bb4..8d05e2c41 100644 --- a/casdk-docs/sidebars.js +++ b/casdk-docs/sidebars.js @@ -14,20 +14,34 @@ /** @type {import('@docusaurus/plugin-content-docs').SidebarsConfig} */ const sidebars = { // By default, Docusaurus generates a sidebar from the docs folder structure - tutorialSidebar: [{type: 'autogenerated', dirName: '.'}], - - // But you can create a sidebar manually - /* tutorialSidebar: [ - 'intro', - 'hello', { - type: 'category', - label: 'Tutorial', - items: ['tutorial-basics/create-a-document'], + type: 'autogenerated', + dirName: '.' }, - ], - */ + { + type: 'category', + label: 'API references for WebAPI client libraries', + items: [ + { + type: 'category', + label: 'WebAPI 1.0.0', + items: [ + { + type: 'link', + label: 'Java', + href: 'https://carbon-aware-sdk.greensoftware.foundation/client-apidocs/1.0.0/java/' + }, + { + type: 'link', + label: 'NPM', + href: 'https://carbon-aware-sdk.greensoftware.foundation/client-apidocs/1.0.0/npm/' + } + ] + }, + ] + } + ] }; module.exports = sidebars; diff --git a/samples/npm-client/.npmrc b/samples/npm-client/.npmrc new file mode 100644 index 000000000..5e51c6d94 --- /dev/null +++ b/samples/npm-client/.npmrc @@ -0,0 +1,2 @@ +//npm.pkg.github.com/:_authToken=${GH_TOKEN} +@Green-Software-Foundation:registry=https://npm.pkg.github.com diff --git a/samples/npm-client/README.md b/samples/npm-client/README.md new file mode 100644 index 000000000..f885ac5a2 --- /dev/null +++ b/samples/npm-client/README.md @@ -0,0 +1,66 @@ +# NPM Client Example + +This folder contains an example for WebAPI client in NPM. Client library would be pulled from [GitHub Packages](https://github.com/orgs/Green-Software-Foundation/packages?repo_name=carbon-aware-sdk). + +TypeDoc is [here](https://carbon-aware-sdk.greensoftware.foundation/client-apidocs/1.0.0/npm). + +## Requirements + +- WebAPI instance + - See the [Overview](../../docs/overview.md#publish-webapi-with-container) if you'd like to start it on container. +- Node.js 18 or later + +## Client code + +[index.js](index.js) is an example program to call WebAPI endpoint. It calls all of endpoints, and shows the result. + +Following methods are available: + +- `processBestEmissionsDataByLocations` + - Call /emissions/bylocations/best + - Gather the best emission data for west/central/east US yesterday. +- `processEmissionsDataByLocations` + - Call /emissions/bylocations + - Gather emission data for west/central/east US yesterday. +- `processEmissionsDataByLocation` + - Call /emissions/bylocation + - Gather emission data for westus yesterday. +- `processCurrentForecastData` + - Call /emissions/forecasts/current + - Gather forecast data for westus. +- `processForecastBatchData` + - Call /emissions/forecasts/batch + - Gather forecast data for westus. +- `processAverageData` + - Call /emissions/average-carbon-intensity . + - Gather average data for westus yesterday. +- `processAverageBatchData` + - Call /emissions/average-carbon-intensity/batch + - Gather average data for westus yesterday. + +## How it works + +### 1. Set WebAPI endpoint + +You need to set base URL of WebAPI endpoint in `index.ts`. `http://localhost:8080` is set by default. + +```typescript +const conf = new Configuration({basePath: 'http://localhost:8080'}); +``` + +### 2. Install required packages + +You have to run `npm install` at first. CASDK client module would be downloaded from GitHub Packages, so you need to set GitHub Parsonal Access Token. You can set it to `GH_TOKEN` environment variable used in [.npmrc](.npmrc). + +If you have not yet got GitHub PAT, see [this guide](https://docs.github.com/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens). + + +```sh +GH_TOKEN= npm install +``` + +### 3. Run + +```sh +npx ts-node index.ts +``` diff --git a/samples/npm-client/index.ts b/samples/npm-client/index.ts new file mode 100644 index 000000000..0fd64b647 --- /dev/null +++ b/samples/npm-client/index.ts @@ -0,0 +1,157 @@ +import { CarbonAwareApi, CarbonIntensityBatchParametersDTO, Configuration, EmissionsForecastBatchParametersDTO } from '@Green-Software-Foundation/casdk-client'; + +const conf = new Configuration({basePath: 'http://localhost:8080'}); +const caApi = new CarbonAwareApi(conf); + + +/** + * Call /emissions/bylocations/best . + * Returns the best emission data for west/central/east US yesterday as Promise. + */ +const processBestEmissionsDataByLocations = async() => { + const time = new Date(); + time.setUTCDate(time.getUTCDate() - 1); + time.setUTCHours(0, 0, 0, 0); + const toTime = new Date(time.getTime()); + toTime.setUTCHours(23, 59, 59, 999); + + return caApi.getBestEmissionsDataForLocationsByTime(['westus', 'centralus', 'eastus'], time.toISOString(), toTime.toISOString()) + .then((response) => response.data); +}; + +/** + * Call /emissions/bylocations . + * Returns emission data for west/central/east US yesterday as Promise. + */ +const processEmissionsDataByLocations = async() => { + const time = new Date(); + time.setUTCDate(time.getUTCDate() - 1); + time.setUTCHours(0, 0, 0, 0); + const toTime = new Date(time.getTime()); + toTime.setUTCHours(23, 59, 59, 999); + + return caApi.getEmissionsDataForLocationsByTime(['westus', 'centralus', 'eastus'], time.toISOString(), toTime.toISOString()) + .then((response) => response.data); +}; + +/** + * Call /emissions/bylocation . + * Returns emission data for westus yesterday as Promise. + */ +const processEmissionsDataByLocation = async() => { + const time = new Date(); + time.setUTCDate(time.getUTCDate() - 1); + time.setUTCHours(0, 0, 0, 0); + const toTime = new Date(time.getTime()); + toTime.setUTCHours(23, 59, 59, 999); + + return caApi.getEmissionsDataForLocationByTime('westus', time.toISOString(), toTime.toISOString()) + .then((response) => response.data); +}; + +/** + * Call /emissions/forecasts/current . + * Returns forecast data for westus as Promise. + */ +const processCurrentForecastData = async() => { + const startTime = new Date(); + startTime.setUTCHours(startTime.getUTCHours() + 1); + startTime.setUTCSeconds(0); + startTime.setUTCMilliseconds(0); + const endTime = new Date(startTime.getTime()); + endTime.setUTCHours(endTime.getUTCHours() + 1); + + return caApi.getCurrentForecastData(['westus'], startTime.toISOString(), endTime.toISOString()) + .then((response) => response.data); +}; + +/** + * Call /emissions/forecasts/batch . + * Returns forecast data for westus as Promise. + */ +const processForecastBatchData = async() => { + const requestTime = new Date(); + requestTime.setUTCMilliseconds(0); + const startTime = new Date(requestTime.getTime()); + startTime.setUTCMinutes(requestTime.getUTCMinutes() + 10); + const endTime = new Date(startTime.getTime()); + endTime.setUTCMinutes(startTime.getUTCMinutes() + 10); + const dto: EmissionsForecastBatchParametersDTO = { + location: 'westus', + requestedAt: requestTime.toISOString(), + dataStartAt: startTime.toISOString(), + dataEndAt: endTime.toISOString(), + windowSize: 10, + }; + + return caApi.batchForecastDataAsync([dto]) + .then((response) => response.data); +}; + +/** + * Call /emissions/average-carbon-intensity . + * Returns average data for westus yesterday as Promise. + */ +const processAverageData = async() => { + const startTime = new Date(); + startTime.setUTCDate(startTime.getUTCDate() - 1); + startTime.setUTCHours(0, 0, 0, 0); + const endTime = new Date(startTime.getTime()); + endTime.setUTCHours(23, 59, 59, 999); + + return caApi.getAverageCarbonIntensity('westus', startTime.toISOString(), endTime.toISOString()) + .then((response) => response.data); +}; + +/** + * Call /emissions/average-carbon-intensity/batch . + * Returns average data for westus yesterday as Promise. + */ +const processAverageBatchData = async() => { + const startTime = new Date(); + startTime.setUTCDate(startTime.getUTCDate() - 1); + startTime.setUTCHours(0, 0, 0, 0); + const endTime = new Date(startTime.getTime()); + endTime.setUTCHours(23, 59, 59, 999); + const dto: CarbonIntensityBatchParametersDTO = { + location: 'westus', + startTime: startTime.toISOString(), + endTime: endTime.toISOString(), + }; + + return caApi.getAverageCarbonIntensityBatch([dto]) + .then((response) => response.data); +}; + + +async function runAll(){ + console.log('--- /emissions/bylocations/best ---'); + console.log(await processBestEmissionsDataByLocations()); + console.log(); + + console.log('--- /emissions/bylocations ---'); + console.log(await processEmissionsDataByLocations()); + console.log(); + + console.log('--- /emissions/bylocation ---'); + console.log(await processEmissionsDataByLocation()); + console.log(); + + console.log('--- /emissions/forecasts/current ---'); + console.log(await processCurrentForecastData()); + console.log(); + + console.log('--- /emissions/forecasts/batch ---'); + console.log(await processForecastBatchData()); + console.log(); + + console.log('--- /emissions/average-carbon-intensity ---'); + console.log(await processAverageData()); + console.log(); + + console.log('--- /emissions/average-carbon-intensity/batch ---'); + console.log(await processAverageBatchData()); + console.log(); +} + +runAll(); diff --git a/samples/npm-client/package.json b/samples/npm-client/package.json new file mode 100644 index 000000000..bd3c3a5f5 --- /dev/null +++ b/samples/npm-client/package.json @@ -0,0 +1,9 @@ +{ + "devDependencies": { + "ts-node": "^10.9.2", + "typescript": "^5.6.3" + }, + "dependencies": { + "@Green-Software-Foundation/casdk-client": "1.0.0" + } +} diff --git a/samples/npm-client/tsconfig.json b/samples/npm-client/tsconfig.json new file mode 100644 index 000000000..56a8ab810 --- /dev/null +++ b/samples/npm-client/tsconfig.json @@ -0,0 +1,110 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig to read more about this file */ + + /* Projects */ + // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ + // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ + // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ + // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ + // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ + // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ + + /* Language and Environment */ + "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + // "jsx": "preserve", /* Specify what JSX code is generated. */ + // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ + // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ + // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ + // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ + // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ + // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ + // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ + // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ + // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ + + /* Modules */ + "module": "commonjs", /* Specify what module code is generated. */ + // "rootDir": "./", /* Specify the root folder within your source files. */ + // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ + // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ + // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ + // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ + // "types": [], /* Specify type package names to be included without being referenced in a source file. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ + // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */ + // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */ + // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */ + // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */ + // "noUncheckedSideEffectImports": true, /* Check side effect imports. */ + // "resolveJsonModule": true, /* Enable importing .json files. */ + // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ + // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ + + /* JavaScript Support */ + // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ + // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ + // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ + + /* Emit */ + // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + // "declarationMap": true, /* Create sourcemaps for d.ts files. */ + // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ + // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ + // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ + // "noEmit": true, /* Disable emitting files from a compilation. */ + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ + // "outDir": "./", /* Specify an output folder for all emitted files. */ + // "removeComments": true, /* Disable emitting comments. */ + // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ + // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ + // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ + // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ + // "newLine": "crlf", /* Set the newline character for emitting files. */ + // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ + // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ + // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ + // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ + // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + + /* Interop Constraints */ + // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ + // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */ + // "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */ + // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ + "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ + // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ + "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ + + /* Type Checking */ + "strict": true, /* Enable all strict type-checking options. */ + // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ + // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ + // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ + // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ + // "strictBuiltinIteratorReturn": true, /* Built-in iterators are instantiated with a 'TReturn' type of 'undefined' instead of 'any'. */ + // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ + // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ + // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ + // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ + // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ + // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ + // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ + // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ + // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ + // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ + // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ + + /* Completeness */ + // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ + } +} diff --git a/src/CarbonAware.WebApi/src/Configuration/ServiceCollectionExtensions.cs b/src/CarbonAware.WebApi/src/Configuration/ServiceCollectionExtensions.cs index f7f796903..2e260bd53 100644 --- a/src/CarbonAware.WebApi/src/Configuration/ServiceCollectionExtensions.cs +++ b/src/CarbonAware.WebApi/src/Configuration/ServiceCollectionExtensions.cs @@ -37,8 +37,8 @@ public static void AddMonitoringAndTelemetry(this IServiceCollection services, I var enableTelemetryLogging = envVars?.EnableTelemetryLogging ?? true; if (enableTelemetryLogging) { - const string serviceName = "CarbonAware.WebAPI"; - const string serviceVersion = "1.0.0"; + string serviceName = envVars?.WebAPISpecTitle ?? "CarbonAware.WebAPI"; + string serviceVersion = envVars?.WebAPISpecVersion ?? "1.0.0"; services.AddOpenTelemetry() .WithTracing(tracerProviderBuilder => diff --git a/src/CarbonAware.WebApi/src/Program.cs b/src/CarbonAware.WebApi/src/Program.cs index 8f99c8bb5..cbec718b8 100644 --- a/src/CarbonAware.WebApi/src/Program.cs +++ b/src/CarbonAware.WebApi/src/Program.cs @@ -9,6 +9,10 @@ using System.Reflection; var builder = WebApplication.CreateBuilder(args); +builder.Services.Configure(builder.Configuration.GetSection(CarbonAwareVariablesConfiguration.Key)); +CarbonAwareVariablesConfiguration config = new(); +builder.Configuration.GetSection(CarbonAwareVariablesConfiguration.Key).Bind(config); + // Add services to the container. builder.Services.AddControllers(options => @@ -28,15 +32,13 @@ c.EnableAnnotations(); c.OperationFilter(); c.SchemaFilter(); - c.SwaggerDoc("v1", new OpenApiInfo + c.SwaggerDoc(config.WebAPISpecName, new OpenApiInfo { - Version = "CarbonAware.WebAPI", - Title = "1.0.0", + Version = config.WebAPISpecVersion, + Title = config.WebAPISpecTitle, }); }); -builder.Services.Configure(builder.Configuration.GetSection(CarbonAwareVariablesConfiguration.Key)); - bool successfulServices = true; string? errorMessage = null; try @@ -49,10 +51,6 @@ errorMessage = e.Message; } -CarbonAwareVariablesConfiguration config = new(); - -builder.Configuration.GetSection(CarbonAwareVariablesConfiguration.Key).Bind(config); - builder.Services.AddHealthChecks(); builder.Services.AddMonitoringAndTelemetry(builder.Configuration); diff --git a/src/CarbonAware/src/CarbonAwareVariablesConfiguration.cs b/src/CarbonAware/src/CarbonAwareVariablesConfiguration.cs index fc0ba2da3..12b9743aa 100644 --- a/src/CarbonAware/src/CarbonAwareVariablesConfiguration.cs +++ b/src/CarbonAware/src/CarbonAwareVariablesConfiguration.cs @@ -42,4 +42,9 @@ internal class CarbonAwareVariablesConfiguration public Boolean VerboseApi {get; set;} + public string WebAPISpecName { get; } = "v1"; + + public string WebAPISpecVersion { get; } = "1.0.0"; + + public string WebAPISpecTitle { get; } = "CarbonAware.WebAPI"; }