This commit is contained in:
Kyle Spearrin 2019-04-16 17:21:04 -04:00
parent f76051d362
commit 25c82ffd58
2 changed files with 58 additions and 42 deletions

View file

@ -39,5 +39,8 @@ namespace Bit.Core.Abstractions
Task<TResponse> SendAsync<TRequest, TResponse>(HttpMethod method, string path, Task<TResponse> SendAsync<TRequest, TResponse>(HttpMethod method, string path,
TRequest body, bool authed, bool hasResponse); TRequest body, bool authed, bool hasResponse);
void SetUrls(EnvironmentUrls urls); void SetUrls(EnvironmentUrls urls);
Task<CipherResponse> PostCipherAttachmentAsync(string id, MultipartFormDataContent data);
Task PostShareCipherAttachmentAsync(string id, string attachmentId, MultipartFormDataContent data,
string organizationId);
} }
} }

View file

@ -230,12 +230,26 @@ namespace Bit.Core.Services
#region Attachments APIs #region Attachments APIs
public Task<CipherResponse> PostCipherAttachmentAsync(string id, MultipartFormDataContent data)
{
return SendAsync<MultipartFormDataContent, CipherResponse>(HttpMethod.Post,
string.Concat("/ciphers/", id, "/attachment"), data, true, true);
}
public Task DeleteCipherAttachmentAsync(string id, string attachmentId) public Task DeleteCipherAttachmentAsync(string id, string attachmentId)
{ {
return SendAsync<object, object>(HttpMethod.Delete, return SendAsync<object, object>(HttpMethod.Delete,
string.Concat("/ciphers/", id, "/attachments/", attachmentId), null, true, false); string.Concat("/ciphers/", id, "/attachments/", attachmentId), null, true, false);
} }
public Task PostShareCipherAttachmentAsync(string id, string attachmentId, MultipartFormDataContent data,
string organizationId)
{
return SendAsync<MultipartFormDataContent, object>(HttpMethod.Post,
string.Concat("/ciphers/", id, "/attachment/", attachmentId, "/share?organizationId=", organizationId),
data, true, false);
}
#endregion #endregion
#region Sync APIs #region Sync APIs
@ -263,54 +277,53 @@ namespace Bit.Core.Services
public async Task<TResponse> SendAsync<TRequest, TResponse>(HttpMethod method, string path, TRequest body, public async Task<TResponse> SendAsync<TRequest, TResponse>(HttpMethod method, string path, TRequest body,
bool authed, bool hasResponse) bool authed, bool hasResponse)
{ {
var requestMessage = new HttpRequestMessage using(var requestMessage = new HttpRequestMessage())
{ {
Method = method, requestMessage.Method = method;
RequestUri = new Uri(string.Concat(ApiBaseUrl, path)), requestMessage.RequestUri = new Uri(string.Concat(ApiBaseUrl, path));
}; if(body != null)
if(body != null)
{
var bodyType = body.GetType();
if(bodyType == typeof(string))
{ {
requestMessage.Content = new StringContent((object)bodyType as string, Encoding.UTF8, var bodyType = body.GetType();
"application/x-www-form-urlencoded; charset=utf-8"); if(bodyType == typeof(string))
{
requestMessage.Content = new StringContent((object)bodyType as string, Encoding.UTF8,
"application/x-www-form-urlencoded; charset=utf-8");
}
else if(bodyType == typeof(MultipartFormDataContent))
{
requestMessage.Content = body as MultipartFormDataContent;
}
else
{
requestMessage.Content = new StringContent(JsonConvert.SerializeObject(body, _jsonSettings),
Encoding.UTF8, "application/json");
}
} }
else if(false)
{
// TODO: form data content
}
else
{
requestMessage.Content = new StringContent(JsonConvert.SerializeObject(body, _jsonSettings),
Encoding.UTF8, "application/json");
}
}
requestMessage.Headers.Add("Device-Type", _deviceType); requestMessage.Headers.Add("Device-Type", _deviceType);
if(authed) if(authed)
{ {
var authHeader = await GetActiveBearerTokenAsync(); var authHeader = await GetActiveBearerTokenAsync();
requestMessage.Headers.Add("Authorization", string.Concat("Bearer ", authHeader)); requestMessage.Headers.Add("Authorization", string.Concat("Bearer ", authHeader));
} }
if(hasResponse) if(hasResponse)
{ {
requestMessage.Headers.Add("Accept", "application/json"); requestMessage.Headers.Add("Accept", "application/json");
} }
var response = await _httpClient.SendAsync(requestMessage); var response = await _httpClient.SendAsync(requestMessage);
if(hasResponse && response.IsSuccessStatusCode) if(hasResponse && response.IsSuccessStatusCode)
{ {
var responseJsonString = await response.Content.ReadAsStringAsync(); var responseJsonString = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<TResponse>(responseJsonString); return JsonConvert.DeserializeObject<TResponse>(responseJsonString);
}
else if(response.IsSuccessStatusCode)
{
var error = await HandleErrorAsync(response, false);
throw new ApiException(error);
}
return (TResponse)(object)null;
} }
else if(response.IsSuccessStatusCode)
{
var error = await HandleErrorAsync(response, false);
throw new ApiException(error);
}
return (TResponse)(object)null;
} }
public async Task<IdentityTokenResponse> DoRefreshTokenAsync() public async Task<IdentityTokenResponse> DoRefreshTokenAsync()