C# Sonatype Nexus rest api通过C上传#

C# Sonatype Nexus rest api通过C上传#,c#,.net,nexus,C#,.net,Nexus,我需要通过C#将一个工件上传到Nexus,但我无法理解如何解释“cUrl-F”参数来告诉请求名称扩展以及它应该放在哪个存储库中。有人可以共享解决方案吗 以下是对我有用的东西: public async Task<HttpResponseMessage> UploadArtifact(string artifactPath, string artifactName, string artifactId) { using var systemClient = ne

我需要通过C#将一个工件上传到Nexus,但我无法理解如何解释“cUrl-F”参数来告诉请求名称扩展以及它应该放在哪个存储库中。有人可以共享解决方案吗

以下是对我有用的东西:

public async Task<HttpResponseMessage> UploadArtifact(string artifactPath, string artifactName, string artifactId)
    {
        using var systemClient = new HttpClient
        {
            DefaultRequestHeaders =
            {
                Authorization = new AuthenticationHeaderValue("Basic",
                    Convert.ToBase64String(Encoding.ASCII.GetBytes($"{_nexusUsername}:{_nexusPassword}")))
            }
        };
        var client = _httpClient ?? new SystemHttpClient(systemClient);

        var data = _file.ReadAllBytes(artifactPath);
        using var content = new ByteArrayContent(data);

        var postUrl = string.Format(CultureInfo.InvariantCulture, _settings.Value.NexusBaseUrl, artifactId, artifactName);
        return await client.PutAsync(new Uri(postUrl), content).ConfigureAwait(false);
    }
下面是包含所述方法的类的完整实现:

public interface INexusClient
{
    Task<HttpResponseMessage> UploadArtifact(string artifactPath, string artifactName, string artifactId);
}

public sealed class NexusClient : INexusClient
{
    private readonly IFile _file;
    private readonly IHttpClient _httpClient;
    private readonly string _nexusPassword;
    private readonly string _nexusUsername;
    private readonly IOptions<AppConfigurationSettings> _settings;

    public NexusClient(IOptions<AppConfigurationSettings> settings, IFile file, IHttpClient httpClient, IEnvironment environment)
    {
        _settings = settings ?? throw new InvalidOperationException($"{nameof(settings)} must not be null");
        _file = file;
        _httpClient = httpClient;

        if (environment == null) throw new InvalidOperationException($"{nameof(environment)} must not be null");
        _nexusUsername = environment.GetEnvironmentVariable("nexusUsername");
        _nexusPassword = environment.GetEnvironmentVariable("nexusPassword");
        if (string.IsNullOrEmpty(_nexusUsername)) throw new InvalidOperationException($"{nameof(_nexusUsername)} is not configured in this environment");
        if (string.IsNullOrEmpty(_nexusPassword)) throw new InvalidOperationException($"{nameof(_nexusPassword)} is not configured in this environment");
    }

    public async Task<HttpResponseMessage> UploadArtifact(string artifactPath, string artifactName, string artifactId)
    {
        using var systemClient = new HttpClient
        {
            DefaultRequestHeaders =
            {
                Authorization = new AuthenticationHeaderValue("Basic",
                    Convert.ToBase64String(Encoding.ASCII.GetBytes($"{_nexusUsername}:{_nexusPassword}")))
            }
        };
        var client = _httpClient ?? new SystemHttpClient(systemClient);

        var data = _file.ReadAllBytes(artifactPath);
        using var content = new ByteArrayContent(data);

        var postUrl = string.Format(CultureInfo.InvariantCulture, _settings.Value.NexusBaseUrl, artifactId, artifactName);
        return await client.PutAsync(new Uri(postUrl), content).ConfigureAwait(false);
    }
}
不使用客户端的公共接口
{
任务上载工件(字符串工件路径、字符串工件名称、字符串工件ID);
}
公共密封类NexusClient:非USCLIENT
{
私有只读IFile\u文件;
专用只读IHttpClient\u httpClient;
私有只读字符串_nexusPassword;
私有只读字符串_nexusUsername;
专用只读IOptions\u设置;
public NexusClient(IOPS设置、IFile文件、IHttpClient httpClient、IEnEnvironment环境)
{
_设置=设置??抛出新的InvalidOperationException($“{nameof(设置)}不能为空”);
_文件=文件;
_httpClient=httpClient;
if(environment==null)抛出新的invalidoOperationException($“{nameof(environment)}不能为null”);
_nexusUsername=environment.GetEnvironmentVariable(“nexusUsername”);
_nexusPassword=environment.GetEnvironmentVariable(“nexusPassword”);
如果(string.IsNullOrEmpty(_-nexusUsername))抛出新的InvalidOperationException($“{nameof(_-nexusUsername)}未在此环境中配置”);
如果(string.IsNullOrEmpty(_-nexusPassword))抛出新的InvalidOperationException($“{nameof(_-nexusPassword)}未在此环境中配置”);
}
公共异步任务上载工件(字符串工件路径、字符串工件名称、字符串工件ID)
{
使用var systemClient=newhttpclient
{
DefaultRequestHeader=
{
授权=新的AuthenticationHeaderValue(“基本”,
Convert.ToBase64String(Encoding.ASCII.GetBytes($“{nexusUsername}:{{nexusPassword}”))
}
};
var client=_httpClient??新系统httpClient(系统客户端);
var data=_file.ReadAllBytes(artifactPath);
使用var内容=新的ByteArrayContent(数据);
var postrl=string.Format(CultureInfo.InvariantCulture,_settings.Value.NexusBaseUrl,artifactId,artifactName);
返回await client.PutAsync(新Uri(postrl),content.ConfigureAwait(false);
}
}
*请注意,IFile、IHttpClient和IEnEnvironment是我分别用于文件、HttpClient和环境类的包装器接口(用于帮助测试)

public interface INexusClient
{
    Task<HttpResponseMessage> UploadArtifact(string artifactPath, string artifactName, string artifactId);
}

public sealed class NexusClient : INexusClient
{
    private readonly IFile _file;
    private readonly IHttpClient _httpClient;
    private readonly string _nexusPassword;
    private readonly string _nexusUsername;
    private readonly IOptions<AppConfigurationSettings> _settings;

    public NexusClient(IOptions<AppConfigurationSettings> settings, IFile file, IHttpClient httpClient, IEnvironment environment)
    {
        _settings = settings ?? throw new InvalidOperationException($"{nameof(settings)} must not be null");
        _file = file;
        _httpClient = httpClient;

        if (environment == null) throw new InvalidOperationException($"{nameof(environment)} must not be null");
        _nexusUsername = environment.GetEnvironmentVariable("nexusUsername");
        _nexusPassword = environment.GetEnvironmentVariable("nexusPassword");
        if (string.IsNullOrEmpty(_nexusUsername)) throw new InvalidOperationException($"{nameof(_nexusUsername)} is not configured in this environment");
        if (string.IsNullOrEmpty(_nexusPassword)) throw new InvalidOperationException($"{nameof(_nexusPassword)} is not configured in this environment");
    }

    public async Task<HttpResponseMessage> UploadArtifact(string artifactPath, string artifactName, string artifactId)
    {
        using var systemClient = new HttpClient
        {
            DefaultRequestHeaders =
            {
                Authorization = new AuthenticationHeaderValue("Basic",
                    Convert.ToBase64String(Encoding.ASCII.GetBytes($"{_nexusUsername}:{_nexusPassword}")))
            }
        };
        var client = _httpClient ?? new SystemHttpClient(systemClient);

        var data = _file.ReadAllBytes(artifactPath);
        using var content = new ByteArrayContent(data);

        var postUrl = string.Format(CultureInfo.InvariantCulture, _settings.Value.NexusBaseUrl, artifactId, artifactName);
        return await client.PutAsync(new Uri(postUrl), content).ConfigureAwait(false);
    }
}