public async Task <ServiceResult <Geoposition> > GetGeopositionAsync(uint?accuracyInMeters = 50, int maximumAgeInMinutes = 5, int timeoutInSeconds = 10) { var geolocator = new Geolocator { DesiredAccuracyInMeters = accuracyInMeters }; try { return(await geolocator.GetGeopositionAsync( TimeSpan.FromMinutes(maximumAgeInMinutes), TimeSpan.FromSeconds(timeoutInSeconds) ).AsTask().ConfigureAwait(false)); } catch (Exception xcp) { if ((uint)xcp.HResult == 0x80004004) { // the application does not have the right capability or the location master switch is off return(ServiceResult <Geoposition> .Create( null, null, false, 401, "the application does not have the right capability or the location master switch is off")); } return(ServiceResult <Geoposition> .CreateError(xcp)); } }
public async Task <ServiceResult <TwitterData> > GetOlderTweets(long maxId) { await this.Authorize(); if (bearerToken != null) { string q = "q=%40tarabicaconf OR %23tarabica OR %23tarabica19 OR %23tarabicamvp"; string count = "count=20"; string result_type = "result_type=recent"; string include_entities = "include_entities=true"; string tweet_mode = "tweet_mode=extended"; string since_id = String.Format("max_id={0}", maxId); string query = "?" + q + "&" + count + "&" + include_entities + "&" + tweet_mode + "&" + result_type + "&" + since_id; HttpClientHandler handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }; using (var client = new HttpClient(handler)) { try { var request = new HttpRequestMessage { Method = new HttpMethod("GET") }; request.Headers.Add("Accept-Encoding", "gzip"); request.Headers.Add("Authorization", $"Bearer {this.bearerToken}"); client.BaseAddress = new Uri(SearchTweetsUrl + query); var result = await client.SendAsync(request); string resultData = new StreamReader(await result.Content.ReadAsStreamAsync()).ReadToEnd(); var twitterData = JsonConvert.DeserializeObject <TwitterData>(resultData); //foreach (var tweet in twitterData.Statuses) //{ // tweet.Text = tweet.Text.Replace("\n", " ").Replace("\t", " "); //} return(twitterData); } catch (Exception e) { Debug.WriteLine("HTTP ERROR: " + e.Message); return(ServiceResult <TwitterData> .CreateError(e)); } } } return(ServiceResult <TwitterData> .CreateError(0, "Došlo je do greške prilikom komunikacije sa Twitter serverom. Proverite Internet konekciju.", "")); }
public ServiceResult FloorType_Approve_Update(string floorTypeId) { var newFloorType = this.FloorType_GetById(floorTypeId); if (newFloorType == null) { return(ServiceResult.CreateError("Không tìm thấy loại tầng")); } newFloorType.CREATE_DT = newFloorType.APPROVE_DT = null; return(this.procedureHelper.GetData <ServiceResult>("FloorType_Approve_Update", newFloorType)[0]); }
private async Task <AuthorizationResponse> Authorize() { var bearerTokenCredentialsEncoded = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Uri.EscapeUriString(ConsumerKey)}:{Uri.EscapeUriString(ConsumerSecret)}")); HttpClientHandler handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }; using (var client = new HttpClient(handler)) { try { var request = new HttpRequestMessage { Method = new HttpMethod("POST") }; request.Headers.Add("Accept-Encoding", "gzip"); request.Headers.Add("Authorization", $"Basic {bearerTokenCredentialsEncoded}"); request.Content = new StringContent( "grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded"); client.BaseAddress = new Uri(RequestTokenUrl); var result = await client.SendAsync(request); string resultData = new StreamReader(await result.Content.ReadAsStreamAsync()).ReadToEnd(); var authorizationResponse = JsonConvert.DeserializeObject <AuthorizationResponse>(resultData); this.bearerToken = authorizationResponse.AccessToken; return(authorizationResponse); } catch (Exception e) { Debug.WriteLine("HTTP ERROR: " + e.Message); return(ServiceResult <AuthorizationResponse> .CreateError(e)); } } }
public async Task <ServiceResult <ConferenceData> > GetConferenceDataAsync(CancellationToken cancellationToken) { var data = await this.httpClient.GetJsonAsync <ConferenceData>(ServiceUriString + ServiceGetDataMethod, cancellationToken); if (data.Successful) { //foreach (var speaker in data.Value.Speakers) //{ // speaker.PictureUrl = $"{data.Value.PicturesLocation}/{speaker.PictureUrl}"; //} } else { return(ServiceResult <ConferenceData> .CreateError( data.ErrorCode, data.ErrorMessage, data.ErrorDescription)); } return(data); }
public async Task <ServiceResult <ConferenceData> > GetConferenceData(CancellationToken cancellationToken = new CancellationToken()) { ConferenceData conferenceData = null; string json = null; try { json = this.cacheService.GetValueOrDefault <string>(ConferenceDataKey); } catch (Exception) { } ConferenceData item = null; if (json != null) { try { item = JsonConvert.DeserializeObject <ConferenceData>(json); } catch (Exception) { } } if (item != null) { conferenceData = item; if (cancellationToken.IsCancellationRequested) { return(this.FillInFavorites(conferenceData)); } var versionId = conferenceData.Version; var result = await this.conferenceDataService.GetVersionAsync(cancellationToken);//.ConfigureAwait(false); int latestVersionId = 0; if (result.Successful) { latestVersionId = result.Value; } if (versionId >= latestVersionId) { return(this.FillInFavorites(conferenceData)); } } else { var assembly = typeof(ConferenceRepository).GetTypeInfo().Assembly; Stream stream = assembly.GetManifestResourceStream("Conference.Common.Data.Resources.DefaultConferenceData.json"); string text = string.Empty; using (var reader = new System.IO.StreamReader(stream)) { text = reader.ReadToEnd(); } if (!string.IsNullOrEmpty(text)) { try { conferenceData = JsonConvert.DeserializeObject <ConferenceData>(text); var result = await this.conferenceDataService.GetVersionAsync(cancellationToken);//.ConfigureAwait(false); int latestVersionId = 0; if (result.Successful) { latestVersionId = result.Value; } if (conferenceData.Version >= latestVersionId) { return(this.FillInFavorites(conferenceData)); } } catch (Exception ex) { return(ServiceResult <ConferenceData> .CreateError(ex)); } } } var newData = await this.conferenceDataService.GetConferenceDataAsync(cancellationToken);//.ConfigureAwait(false); if (newData.Successful) { if (conferenceData != null) { foreach (var session in conferenceData.Sessions) { var targetSession = newData.Value.Sessions.FirstOrDefault(s => s.Id == session.Id); if (targetSession != null) { targetSession.IsFavorite = this.GetFavorite(session.Id); } } } this.cacheService.AddOrUpdateValue(ConferenceDataKey, JsonConvert.SerializeObject(newData.Value)); } else { newData = conferenceData; this.cacheService.AddOrUpdateValue(ConferenceDataKey, JsonConvert.SerializeObject(newData.Value)); } return(this.FillInFavorites(newData)); }