簡體   English   中英

如何使用 C# 向 REST api 添加參數?

[英]How to add parameters to a REST api using C#?

我正在嘗試將 restsharp 與我的 api 測試一起使用。 我需要幫助我的下面的代碼,以使其正確。 我正在嘗試通過在不同的測試中添加參數來做出響應。 我嘗試了不同的方法,但我完全是編程的初學者。

OrdersGate:

public class Authenticate
{
    public string userLogin { get; set; }
    public string authenticateKey { get; set; }
}

public class Client
{
    public string clientLogin { get; set; }
    public string clientFirstName { get; set; }
    public string clientLastName { get; set; }
    public string clientCity { get; set; }
    public string clientEmail { get; set; }
    public string clientHasTaxNumber { get; set; }
    public string clientSearchingMode { get; set; }
    public string clientFirm { get; set; }
    public string clientCountryId { get; set; }
    public string clientCountryName { get; set; }
}

public class OrdersDateRange
{
    public string ordersDateType { get; set; }
    public List<string> ordersDatesTypes { get; set; }
    public string ordersDateBegin { get; set; }
    public string ordersDateEnd { get; set; }
}

public class OrdersSerialNumberRange
{
    public int ordersSerialNumberBegin { get; set; }
    public int ordersSerialNumberEnd { get; set; }
}

public class OrdersRange
{
    public OrdersDateRange ordersDateRange { get; set; }
    public OrdersSerialNumberRange ordersSerialNumberRange { get; set; }
}

public class AuctionsAccount
{
    public int auctionsAccountId { get; set; }
    public string auctionsAccountLogin { get; set; }
}

public class AuctionsClient
{
    public string auctionClientId { get; set; }
    public string auctionClientLogin { get; set; }
}

public class AuctionsParams
{
    public List<string> auctionsServicesNames { get; set; }
    public List<int> auctionsItemsIds { get; set; }
    public List<AuctionsAccount> auctionsAccounts { get; set; }
    public List<AuctionsClient> auctionsClients { get; set; }
}

public class OrderSource
{
    public int shopsMask { get; set; }
    public List<int> shopsIds { get; set; }
    public AuctionsParams auctionsParams { get; set; }
}

public class Product
{
    public int productId { get; set; }
    public string productName { get; set; }
    public string sizeId { get; set; }
    public string sizePanelName { get; set; }
}

public class Packages
{
    public List<string> packagesNumbers { get; set; }
    public string orderHasPackageNumbers { get; set; }
}

public class Stock
{
    public int stockId { get; set; }
}

public class Campaign
{
    public int campaignId { get; set; }
    public List<string> discountCodes { get; set; }
}

public class OrdersBy
{
    public string elementName { get; set; }
    public string sortDirection { get; set; }
}

public class OrdersGet
{
    public string orderPrepaidStatus { get; set; }
    public List<string> ordersStatuses { get; set; }
    public List<string> couriersName { get; set; }
    public string orderPaymentType { get; set; }
    public List<string> withMissingSalesDocuments { get; set; }
    public string orderType { get; set; }
    public string dropshippingOrderStatus { get; set; }
    public List<string> ordersIds { get; set; }
    public List<int> ordersSerialNumbers { get; set; }
    public List<Client> clients { get; set; }
    public OrdersRange ordersRange { get; set; }
    public OrderSource orderSource { get; set; }
    public List<Product> products { get; set; }
    public int resultsPage { get; set; }
    public int resultsLimit { get; set; }
    public string clientRequestInvoice { get; set; }
    public Packages packages { get; set; }
    public List<Stock> stocks { get; set; }
    public Campaign campaign { get; set; }
    public string loyaltyPointsMode { get; set; }
    public string orderOperatorLogin { get; set; }
    public string orderPackingPersonLogin { get; set; }
    public List<OrdersBy> ordersBy { get; set; }
    public string searchingOperatorTypeMatch { get; set; }
    public string ordersDelayed { get; set; }
}

public class RootOrdersGet
{
    public Authenticate authenticate { get; set; }
    public OrdersGet @params { get; set; }
}

My code:

[TestFixture]
public class Get
{
    private static object[] DataOrdersGet =
    {
        new object[] { GlobalVariables.login, Modules.GenerateKey(Modules.HashPassword(GlobalVariables.pass)), "unpaid"},
        new object[] { GlobalVariables.login, Modules.GenerateKey(Modules.HashPassword(GlobalVariables.pass)), "restored"},
        new object[] { GlobalVariables.login, Modules.GenerateKey(Modules.HashPassword(GlobalVariables.pass)), "waiting"},
    };

    [OneTimeSetUp]
    public void BeforeAnyTests() { }

    [Test, TestCaseSource("DataOrdersGet")]
    public void SprawdzZamowieniaODanymStatusie(string login, string pass, string orderPrepaidStatus)
    {
        string vGate = "100";
        RestClient restClient = new RestClient(GlobalVariables.url + "api/?gate=orders/get/" + vGate + "/json");


        RestRequest restRequest = new RestRequest(Method.POST);

        var Authenticate = new Dictionary<string, string>
            {
                { "userLogin", login },
                { "authenticateKey", pass }
            };
        var Param = new Dictionary<string, string>
            {
                {"orderPrepaidStatus", orderPrepaidStatus}
            };

        var body = new
        {
            authenticate = Authenticate,
            @params = Param

        };

        restRequest.AddJsonBody(body);
        restRequest.AddParameter("application/json", body, ParameterType.RequestBody);

        IRestResponse restResponse = restClient.Execute(restRequest);

        Console.WriteLine("Status: " + restResponse.StatusCode);
        Console.WriteLine("\nResponse: " + restResponse.Content);
        Console.WriteLine("\nRequest: " + body.ToString());
    }

    [OneTimeTearDown]
    protected void CloseAfterAllTests() { }

現在,我正在通過字典添加參數。 沒有它如何讓它變得更好? 我知道有類似restRequest.AddParameter()東西。 但我不知道如何使用它,與我的門。

您可以嘗試這樣做:

var body = new { 
    authenticate = new Authenticate() { 
        userLogin = login, 
        authenticateKey = pass} 
    },
    @params = new OrdersGet() {
        orderPrepaidStatus = orderPrepaidStatus
    }
}

// Adds the entire object to the request
// after serializing it and making it json
restRequest.AddJsonBody(body);

// Executes the request with the proper payload
IRestResponse restResponse = restClient.Execute(restRequest);

Console.WriteLine("Status: " + restResponse.StatusCode);
Console.WriteLine("\nResponse: " + restResponse.Content);
Console.WriteLine("\nRequest: " + body.ToString());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM