internal static void Shrink_Request_Serialize_Valid() { var request = new ShrinkRequest("http://test_url.com"); var json = JsonConvert.SerializeObject(request); Assert.Equal(@"{""source"":{""url"":""http://test_url.com""}}", json); }
public static async Task <ShrunkUrl> ShrinkUrlAsync(ShrinkRequest req) { var db = DatabaseAccessService.OpenOrCreateDefault(); // Return existing URL if it exists var existingUrl = await RetrieveShrunkUrlByTargetAsync(req.Url); if (existingUrl != null) { return(existingUrl); } var storedUrls = db.GetCollection <ShrunkUrl>(DatabaseAccessService.ShrunkUrlCollectionDatabaseKey); var newShrunkUrl = new ShrunkUrl { Target = req.Url, ShrunkPath = StringUtils.SecureRandomString(7), CreatedTimestamp = DateTime.Now }; await Task.Run(() => { using (var trans = db.BeginTrans()) { // save new url storedUrls.Insert(newShrunkUrl); storedUrls.EnsureIndex(x => x.ShrunkPath); storedUrls.EnsureIndex(x => x.Target); trans.Commit(); } }); return(newShrunkUrl); }
/// <summary> /// Compress image. /// </summary> /// <param name="shrinkRequest">URL to image.</param> /// <returns>URL to compressed image.</returns> public async Task <ShrinkResponse> ShrinkAsync(ShrinkRequest shrinkRequest) { var request = new RestRequest(ShrinkUrl, Method.POST) { RequestFormat = DataFormat.Json, JsonSerializer = NewtonsoftJsonSerializer.Default, OnBeforeDeserialization = r => { r.ContentType = "application/json"; } }; request.AddJsonBody(shrinkRequest); var response = await restClient.ExecuteTaskAsync <ShrinkResponse>(request).ConfigureAwait(false); return(response.Data); }
internal async Task Should_return_response_on_call() { var restClient = new Mock <IRestClient>(); restClient .Setup(r => r.ExecuteTaskAsync <ShrinkResponse>(It.IsAny <IRestRequest>())) .Returns(Task.FromResult((IRestResponse <ShrinkResponse>) new RestResponse <ShrinkResponse> { Data = new ShrinkResponse { Message = "message" } })); var tinifyClient = new TinifyClient(ApiKey, restClient.Object); var request = new ShrinkRequest("image_url.jpg"); var response = await tinifyClient.ShrinkAsync(request).ConfigureAwait(false); Assert.Equal("message", response.Message); }