1

Get an IFS OData projection

by
Published Nov 22, 2024

This script allows an operator or another script to get data from an IFS OData projection without needing to perform OAuth in the client.

Script ifs Verified

The script

Submitted by hugo989 Bun
Verified 6 days ago
1
import { OAuth2Client, OAuth2Fetch } from '@badgateway/oauth2-client';
2
import * as wmill from '[email protected]';
3

4
type CIfsDeployment = {
5
  server: string;
6
  clientId: string;
7
  oidcPath: string;
8
  clientSecret: string;
9
};
10

11
async function getConfiguration(deployment: string): Promise<CIfsDeployment> {
12
  const configuration = await wmill.getResource(`f/settings/ifs-${deployment}`);
13
  return configuration;
14
}
15

16
export async function main(deployment: string, projection: string, isEntity: boolean) {
17
  const config = await getConfiguration(deployment);
18

19
  const client = new OAuth2Client({
20
    server: config.server,
21
    discoveryEndpoint: config.oidcPath,
22
    clientId: config.clientId,
23
    clientSecret: config.clientSecret,
24
    authenticationMethod: 'client_secret_post',
25
  });
26

27
  const fetcher = new OAuth2Fetch({
28
    client,
29
    getNewToken: async () => {
30
      return client.clientCredentials();
31
    },
32
    onError: (error) => {
33
      console.error('error: ', error);
34
    },
35
  });
36

37
  try {
38
    const url = `${config.server}/main/ifsapplications/projection/v1/${projection}`;
39
    const response = await fetcher.fetch(url);
40
    const data = await response.json();
41
    return data;
42
  } catch (error) {
43
    console.error('error: ', error);
44
    return { error: error };
45
  }
46
}
Other submissions
  • Submitted by chris edgington162 Deno
    Created 398 days ago
    1
    import { OAuth2Client, OAuth2Fetch } from 'npm:@badgateway/oauth2-client';
    2
    import * as wmill from 'npm:[email protected]';
    3
    
    
    4
    type CIfsDeployment = {
    5
      server: string;
    6
      clientId: string;
    7
      oidcPath: string;
    8
      clientSecret: string;
    9
    };
    10
    
    
    11
    async function getConfiguration(deployment: string): Promise<CIfsDeployment> {
    12
      const configuration = await wmill.getResource(`f/settings/ifs-${deployment}`);
    13
      return configuration;
    14
    }
    15
    
    
    16
    export async function main(deployment: string, projection: string, isEntity: boolean) {
    17
      const config = await getConfiguration(deployment);
    18
    
    
    19
      const client = new OAuth2Client({
    20
        server: config.server,
    21
        discoveryEndpoint: config.oidcPath,
    22
        clientId: config.clientId,
    23
        clientSecret: config.clientSecret,
    24
        authenticationMethod: 'client_secret_post',
    25
      });
    26
    
    
    27
      const fetcher = new OAuth2Fetch({
    28
        client,
    29
        getNewToken: async () => {
    30
          return client.clientCredentials();
    31
        },
    32
        onError: (error) => {
    33
          console.error('error: ', error);
    34
        },
    35
      });
    36
    
    
    37
      try {
    38
        const url = `${config.server}/main/ifsapplications/projection/v1/${projection}`;
    39
        const response = await fetcher.fetch(url);
    40
        const data = await response.json();
    41
        return data;
    42
      } catch (error) {
    43
        console.error('error: ', error);
    44
        return { error: error };
    45
      }
    46
    }