コード例 #1
0
        private async Task <string> GetAsync(Uri path)
        {
            _log.Debug("Getting flags with uri: {0}", path.AbsoluteUri);
            var request = new HttpRequestMessage(HttpMethod.Get, path);

            _httpProperties.AddHeaders(request);
            lock (_etags)
            {
                if (_etags.TryGetValue(path, out var etag))
                {
                    request.Headers.IfNoneMatch.Add(etag);
                }
            }

            using (var cts = new CancellationTokenSource(_connectTimeout))
            {
                try
                {
                    using (var response = await _httpClient.SendAsync(request, cts.Token).ConfigureAwait(false))
                    {
                        if (response.StatusCode == HttpStatusCode.NotModified)
                        {
                            _log.Debug("Get all flags returned 304: not modified");
                            return(null);
                        }
                        //We ensure the status code after checking for 304, because 304 isn't considered success
                        if (!response.IsSuccessStatusCode)
                        {
                            throw new UnsuccessfulResponseException((int)response.StatusCode);
                        }
                        lock (_etags)
                        {
                            if (response.Headers.ETag != null)
                            {
                                _etags[path] = response.Headers.ETag;
                            }
                            else
                            {
                                _etags.Remove(path);
                            }
                        }
                        var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                        return(string.IsNullOrEmpty(content) ? null : content);
                    }
                }
                catch (TaskCanceledException tce)
                {
                    if (tce.CancellationToken == cts.Token)
                    {
                        //Indicates the task was cancelled by something other than a request timeout
                        throw;
                    }
                    //Otherwise this was a request timeout.
                    throw new TimeoutException("Get item with URL: " + path.AbsoluteUri +
                                               " timed out after : " + _connectTimeout);
                }
            }
        }
コード例 #2
0
        private HttpRequestMessage PrepareRequest(Uri uri, string payloadId)
        {
            var request = new HttpRequestMessage(HttpMethod.Post, uri);

            _httpProperties.AddHeaders(request);
            if (payloadId != null) // payloadId is provided for regular analytics events payloads, not for diagnostic events
            {
                request.Headers.Add("X-LaunchDarkly-Payload-ID", payloadId);
                request.Headers.Add("X-LaunchDarkly-Event-Schema", CurrentSchemaVersion);
            }
            return(request);
        }