Пример #1
0
        public async Task <CommitStatusResponse> Post(CommitStatus status, CancellationToken token = default(CancellationToken))
        {
            try {
                return(await PostMessage(status, token));
            }
            catch (WebException error) when(error.Response is HttpWebResponse response)
            {
                if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    throw new ApplicationException($"Status API was not found! [{StatusUrl}]");
                }

                throw;
            }
        }
Пример #2
0
        private async Task <CommitStatusResponse> PostMessage(CommitStatus status, CancellationToken token = default(CancellationToken))
        {
            var data   = status.ToJson();
            var buffer = Encoding.UTF8.GetBytes(data);
            //var url = NetPath.Combine(StatusUrl, Sha);
            var url = StatusUrl.Replace("{sha}", Sha);

            var request = WebRequest.CreateHttp(url);

            request.Method        = "POST";
            request.KeepAlive     = false;
            request.ContentType   = "application/json";
            request.ContentLength = buffer.Length;
            request.UserAgent     = "Photon.Server";

            var hasUsername = !string.IsNullOrEmpty(Username);
            var hasPassword = !string.IsNullOrEmpty(Password);

            if (hasUsername || hasPassword)
            {
                var encoding   = Encoding.GetEncoding("ISO-8859-1");
                var authBuffer = encoding.GetBytes($"{Username}:{Password}");
                var authString = Convert.ToBase64String(authBuffer);
                request.Headers.Add("Authorization", $"Basic {authString}");
            }

            using (token.Register(() => request.Abort())) {
                using (var requestStream = request.GetRequestStream()) {
                    await requestStream.WriteAsync(buffer, 0, buffer.Length, token);
                }

                using (var response = (HttpWebResponse)await request.GetResponseAsync())
                    using (var responseStream = response.GetResponseStream()) {
                        if (responseStream == null)
                        {
                            return(null);
                        }

                        using (var reader = new StreamReader(responseStream))
                            using (var jsonReader = new JsonTextReader(reader)) {
                                return(JsonSettings.Serializer.Deserialize <CommitStatusResponse>(jsonReader));
                            }
                    }
            }
        }