コード例 #1
0
ファイル: StructurizrClient.cs プロジェクト: fhchina/dotnet-1
        public void PutWorkspace(long workspaceId, Workspace workspace)
        {
            if (workspace == null)
            {
                throw new ArgumentException("A workspace must be supplied");
            }
            else if (workspaceId <= 0)
            {
                throw new ArgumentException("The workspace ID must be set");
            }

            if (MergeFromRemote)
            {
                Workspace remoteWorkspace = GetWorkspace(workspaceId);
                if (remoteWorkspace != null)
                {
                    workspace.Views.CopyLayoutInformationFrom(remoteWorkspace.Views);
                    workspace.Views.Configuration.CopyConfigurationFrom(remoteWorkspace.Views.Configuration);
                }
            }

            workspace.Id = workspaceId;

            using (WebClient webClient = new WebClient())
            {
                try
                {
                    string httpMethod      = "PUT";
                    string path            = WorkspacePath + workspaceId;
                    string workspaceAsJson = "";

                    using (StringWriter stringWriter = new StringWriter())
                    {
                        if (EncryptionStrategy == null)
                        {
                            JsonWriter jsonWriter = new JsonWriter(false);
                            jsonWriter.Write(workspace, stringWriter);
                        }
                        else
                        {
                            EncryptedWorkspace  encryptedWorkspace = new EncryptedWorkspace(workspace, EncryptionStrategy);
                            EncryptedJsonWriter jsonWriter         = new EncryptedJsonWriter(false);
                            jsonWriter.Write(encryptedWorkspace, stringWriter);
                        }
                        stringWriter.Flush();
                        workspaceAsJson = stringWriter.ToString();
                        System.Console.WriteLine(workspaceAsJson);
                    }

                    AddHeaders(webClient, httpMethod, path, workspaceAsJson, "application/json; charset=UTF-8");

                    string response = webClient.UploadString(this.Url + path, httpMethod, workspaceAsJson);
                    System.Console.WriteLine(response);
                }
                catch (Exception e)
                {
                    throw new StructurizrClientException("There was an error putting the workspace: " + e.Message, e);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Updates the given workspace.
        /// </summary>
        /// <param name="workspaceId">The workspace ID.</param>
        /// <param name="workspace">The workspace to be updated.</param>
        public void PutWorkspace(long workspaceId, Workspace workspace)
        {
            if (workspace == null)
            {
                throw new ArgumentException("The workspace must not be null.");
            }
            else if (workspaceId <= 0)
            {
                throw new ArgumentException("The workspace ID must be a positive integer.");
            }

            if (MergeFromRemote)
            {
                Workspace remoteWorkspace = GetWorkspace(workspaceId);
                if (remoteWorkspace != null)
                {
                    workspace.Views.CopyLayoutInformationFrom(remoteWorkspace.Views);
                    workspace.Views.Configuration.CopyConfigurationFrom(remoteWorkspace.Views.Configuration);
                }
            }

            workspace.Id = workspaceId;
            workspace.LastModifiedDate  = DateTime.UtcNow;
            workspace.LastModifiedAgent = getAgentName();
            workspace.LastModifiedUser  = getUser();

            using (HttpClient httpClient = createHttpClient())
            {
                try
                {
                    string httpMethod      = "PUT";
                    string path            = WorkspacePath + workspaceId;
                    string workspaceAsJson = "";

                    using (StringWriter stringWriter = new StringWriter())
                    {
                        if (EncryptionStrategy == null)
                        {
                            JsonWriter jsonWriter = new JsonWriter(false);
                            jsonWriter.Write(workspace, stringWriter);
                        }
                        else
                        {
                            EncryptedWorkspace  encryptedWorkspace = new EncryptedWorkspace(workspace, EncryptionStrategy);
                            EncryptedJsonWriter jsonWriter         = new EncryptedJsonWriter(false);
                            jsonWriter.Write(encryptedWorkspace, stringWriter);
                        }
                        stringWriter.Flush();
                        workspaceAsJson = stringWriter.ToString();
                        System.Console.WriteLine(workspaceAsJson);
                    }

                    AddHeaders(httpClient, httpMethod, new Uri(Url + path).AbsolutePath, workspaceAsJson, "application/json; charset=UTF-8");

                    HttpContent content = new StringContent(workspaceAsJson, Encoding.UTF8, "application/json");
                    content.Headers.ContentType.CharSet = "UTF-8";
                    string contentMd5 = new Md5Digest().Generate(workspaceAsJson);
                    string contentMd5Base64Encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(contentMd5));
                    content.Headers.ContentMD5 = Encoding.UTF8.GetBytes(contentMd5);

                    var    response        = httpClient.PutAsync(this.Url + path, content);
                    string responseContent = response.Result.Content.ReadAsStringAsync().Result;
                    System.Console.WriteLine(responseContent);

                    if (response.Result.StatusCode != HttpStatusCode.OK)
                    {
                        ApiResponse apiResponse = ApiResponse.Parse(responseContent);
                        throw new StructurizrClientException(apiResponse.Message);
                    }
                }
                catch (Exception e)
                {
                    throw new StructurizrClientException("There was an error putting the workspace: " + e.Message, e);
                }
            }
        }