我在代码中使用了这个REST API端点
public static void Foo(string organization, string project, string buildId, string artifactName, IWorkerInstrumentation instrumentation)
{
string artifactUrl = $"https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/artifacts?artifactName={artifactName}&api-version=4.1";
// Further code that calls the above endpoint
// Irrelevant to the discussion
}
并收到如下建议
这些字符串常量和专有URL模式应该隔离 放在中心位置,这样就可以轻松更改它们。
我想了一下,觉得这个URL是根据传递给此函数的参数动态生成的,因此不能在常量文件中包含整个URL。
我能想到的最接近的方法是具有一个函数,该函数根据传入的参数生成此URL,并使URL的某些词干部分来自常量文件,如下所示。
助手类
public static class AzureDevOpsAPIHelper
{
public static string GenerateArtifactDownloadUrl(string organization, string project, string buildId, string artifactName)
{
return AzureDevOpsConstants.AzureDevOpsApiBaseUrl + "/" + organization + "/" + project + AzureDevOpsConstants.AzureDevOpsBuildsSubUrl + "/" + buildId + "/" + AzureDevOpsConstants.AzureDevOpsArtifactsSubUrl + artifactName + AzureDevOpsConstants.AzureDevOpsArtifactsApiVersion;
}
}
常量文件
public static class AzureDevOpsConstants
{
/**
* The constants used by the Azure Dev Ops API helper to generate REST API endpoints to talk to Azure Dev Ops
*/
public static readonly string AzureDevOpsApiBaseUrl = "https://dev.azure.com/";
public static readonly string AzureDevOpsBuildsSubUrl = "_apis/build/builds";
public static readonly string AzureDevOpsArtifactsSubUrl = "artifacts?artifactName=";
public static readonly string AzureDevOpsArtifactsApiVersion = "&api-version=4.1";
}
原始API文件
string artifactUrl = AzureDevOpsAPIHelper.GenerateArtifactDownloadUrl(organization, project, buildId, artifactName);
我不确定这有多有用。有人可以建议一种替代/更正确的方法来解决我在上面发表的评论。