public async Task Attachment_CreateVideo_ErrorOccursAtPostAttachment_ThrowsIS24Exception() { RestClient.RespondWith(r => { return(new VideoUploadTicket { Auth = "secret", UploadUrl = "http://www.example.com/test", VideoId = "xyz" }); }).ThenWith(r => { return("ok"); }).ThenWith(r => { return(new RestResponseStub { StatusCode = HttpStatusCode.PreconditionFailed, ResponseObject = new Messages() }); }); await AssertEx.ThrowsAsync <IS24Exception>(async() => { var video = new StreamingVideo { Title = "Video" }; var re = new RealEstateItem(new ApartmentRent { Id = 4711 }, Client.Connection); await re.Attachments.CreateStreamingVideoAsync(video, @"..\..\..\test.avi"); }); }
public async Task Attachment_CreateVideo_CanDeserializeResponse() { RestClient.RespondWith(r => { return(new VideoUploadTicket { Auth = "secret", UploadUrl = "http://www.example.com/test", VideoId = "xyz" }); }).ThenWith(r => { return("ok"); }).ThenWith(r => { return(new Messages { Message = { new Message { MessageCode = MessageCode.MESSAGE_RESOURCE_CREATED, MessageProperty = "Resource with id [4711] has been created.", Id = "4711" } } }); }); var video = new StreamingVideo { Title = "Video" }; var re = new RealEstateItem(new ApartmentRent { Id = 4711 }, Client.Connection); await re.Attachments.CreateStreamingVideoAsync(video, @"..\..\..\test.avi"); Assert.Equal(4711, video.Id); Assert.Equal("xyz", video.VideoId); }
/// <summary> /// Creates a video stream attachment (upload to http://www.screen9.com/). /// </summary> /// <param name="video">The attachment.</param> /// <param name="path">The path to the attachment file.</param> /// <returns></returns> /// <exception cref="System.NotImplementedException"></exception> public async Task CreateStreamingVideoAsync(StreamingVideo video, string path) { // 1. Request a ticket for upload var req = Connection.CreateRequest("videouploadticket"); var videoUploadTicket = await ExecuteAsync <VideoUploadTicket>(Connection, req); // 2. Upload your video to screen9 var uploadClient = Connection.RestClientFactory(videoUploadTicket.UploadUrl); req = new RestRequest(Method.POST); req.AddParameter("auth", videoUploadTicket.Auth); var fileName = Path.GetFileName(path); byte[] bytes = null; using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true)) using (var ms = new MemoryStream()) { await fs.CopyToAsync(ms); bytes = ms.ToArray(); } req.AddFile("videofile", bytes, fileName, "application/octet-stream"); var resp = await uploadClient.ExecuteTaskAsync(req); if (resp.ErrorException != null) { throw new IS24Exception(string.Format("Error uploading video {0}.", path), resp.ErrorException); } if ((int)resp.StatusCode >= 400) { throw new IS24Exception(string.Format("Error uploading video {0}: {1}", path, resp.StatusDescription)); } // 3. Post StreamingVideo attachment req = Connection.CreateRequest("realestate/{id}/attachment", Method.POST); req.AddParameter("id", RealEstate.Id, ParameterType.UrlSegment); video.VideoId = videoUploadTicket.VideoId; req.AddBody(video, typeof(Attachment)); var msg = await ExecuteAsync <Messages>(Connection, req); var id = msg.ExtractCreatedResourceId(); if (!id.HasValue) { throw new IS24Exception(string.Format("Error creating attachment {0}: {1}", path, msg.ToMessage())) { Messages = msg }; } video.Id = id.Value; }
public async Task Attachment_CreateVideo_ErrorOccurs_ThrowsIS24Exception() { RestClient.RespondWith(r => { return(new RestResponseStub { StatusCode = HttpStatusCode.PreconditionFailed, ResponseObject = new Messages() }); }); await AssertEx.ThrowsAsync <IS24Exception>(async() => { var video = new StreamingVideo { Title = "Video" }; var re = new RealEstateItem(new ApartmentRent { Id = 4711 }, Client.Connection); await re.Attachments.CreateStreamingVideoAsync(video, @"..\..\..\test.avi"); }); }
public async Task Attachment_CreateVideo_RequestsCorrectResource() { RestClient.RespondWith(r => { Assert.Equal(Method.GET, r.Method); Assert.Equal("https://rest.sandbox-immobilienscout24.de/restapi/api/offer/v1.0/user/me/videouploadticket", RestClient.BuildUri(r).AbsoluteUri); return(new VideoUploadTicket { Auth = "secret", UploadUrl = "http://www.example.com/test", VideoId = "xyz" }); }).ThenWith(r => { Assert.Equal(Method.POST, r.Method); Assert.Equal("secret", (string)r.Parameters.Single(p => p.Name == "auth").Value); Assert.Equal("http://www.example.com/test", RestClient.BuildUri(r).AbsoluteUri); return("ok"); }).ThenWith(r => { Assert.Equal(Method.POST, r.Method); var v = new BaseXmlDeserializer().Deserialize <Attachment>(new RestResponse { Content = (string)r.Body() });; Assert.Equal("xyz", ((StreamingVideo)v).VideoId); Assert.Equal("Video", ((StreamingVideo)v).Title); Assert.Equal("https://rest.sandbox-immobilienscout24.de/restapi/api/offer/v1.0/user/me/realestate/4711/attachment", RestClient.BuildUri(r).AbsoluteUri); return(new Messages { Message = { new Message { MessageCode = MessageCode.MESSAGE_RESOURCE_CREATED, MessageProperty = "Resource with id [4711] has been created.", Id = "4711" } } }); }); var video = new StreamingVideo { Title = "Video" }; var re = new RealEstateItem(new ApartmentRent { Id = 4711 }, Client.Connection); await re.Attachments.CreateStreamingVideoAsync(video, @"..\..\..\test.avi"); }
/// <summary> /// Creates a video stream attachment (upload to http://www.screen9.com/). /// </summary> /// <param name="video">The attachment.</param> /// <param name="path">The path to the attachment file.</param> /// <returns></returns> /// <exception cref="System.NotImplementedException"></exception> public async Task CreateStreamingVideoAsync(StreamingVideo video, string path) { // 1. Request a ticket for upload var req = Connection.CreateRequest("videouploadticket"); var videoUploadTicket = await ExecuteAsync<VideoUploadTicket>(Connection, req); // 2. Upload your video to screen9 var uploadClient = new RestClient(videoUploadTicket.UploadUrl); if (Connection.HttpFactory != null) uploadClient.HttpFactory = Connection.HttpFactory; req = new RestRequest(); req.AddParameter("auth", videoUploadTicket.Auth); var fileName = Path.GetFileName(path); byte[] bytes = null; using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true)) using (var ms = new MemoryStream()) { await fs.CopyToAsync(ms); bytes = ms.ToArray(); } req.AddFile("videofile", bytes, fileName, "application/octet-stream"); var resp = await uploadClient.ExecutePostTaskAsync(req); if (resp.ErrorException != null) throw new IS24Exception(string.Format("Error uploading video {0}.", path), resp.ErrorException); if ((int)resp.StatusCode >= 400) throw new IS24Exception(string.Format("Error uploading video {0}: {1}", path, resp.StatusDescription)); // 3. Post StreamingVideo attachment req = Connection.CreateRequest("realestate/{id}/attachment", Method.POST); req.AddParameter("id", RealEstate.Id, ParameterType.UrlSegment); video.VideoId = videoUploadTicket.VideoId; req.AddBody(video, typeof(Attachment)); var msg = await ExecuteAsync<Messages>(Connection, req); var id = msg.ExtractCreatedResourceId(); if (!id.HasValue) { throw new IS24Exception(string.Format("Error creating attachment {0}: {1}", path, msg.ToMessage())) { Messages = msg }; } video.Id = id.Value; }
private static async Task TestAsync(Config config) { var connection = new IS24Connection { ConsumerKey = config.ConsumerKey, ConsumerSecret = config.ConsumerSecret, AccessToken = config.AccessToken, AccessTokenSecret = config.AccessSecret, BaseUrlPrefix = @"https://rest.sandbox-immobilienscout24.de/restapi/api" }; var searchResource = new SearchResource(connection); var query = new RadiusQuery { Latitude = 52.49023, Longitude = 13.35939, Radius = 1, RealEstateType = RealEstateType.APARTMENT_RENT, Parameters = new { Price = new DecimalRange { Max = 1000 }, LivingSpace = new DecimalRange { Min = 100 }, NumberOfRooms = new DecimalRange { Min = 4 }, Equipment = "balcony" }, Channel = new HomepageChannel("me") }; var results = await searchResource.Search(query); var api = new ImportExportClient(connection); RealtorContactDetails contact = null; try { contact = await api.Contacts.GetAsync("Hans Meiser", isExternal: true); } catch (IS24Exception ex) { if (ex.Messages.Message.First().MessageCode != MessageCode.ERROR_RESOURCE_NOT_FOUND) throw; } if (contact == null) { contact = new RealtorContactDetails { Lastname = "Meiser", Firstname = "Hans", Address = new Address { Street = "Hauptstraße", HouseNumber = "1", Postcode = "10827", City = "Berlin", InternationalCountryRegion = new CountryRegion { Country = CountryCode.DEU, Region = "Berlin" } }, Email = "*****@*****.**", ExternalId = "Hans Meiser" }; await api.Contacts.CreateAsync(contact); contact.Address.HouseNumber = "1a"; await api.Contacts.UpdateAsync(contact); } IRealEstate realEstate = null; try { realEstate = await api.RealEstates.GetAsync("Hauptstraße 1", isExternal: true); } catch (IS24Exception ex) { if (ex.Messages.Message.First().MessageCode != MessageCode.ERROR_RESOURCE_NOT_FOUND) throw; } if (realEstate == null) { var re = new ApartmentRent { Contact = new RealEstateContact { Id = contact.Id }, ExternalId = "Hauptstraße 1", Title = "IS24RestApi Test", Address = new Wgs84Address { Street = "Hauptstraße", HouseNumber = "1", City = "Berlin", Postcode = "10827" }, BaseRent = 500.0, LivingSpace = 100.0, NumberOfRooms = 4.0, ShowAddress = true, Courtage = new CourtageInfo { HasCourtage = YesNoNotApplicableType.NO } }; await api.RealEstates.CreateAsync(re); re.BaseRent += 100.0; await api.RealEstates.UpdateAsync(re); realEstate = new RealEstateItem(re, connection); } else { var re = (ApartmentRent)realEstate.RealEstate; re.BaseRent += 100.0; await api.RealEstates.UpdateAsync(re); re.BaseRent -= 100.0; await api.RealEstates.UpdateAsync(re); } var projects = await api.RealEstateProjects.GetAllAsync(); if (projects.RealEstateProject.Any()) { var project = projects.RealEstateProject.First(); var entries = await api.RealEstateProjects.GetAllAsync(project.Id.Value); if (!entries.RealEstateProjectEntry.Any(e => e.RealEstateId.Value == realEstate.RealEstate.Id.Value)) await api.RealEstateProjects.AddAsync(project.Id.Value, realEstate.RealEstate); } var placement = await realEstate.PremiumPlacements.GetAsync(); var atts = await realEstate.Attachments.GetAsync(); if (atts == null || !atts.Any()) { var att = new Picture { Floorplan = false, TitlePicture = true, Title = "Zimmer", }; await realEstate.Attachments.CreateAsync(att, @"..\..\test.jpg"); att.Title = "Zimmer 1"; await realEstate.Attachments.UpdateAsync(att); var link = new Link { Title = "Test", Url = "http://www.example.com/" }; await realEstate.Attachments.CreateAsync(link); var video = new StreamingVideo { Title = "Video" }; await realEstate.Attachments.CreateStreamingVideoAsync(video, @"..\..\test.avi"); } var res = new List<RealEstate>(); await api.RealEstates.GetAsync().ForEachAsync(x => res.Add(x.RealEstate)); }