示例#1
0
        public void CreateRepository(CreateRepositoryRequest request)
        {
            try {
                var uri = request.CreateUri(BaseUrl);
                var webRequest = CreateRequest (uri);
                webRequest.Method = "POST";
                webRequest.UserAgent = "DotNet-GithubAPI";

                var payload = request.Entity;
                var requestStream = webRequest.GetRequestStream();
                var serializer = new DataContractJsonSerializer (typeof(RepositoryProperties));
                serializer.WriteObject(requestStream, payload);

                var webResponse = (HttpWebResponse) webRequest.GetResponse ();
                HandleCreateRepositoryResponse(request, webResponse);
            }
            catch(WebException webException) {
                var webResponse = (HttpWebResponse)  webException.Response;
                HandleCreateRepositoryResponse(request, webResponse);
            }
            catch(Exception exception) {
                throw;
            }
        }
示例#2
0
        protected void HandleCreateRepositoryResponse(CreateRepositoryRequest request, HttpWebResponse webResponse)
        {
            int status = (int) webResponse.StatusCode;
            var responseStream = webResponse.GetResponseStream ();
            DataContractJsonSerializer serializer = null;

            if (status == 201 && request.On201 != null)
            {
                serializer = new DataContractJsonSerializer (typeof(Repository));
                Repository result = (Repository) serializer.ReadObject (responseStream);
                request.On201(result);
            }else
            if (status == 400 && request.On400 != null)
            {
                serializer = new DataContractJsonSerializer (typeof(ErrorMessage));
                ErrorMessage result = (ErrorMessage) serializer.ReadObject (responseStream);
                request.On400(result);
            }else
            if (status == 401 && request.On401 != null)
            {
                serializer = new DataContractJsonSerializer (typeof(ErrorMessage));
                ErrorMessage result = (ErrorMessage) serializer.ReadObject (responseStream);
                request.On401(result);
            }else
            if (status == 422 && request.On422 != null)
            {
                serializer = new DataContractJsonSerializer (typeof(ErrorMessage));
                ErrorMessage result = (ErrorMessage) serializer.ReadObject (responseStream);
                request.On422(result);
            }
            else if(request.OnOther != null)
            {
                request.OnOther(webResponse);
            }
        }