Przestrzeń nazw WebServiceRunner
Podsumowanie:W akcji algorytm C# można korzystać z dwóch klas z przestrzeni nazw CSharpScript.WebServiceRunner, które umożliwiają wykonywanie zapytań do webserwisów:
- CSharpScript.WebServiceRunner.RestManager: służy dla wykonania zapytania REST
- CSharpScript.WebServiceRunner.SoapManager: służy dla wykonania zapytania SOAP
Aby wykonać żądanie do webserwisu, należy użyć metody ExecuteRequestAsync dla instancji odpowiedniej klasy. Na przykład:
await CSharpScript.WebServiceRunner.RestManager.Instance.ExecuteRequestAsync(model, cancellationToken);
await CSharpScript.WebServiceRunner.SoapManager.Instance.ExecuteRequestAsync(model, cancellationToken);
Metoda ExecuteRequestAsync
public Task<IResponse> ExecuteRequestAsync(BaseRequestModel model, CancellationToken cancellationToken = default)
Parametry:
- Model: model żądania; w zależności od typu żądania — RestRequestModel albo SoapRequestModel
- CancellationToken: parametr nieobowiązkowy; propaguje powiadomienie, że operacja powinna zostać anulowana.
Wynik:
Asynchroniczna operacja o typie IResponse:
public interface IResponse
{
/// <summary>
/// Contains the values of status codes returned from HTTP request
/// Type: System.Net.HttpStatusCode
/// </summary>
HttpStatusCode ResponseStatusCode { get; }
/// <summary>
/// Contains information whether the request was successful
/// </summary>
bool IsSuccess { get; }
/// <summary>
/// Result of HTTP request
/// In case of WebException will contains exception message
/// </summary>
string RawResult { get; }
}
Klasa RestManager
Sposób wywołania:
var model = new CSharpScript.WebServiceRunner.Models.RestRequestModel();
var result = await CSharpScript.WebServiceRunner.RestManager.Instance.ExecuteRequestAsync(model, cancellationToken);
Dla klasy RestManager metoda ExecuteRequestAsync przyjmuje jako model klasę o nazwie RestRequestModel.
public class RestRequestModel
{
/// <summary>
/// HTTP method to call (System.Net.Http.HttpMethod)
/// Allowed values are Get | Post | Delete | Put | Patch
/// </summary>
public HttpMethod Method { get; set; }
/// <summary>
/// Request headers
/// </summary>
public Dictionary<string, string> Headers { get; set; }
/// <summary>
/// Web service URL
/// </summary>
public string Url { get; set; }
/// <summary>
/// Request body to send
/// REST - used when HttpMethod is set to POST | PUT | Patch
/// </summary>
public string Body { get; set; }
/// <summary>
/// Authentication type
/// Default value is AuthType.NoAuth
/// ApiConnector.Enums.AuthType (NoAuth | Base64 | JWT | OAuth2Password | OAuth2ClientCredentials)
/// In REST Manager could be used with Base64 | JWT | OAuth2Password | OAuth2ClientCredentials
/// </summary>
public AuthType AuthType { get; set; } = AuthType.NoAuth;
/// <summary>
/// URL to authentication service
/// Could be null or empty when AuthType is set to Base64
/// Is required when AuthType is set to JWT | OAuth2Password | OAuth2ClientCredentials
/// </summary>
public string AuthUrl { get; set; }
/// <summary>
/// Username for authentication purpose
/// REST - is used when AuthType is set to Base64 | JWT | OAuth2Password
/// </summary>
public string UserName { get; set; }
/// <summary>
/// Password for authentication purpose
/// REST - is used when AuthType is set to Base64 | JWT | OAuth2Password
/// </summary>
public string Password { get; set; }
/// <summary>
/// Authentication scope
/// Used when AuthType is set to OAuth2Password | OAuth2ClientCredentials
/// </summary>
public string Scope { get; set; }
/// <summary>
/// Authentication client id
/// Used when AuthType is set to OAuth2Password | OAuth2ClientCredentials
/// </summary>
public string ClientId { get; set; }
/// <summary>
/// Authentication client secret
/// Used when AuthType is set to OAuth2Password | OAuth2ClientCredentials
/// </summary>
public string ClientSecret { get; set; }
}
Klasa SoapManager
Sposób wywołania:
var model = new CSharpScript.WebServiceRunner.Models.SoapRequestModel();
var result = await CSharpScript.WebServiceRunner.SoapManager.Instance.ExecuteRequestAsync(model);
Dla klasy SoapManager metoda ExecuteRequestAsync przyjmuje jako model klasę o nazwie SoapRequestModel.
public class SoapRequestModel
{
/// <summary>
/// Request headers
/// </summary>
public Dictionary<string, string> Headers { get; set; }
/// <summary>
/// Web service URL
/// </summary>
public string Url { get; set; }
/// <summary>
/// Content type - not required
/// Default value is 'text/xml;charset="utf-8"'
/// </summary>
public string ContentType { get; set; }
/// <summary>
/// SOAP action from WSDL
/// </summary>
public string Action { get; set; }
/// <summary>
/// Request body to send
/// SOAP - xml to send
/// </summary>
public string Body { get; set; }
/// <summary>
/// Authentication type
/// Default value is AuthType.NoAuth
/// ApiConnector.Enums.AuthType (NoAuth | Base64 | JWT | OAuth2Password | OAuth2ClientCredentials)
/// In Soap Manager could be use only with Base64
/// </summary>
public AuthType AuthType { get; set; } = AuthType.NoAuth;
/// <summary>
/// Username for authentication purpose
/// SOAP - is used when AuthType is set to Base64
/// </summary>
public string UserName { get; set; }
/// <summary>
/// Password for authentication purpose
/// SOAP - is used when AuthType is set to Base64
/// </summary>
public string Password { get; set; }
}
Powiązane tematy: