예제 #1
0
 public async Task <AssetLocation> AddAssetLocationToAssetAsync(
     int assetId,
     AddAssetLocationToAssetAssetLocationType assetLocationType,
     string location,
     CancellationToken cancellationToken = default
     )
 {
     using (var _res = await AddAssetLocationToAssetInternalAsync(
                assetId,
                assetLocationType,
                location,
                cancellationToken
                ).ConfigureAwait(false))
     {
         return(_res.Body);
     }
 }
예제 #2
0
        internal async Task <HttpOperationResponse <AssetLocation> > AddAssetLocationToAssetInternalAsync(
            int assetId,
            AddAssetLocationToAssetAssetLocationType assetLocationType,
            string location,
            CancellationToken cancellationToken = default
            )
        {
            if (assetId == default(int))
            {
                throw new ArgumentNullException(nameof(assetId));
            }

            if (assetLocationType == default(AddAssetLocationToAssetAssetLocationType))
            {
                throw new ArgumentNullException(nameof(assetLocationType));
            }

            if (string.IsNullOrEmpty(location))
            {
                throw new ArgumentNullException(nameof(location));
            }

            const string apiVersion = "2019-01-16";

            var _path = "/api/assets/{assetId}/locations";

            _path = _path.Replace("{assetId}", Client.Serialize(assetId));

            var _query = new QueryBuilder();

            if (!string.IsNullOrEmpty(location))
            {
                _query.Add("location", Client.Serialize(location));
            }
            if (assetLocationType != default(AddAssetLocationToAssetAssetLocationType))
            {
                _query.Add("assetLocationType", Client.Serialize(assetLocationType));
            }
            _query.Add("api-version", Client.Serialize(apiVersion));

            var _uriBuilder = new UriBuilder(Client.BaseUri);

            _uriBuilder.Path  = _uriBuilder.Path.TrimEnd('/') + _path;
            _uriBuilder.Query = _query.ToString();
            var _url = _uriBuilder.Uri;

            HttpRequestMessage  _req = null;
            HttpResponseMessage _res = null;

            try
            {
                _req = new HttpRequestMessage(HttpMethod.Post, _url);

                if (Client.Credentials != null)
                {
                    await Client.Credentials.ProcessHttpRequestAsync(_req, cancellationToken).ConfigureAwait(false);
                }

                _res = await Client.SendAsync(_req, cancellationToken).ConfigureAwait(false);

                if (!_res.IsSuccessStatusCode)
                {
                    await OnAddAssetLocationToAssetFailed(_req, _res);
                }
                string _responseContent = await _res.Content.ReadAsStringAsync().ConfigureAwait(false);

                return(new HttpOperationResponse <AssetLocation>
                {
                    Request = _req,
                    Response = _res,
                    Body = Client.Deserialize <AssetLocation>(_responseContent),
                });
            }
            catch (Exception)
            {
                _req?.Dispose();
                _res?.Dispose();
                throw;
            }
        }
예제 #3
0
        public async Task <AssetLocation> AddAssetLocationToAssetAsync(
            int assetId,
            AddAssetLocationToAssetAssetLocationType assetLocationType,
            string location,
            CancellationToken cancellationToken = default
            )
        {
            if (assetId == default(int))
            {
                throw new ArgumentNullException(nameof(assetId));
            }

            if (assetLocationType == default(AddAssetLocationToAssetAssetLocationType))
            {
                throw new ArgumentNullException(nameof(assetLocationType));
            }

            if (string.IsNullOrEmpty(location))
            {
                throw new ArgumentNullException(nameof(location));
            }

            const string apiVersion = "2019-01-16";

            var _baseUri = Client.Options.BaseUri;
            var _url     = new RequestUriBuilder();

            _url.Reset(_baseUri);
            _url.AppendPath(
                "/api/assets/{assetId}/locations".Replace("{assetId}", Uri.EscapeDataString(Client.Serialize(assetId))),
                false);

            if (!string.IsNullOrEmpty(location))
            {
                _url.AppendQuery("location", Client.Serialize(location));
            }
            if (assetLocationType != default(AddAssetLocationToAssetAssetLocationType))
            {
                _url.AppendQuery("assetLocationType", Client.Serialize(assetLocationType));
            }
            _url.AppendQuery("api-version", Client.Serialize(apiVersion));


            using (var _req = Client.Pipeline.CreateRequest())
            {
                _req.Uri    = _url;
                _req.Method = RequestMethod.Post;

                using (var _res = await Client.SendAsync(_req, cancellationToken).ConfigureAwait(false))
                {
                    if (_res.Status < 200 || _res.Status >= 300)
                    {
                        await OnAddAssetLocationToAssetFailed(_req, _res).ConfigureAwait(false);
                    }

                    if (_res.ContentStream == null)
                    {
                        await OnAddAssetLocationToAssetFailed(_req, _res).ConfigureAwait(false);
                    }

                    using (var _reader = new StreamReader(_res.ContentStream))
                    {
                        var _content = await _reader.ReadToEndAsync().ConfigureAwait(false);

                        var _body = Client.Deserialize <AssetLocation>(_content);
                        return(_body);
                    }
                }
            }
        }