private void AddHeaders(HttpClient httpClient, string httpMethod, string path, string content, string contentType) { string contentMd5 = new Md5Digest().Generate(content); string nonce = "" + getCurrentTimeInMilliseconds(); HashBasedMessageAuthenticationCode hmac = new HashBasedMessageAuthenticationCode(ApiSecret); HmacContent hmacContent = new HmacContent(httpMethod, path, contentMd5, contentType, nonce); string authorizationHeader = new HmacAuthorizationHeader(ApiKey, hmac.Generate(hmacContent.ToString())).ToString(); httpClient.DefaultRequestHeaders.Add(HttpHeaders.XAuthorization, authorizationHeader); httpClient.DefaultRequestHeaders.Add(HttpHeaders.UserAgent, getAgentName()); httpClient.DefaultRequestHeaders.Add(HttpHeaders.Nonce, nonce); }
/// <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); } } }