【问题标题】:How to dynamically inject REST API params?如何动态注入 REST API 参数?
【发布时间】:2018-06-26 14:11:33
【问题描述】:

我正在处理 nodejs 项目,我需要调用几个 ms 图形 api 来获取一些数据。一些示例调用如下

https://graph.microsoft.com/v1.0/users/{userPrincipalName}/manager
https://graph.microsoft.com/beta/users/{userPrincipalName}/notebooks
https://graph.microsoft.com/v1.0/users/{userPrincipalName}/photo/

我的目标是不要每次都使用请求模块来调用以上 3 个 api 并分别获取数据。我想创建某种辅助函数,我可以在其中传递我需要使用的 URL 以及 {userPrincipalName} 是什么

所以我创建了 graphendpoints.js 来存储所有 url 的

module.exports = {
    manager: 'https://graph.microsoft.com/v1.0/users/userPrincipalName/manager',
    notebooks: 'https://graph.microsoft.com/beta/users/{userPrincipalName}/notebooks',
photo: 'https://graph.microsoft.com/v1.0/users/{userPrincipalName}/photo/'
  };

这就是我通过身份验证令牌和网址通过单个函数调用图形的方式。我没有每个单独的方法来执行对经理、笔记本或照片等的请求。

executeGraphApi: function (token, url) {
        return new Promise(function (resolve, reject) {
            let parsedBody = []
            request.get({
                    url: url,
                    headers: {
                        authorization: 'Bearer ' + token
                    }
                },
                function (err, response, body) {
                    if (err) {
                        reject(err)
                    } else {
                        parsedBody.push(JSON.parse(body).displayName)
                        resolve(parsedBody)
                    }
                })
        });
    }

帮助函数runMSGraphApi(url)调用上面的executeGraphApi(token,url)

var graphcallrunner = require('../../app/msgraph/graphcallrunner');
runMSGraphApi: function (url) {
        return new Promise(function (resolve,reject) {
            msGraph.getAccessToken()
                .then(function (token) {
                    graphcallrunner.executeGraphApi(token,url)
                        .then(function (results) {
                            resolve(results)
                        })
                        .catch(function (error) {
                            reject(error)
                        })
                })
                .catch(function (error) {
                    console.log(error);
                    reject(error)
                })
        })
    }

这个的消费者如下所示。我需要确保想要调用 ms graph 的人只需使用 runMSGraphApi() 并传入正确的 url。

var graphendpoints = require('../../../msgraph/graphendpoints.js');
graphapihelper.runMSGraphApi(graphendpoints.manager),

如果您在上面看到,我从 graphendpoints.js 中选择了 manager api 调用并传入了该 url。这个网址其实是 https://graph.microsoft.com/v1.0/users/{userPrincipalName}/manager

其中 {userPrincipalName} 未注入。如何将 {userPrincipalName} 的值动态传递给 executeGraphApi() 中的最终 url 构造。

https://graph.microsoft.com/v1.0/users/bob.bar@google.com/manager

【问题讨论】:

标签: node.js express microsoft-graph-api node-modules


【解决方案1】:

您可以使用正则表达式来做到这一点

const endpoints = {
  manager: 'https://graph.microsoft.com/v1.0/users/userPrincipalName/manager',
  notebooks: 'https://graph.microsoft.com/beta/users/{userPrincipalName}/notebooks',
  photo: 'https://graph.microsoft.com/v1.0/users/{userPrincipalName}/photo/'
}

function parseUrl(url, params) {
  let result = url
  const regex = /(?<={)(.*)(?=})/
  const match = url.match(regex)

  if (match) {
    const replace = match[0]
    const value = params[replace]
    const holeThing = /{(?<={)(.*)(?=})}/
    if (value) {
      result = result.replace(holeThing, value);
    } else {
      throw new Error('Please specify the value for parsing the URL')
    }
  }
  return result
}

const url = parseUrl(endpoints.notebooks, {
  userPrincipalName: 'woof'
})

console.log(url)

【讨论】:

  • 对不起,我没有得到你。当消费者尝试像下面这样使用它时,graphendpoints.manager 将是 graph.microsoft.com/v1.0/users{userPrincipalName}/manager graphapihelper.runMSGraphApi(graphendpoints.manager), 的字段,我需要通过 bob.bar@google.com 传入或填写 {userPrincipalName}飞。
猜你喜欢
  • 2017-11-16
  • 1970-01-01
  • 2019-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多