diff --git a/Runtime/Scripts/OpenAPI/UnityWebRequestHttpClient.cs b/Runtime/Scripts/OpenAPI/UnityWebRequestHttpClient.cs index b0a847cb9db90410f41118a0a1f466380adec98f..94466cec553cae872c2b684a5ec167c2ad00445c 100644 --- a/Runtime/Scripts/OpenAPI/UnityWebRequestHttpClient.cs +++ b/Runtime/Scripts/OpenAPI/UnityWebRequestHttpClient.cs @@ -27,6 +27,41 @@ namespace ETSI.ARF.OpenAPI public void Dispose(); } + public class BasicHTTPClient : IHttpClient + { + + public BasicHTTPClient() { } + + public BasicHTTPClient(string baseUri) + { + BaseAddress = new Uri(baseUri); + _httpClient.BaseAddress = BaseAddress; + } + + public BasicHTTPClient (Uri baseUri) + { + BaseAddress = baseUri; + _httpClient.BaseAddress = BaseAddress; + } + + public Uri BaseAddress { get; set; } + public HttpRequestHeaders DefaultRequestHeaders => _httpClient.DefaultRequestHeaders; + + private readonly HttpClient _httpClient = new HttpClient(); + + public async Task SendAsync(HttpRequestMessage message, HttpCompletionOption option, CancellationToken token) + { + return await _httpClient.SendAsync(message, option, token); + } + + public void Dispose() + { + _httpClient.Dispose(); + DefaultRequestHeaders.Clear(); + BaseAddress = null; + } + } + public class UnityWebRequestHttpClient : IHttpClient { public UnityWebRequestHttpClient() { } diff --git a/Runtime/Scripts/REST/RelocalizationInformationRequest.cs b/Runtime/Scripts/REST/RelocalizationInformationRequest.cs index 8c4a22f1263502aef396d45cc48241946f9f8cc0..309618c9459d89ebd8bc4eda267727d2c9660624 100644 --- a/Runtime/Scripts/REST/RelocalizationInformationRequest.cs +++ b/Runtime/Scripts/REST/RelocalizationInformationRequest.cs @@ -15,18 +15,16 @@ // See the License for the specific language governing permissions and // limitations under the License. // -// Last change: March 2024 +// Last change: May 2024 // using System; using System.Collections.Generic; -using System.Threading.Tasks; -using UnityEngine; - using ETSI.ARF.OpenAPI.WorldStorage; using ETSI.ARF.OpenAPI; +using Newtonsoft.Json; namespace ETSI.ARF.WorldStorage.REST -{ +{ public class RelocalizationInformationRequest : RequestBase { @@ -36,8 +34,7 @@ namespace ETSI.ARF.WorldStorage.REST var httpClient = new UnityWebRequestHttpClient(ws.URI); apiClient = new WorldStorageClient(httpClient); - Debug.Log("Request Reloc Information .."); - ResponseObject ro = new ResponseObject("Request Reloc Inofmration ", func); + ResponseObject ro = new ResponseObject("Request Reloc Information ", func); List anonymous = new List(); for (int i = 0; i< uuids.Count; i++) @@ -51,5 +48,174 @@ namespace ETSI.ARF.WorldStorage.REST apiClient.GetRelocalizationInformationAsync(token, anonymous, capabilities).ContinueWith(OnReceiveObject, ro); return ro; } + + + static public Response GetRelocalizationInformation(WorldStorageServer ws, List uuids, List modes, List capabilities) + { + wsServer = ws; + var httpClient = new BasicHTTPClient(ws.URI); + apiClient = new MyWorldStorageClient(httpClient); + + + List anonymous = new List(); + for (int i = 0; i < uuids.Count; i++) + { + // Check same size or give anonymous as parameter? + Anonymous newOne = new Anonymous(); + newOne.Uuid = uuids[i]; + newOne.Mode = modes[i]; + anonymous.Add(newOne); + } + + Response ro = apiClient.GetRelocalizationInformation(token, anonymous, capabilities); + return ro; + } + } +} + +namespace ETSI.ARF.OpenAPI.WorldStorage +{ + public partial class WorldStorageClient + { + public ETSI.ARF.OpenAPI.IHttpClient GetHttpClient() + { + return _httpClient; + } + + public string ConvertToString_(object value, System.Globalization.CultureInfo cultureInfo) + { + return ConvertToString(value, cultureInfo); + } + + + public void PrepareRequest_(ETSI.ARF.OpenAPI.IHttpClient client, System.Net.Http.HttpRequestMessage request, string url) + { + PrepareRequest(client, request, url); + } + public void PrepareRequest_(ETSI.ARF.OpenAPI.IHttpClient client, System.Net.Http.HttpRequestMessage request, System.Text.StringBuilder urlBuilder) + { + PrepareRequest(client, request, urlBuilder); + } + public void ProcessResponse_(ETSI.ARF.OpenAPI.IHttpClient client, System.Net.Http.HttpResponseMessage response) + { + ProcessResponse(client, response); + } + } + + public partial class MyWorldStorageClient : WorldStorageClient + { + public MyWorldStorageClient(IHttpClient httpClient) : base(httpClient) + { + } + + /// + /// Override Nswag generated function to access relocalization information : deal with parameters in the query + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public override async System.Threading.Tasks.Task GetRelocalizationInformationAsync(string token, System.Collections.Generic.IEnumerable uuids, System.Collections.Generic.IEnumerable capabilities, System.Threading.CancellationToken cancellationToken) + { + if (uuids == null) + throw new System.ArgumentNullException("uuids"); + + if (capabilities == null) + throw new System.ArgumentNullException("capabilities"); + + var client_ = GetHttpClient(); + var disposeClient_ = false; + try + { + using (var request_ = new System.Net.Http.HttpRequestMessage()) + { + + if (token != null) + request_.Headers.TryAddWithoutValidation("token", ConvertToString_(token, System.Globalization.CultureInfo.InvariantCulture)); + request_.Method = new System.Net.Http.HttpMethod("GET"); + request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json")); + + var urlBuilder_ = new System.Text.StringBuilder(); + + urlBuilder_.Append("relocalizationInformation"); + urlBuilder_.Append('?'); + foreach (var item_ in uuids) { urlBuilder_.Append(System.Uri.EscapeDataString("uuids")).Append('=').Append(System.Uri.EscapeDataString(JsonConvert.SerializeObject(item_))).Append('&'); } + foreach (var item_ in capabilities) { urlBuilder_.Append(System.Uri.EscapeDataString("capabilities")).Append('=').Append(System.Uri.EscapeDataString(JsonConvert.SerializeObject(item_))).Append('&'); } + urlBuilder_.Length--; + + PrepareRequest_(client_, request_, urlBuilder_); + + var url_ = urlBuilder_.ToString(); + request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute); + + PrepareRequest_(client_, request_, url_); + + var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); + var disposeResponse_ = true; + try + { + var headers_ = new System.Collections.Generic.Dictionary>(); + foreach (var item_ in response_.Headers) + headers_[item_.Key] = item_.Value; + if (response_.Content != null && response_.Content.Headers != null) + { + foreach (var item_ in response_.Content.Headers) + headers_[item_.Key] = item_.Value; + } + + ProcessResponse_(client_, response_); + + var status_ = (int)response_.StatusCode; + if (status_ == 200) + { + var objectResponse_ = await ReadObjectResponseAsync(response_, headers_, cancellationToken).ConfigureAwait(false); + if (objectResponse_.Object == null) + { + throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null); + } + return objectResponse_.Object; + } + else + if (status_ == 400) + { + var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false); + var result_ = (string)System.Convert.ChangeType(responseData_, typeof(string)); + throw new ApiException("Invalid UUID supplied.", status_, responseData_, headers_, result_, null); + } + else + if (status_ == 404) + { + var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false); + var result_ = (string)System.Convert.ChangeType(responseData_, typeof(string)); + throw new ApiException("Not found, could not find UUID in database.", status_, responseData_, headers_, result_, null); + } + else + { + var objectResponse_ = await ReadObjectResponseAsync(response_, headers_, cancellationToken).ConfigureAwait(false); + if (objectResponse_.Object == null) + { + throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null); + } + throw new ApiException("Unexpected error.", status_, objectResponse_.Text, headers_, objectResponse_.Object, null); + } + } + finally + { + if (disposeResponse_) + response_.Dispose(); + } + } + } + finally + { + if (disposeClient_) + client_.Dispose(); + } + } } } \ No newline at end of file