/// <summary> /// Retrieves image as byte array by its id. /// </summary> /// <param name="id">The ID of the media library element.</param> /// <param name="imageId">The ID of the image.</param> /// <returns></returns> public async Task <byte[]> GetImage(string id, string imageId, ImageTypeEnum imageType) { if (string.IsNullOrEmpty(id)) { return(null); } if (string.IsNullOrEmpty(imageId)) { return(null); } ImageCacheItem cacheItem = new ImageCacheItem { Id = imageId, ImageType = imageType.ToString(), MediaElementId = id }; if (isCacheEnabled) { ImageCacheItem retrievedItem = await _localCacheService.Get <ImageCacheItem>(cacheItem.GetFileName()); if (retrievedItem != null) { return(retrievedItem.Image); } } string imageEndpoint = string.Format(GetImageEndpoint, id, imageType, imageId); using (HttpClient cli = new HttpClient()) { cli.AddAuthorizationHeaders(); HttpResponseMessage result = await cli.GetAsync(imageEndpoint); byte[] retrievedImage = await result.Content.ReadAsByteArrayAsync(); if (isCacheEnabled) { cacheItem.Image = retrievedImage; #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed _localCacheService.Set(cacheItem.GetFileName(), cacheItem); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed } return(retrievedImage); } }
public async Task <bool> Login(string host, LoginModel loginModel) { try { if (!await CheckUrl(host)) { return(false); } string json = JsonConvert.SerializeObject(loginModel); using (HttpClient client = new HttpClient()) { client.AddAuthorizationHeaders(); client.DefaultRequestHeaders.Accept .Add(new MediaTypeWithQualityHeaderValue("application/json")); string authUrl = $"{host}{AuthenticateByNameEndpoint}"; HttpContent content = new StringContent(json, Encoding.UTF8, "application/json"); HttpResponseMessage result = await client.PostAsync(authUrl, content); if (!result.IsSuccessStatusCode) { return(false); } string resultText = await result.Content.ReadAsStringAsync(); LoginResult jsonObject = JsonConvert.DeserializeObject <LoginResult>(resultText); Globals.Instance.AccessToken = jsonObject.AccessToken; Globals.Instance.ServerId = jsonObject.ServerId; Globals.Instance.SessionInfo = jsonObject.SessionInfo; Globals.Instance.User = jsonObject.User; Globals.Instance.Host = host; } return(true); } catch (Exception xc) { return(false); } }