【问题标题】:loopback4 rest connector multiple callsloopback4 休息连接器多次调用
【发布时间】:2020-05-11 20:34:39
【问题描述】:

我在 Loopback4 中使用 Rest 连接器,但您将如何扩展此数据源,以便它可以选择调用不止 1 个 url 并具有不止 1 个函数?

这是我的 ic.datasource.config.json 文件:

{
  "name": "ic",
  "connector": "rest",
  "debug": "false",
  "options": {
    "headers": {
      "accept": "application/json",
      "content-type": "application/json",
      "authorization": "Basic 64 Encoded credentials"
    }
  },
  "operations": [{
    "template": {
      "method": "GET",
      "url": "https://webapi.ic.com/customers/71/jobs/{id}    
    },
    "functions": {
      "getDetails": ["id"]
    }
  }]
}

这是我的 ic.service.ts 文件:

import { getService, juggler } from '@loopback/service-proxy';
import { inject, Provider } from '@loopback/core';
import { icDataSource } from '../datasources/ic.datasource';

export interface icResponseData {
  id: string;
  title: string;
  manager: string;
  overview: string;
}

export interface icService {
  getDetails(id?: number): Promise<icResponseData>;
}

export class icProvider implements Provider<icService> {
  constructor(
  // ic must match the name property in the datasource json file
  @inject('datasources.ic')
  protected dataSource: juggler.DataSource = new icDataSource(),
 ) {}

 value(): Promise<icService> {
   return getService(this.dataSource);
 }
}

【问题讨论】:

    标签: loopback4


    【解决方案1】:

    我想通了。

    这是我的 ic.datasource.config.json 文件:

    {
      "name": "ic",
      "connector": "rest",
      "debug": "false",
      "options": {
        "headers": {
          "accept": "application/json",
          "content-type": "application/json",
          "authorization": "Basic 64 Encoded credentials"
        }
      },
      "operations": [{
        "template": {
          "method": "GET",
          "url": "https://webapi.ic.com/customers/71/search/{id}    
        },
        "functions": {
          "getAll": ["id"]
        }
      },
        {
          "template": {
            "method": "GET",
            "url": "https://webapi.ics.com/customers/71/jobs/{id}"
          },
          "functions": {
            "getJobDetails": ["id"]
          }
        }]
    }
    

    这是我的 ic.service.ts 文件:

    import { getService, juggler } from '@loopback/service-proxy';
    import { inject, Provider } from '@loopback/core';
    import { icDataSource } from '../datasources/ic.datasource';
    
    export interface icResponseData {
      id: string;
      title: string;
      manager: string;
      overview: string;
    }
    
    export interface icimsJobIDsResponseData {
      portalUrl: string;
      id: number;
      updateDate: string;
    }
    
    export interface icimsService {
      getAll(portalid?: number): Promise<icAllResponseData>;
    }
    
    export interface icService {
      getDetails(id?: number): Promise<icResponseData>;
    }
    
    export class icProvider implements Provider<icService> {
      constructor(
      // ic must match the name property in the datasource json file
      @inject('datasources.ic')
      protected dataSource: juggler.DataSource = new icDataSource(),
     ) {}
    
     value(): Promise<icService> {
       return getService(this.dataSource);
     }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-07-25
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 2023-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多