Esempio n. 1
0
        static async Task <List <MisspelledWordModel> > SpellCheckString(string text)
        {
            try
            {
                var flaggedTokenList = await GetDataObjectFromAPI <MisspelledWordRootObjectModel>($"https://api.cognitive.microsoft.com/bing/v7.0/spellcheck/?text={text}");

                return(flaggedTokenList?.FlaggedTokens);
            }
            catch (HttpRequestException e) when(e.Message.Contains("401"))
            {
                DebugHelpers.PrintException(e);
                OnInvalidBingSpellCheckAPIKey();

                return(null);
            }
            catch (HttpRequestException e) when(e.Message.Contains("429"))
            {
                DebugHelpers.PrintException(e);
                OnError429_TooManySpellCheckAPIRequests();

                return(null);
            }
            catch (Exception e)
            {
                DebugHelpers.PrintException(e);
                return(null);
            }
        }
Esempio n. 2
0
        public static async Task <OcrResults> GetOcrResultsFromMediaFile(MediaFile mediaFile)
        {
            try
            {
                var ocrResults = await VisionClient.RecognizeTextAsync(ConverterHelpers.ConvertMediaFileToStream(mediaFile, false));

                return(ocrResults);
            }
            catch (ClientException e) when(e.HttpStatus == 0)
            {
                DebugHelpers.PrintException(e);

                OnInvalidComputerVisionAPIKey();

                return(null);
            }
        }
Esempio n. 3
0
        static async Task <T> GetDataObjectFromAPI <T>(string apiUrl)
        {
            try
            {
                using (var stream = await Client.GetStreamAsync(apiUrl).ConfigureAwait(false))
                    using (var reader = new StreamReader(stream))
                        using (var json = new JsonTextReader(reader))
                        {
                            if (json == null)
                            {
                                return(default(T));
                            }

                            return(await Task.Run(() => _serializer.Deserialize <T>(json)));
                        }
            }
            catch (HttpRequestException e)
            {
                DebugHelpers.PrintException(e);

                if (e.Message.Contains("401"))
                {
                    OnInvalidBingSpellCheckAPIKey();
                }
                else if (e.Message.Contains("429"))
                {
                    OnError429_TooManySpellCheckAPIRequests();
                }

                return(default(T));
            }
            catch (Exception e)
            {
                DebugHelpers.PrintException(e);
                return(default(T));
            }
        }