简体   繁体   English

如何定义可在所有API中使用的基本网址

[英]How do I define a base url which can be used in all the APIs

I have a set of APIs in my automation test which uses the request package. 我的自动化测试中有一组使用请求包的API。 I want to have a common base URL so instead of changing the URL every time in all the APIs I can just change it in one place which will be used everywhere. 我希望有一个通用的基本URL,因此不必每次都在所有API中都更改URL,而是可以在一个地方更改它,以便在任何地方使用。

I have tried using the $id with $ref but I cannot seem to get it working. 我尝试将$id$ref一起使用,但似乎无法正常工作。 Any suggestion would be really appreciated as what is the best way to do it in a JSON file. 任何建议都是真的,因为在JSON文件中做到这一点的最佳方法是什么。

These are a few APIs that I have: 这些是我拥有的一些API:

"create_api": https://url/receive/Test0/text?channel=Viber&sender.id=95
"attachment_api": "url/receive/Test0/attachment?channel=Viber&sender.id=95",
"sendmessage_api": "https://url/um/receive/Test0/text?channel=Viber&sender.id=95&text=New Message",

Instead of specifying the URL over and over again I want to specify it globally in the JSON file. 我不想一遍又一遍地指定URL,而是要在JSON文件中全局指定它。

There can be of many different approaches. 可以有许多不同的方法。 The one which I follow is 我跟随的是

var configuration = {
    "api": {
            "base": {
                "development": "http://localhost/",
                "test": "http://testserver.com/",
                "production": "productionserver.com/"
            },
            "login": "login/",
            "search": "questions/search/"
        }
}

and the way we can retrieve is 我们可以检索的方式是

var environment = "development";
var loginurl = configuration.api.base[environment] + configuration.api.login;
var searchurl = configuration.api.base[environment] + configuration.api.search;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM