示例#1
0
        async Task <CallServiceResponse <TTServiceResponse> > ICallServices <TTServiceResponse> .CallServiceAsync(Uri url, System.Collections.Generic.Dictionary <string, string> apiArgs)
        {
            Log.WriteLine("GenericServiceAsync: url:" + url);
            CallServiceResponse <TTServiceResponse> response = new CallServiceResponse <TTServiceResponse>(service);

            foreach (Settings.Request request in service.requests.Where(p => p.argType == "url"))
            {
                response.Request = request; // init when response object is newed up?
                await HttpMethods.CallApiAsync(response, url, apiArgs);

                switch (request.response.type)
                {
                case "binary":
                    System.IO.File.WriteAllBytes(response.FileName + ".bin", response.ResponseBytes);
                    break;

                case "json":
                    await HttpMethods.ExtractResultAsync(response);

                    break;

                default:
                    break;
                }
                return(response);
            }
            throw new Exception("GenericServiceAsync: url: no match for request: type:" + service.classInterface);
        }
        public static string ParseAnalyzerStringized; // Parse service only

        public override async System.Threading.Tasks.Task <CallServiceResponse <IGenericServiceResponse> > CallServiceAsync(string text, System.Collections.Generic.Dictionary <string, string> apiArgs)
        {
            // so far text and apiArgs are unused. The purpose of this call is to make a one-time invocation to retrieve Microsoft's parse analyzer IDs.
            Log.WriteLine("MicrosoftCognitiveParseAnalyzersServices: CallServiceAsync");
            CallServiceResponse <IGenericServiceResponse> response = new CallServiceResponse <IGenericServiceResponse>(service);

            response.Request = Array.Find(service.requests, p => p.argType == "text");
            await HttpMethods.CallApiAsync(response, text, apiArgs);

#if true // TODO: implement ExtractResultAsync?
            await HttpMethods.ExtractResultAsync(response);

            ParseAnalysers          = response.ResponseResult.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); // split at ", " into array
            ParseAnalyzerStringized = " \"" + string.Join("\", \"", ParseAnalysers) + "\" ";                                    // rejoing array into a string. each item must be quoted and separated by a comma.
#else
            ParseAnalysers = new System.Collections.Generic.List <string>();
            foreach (JToken tokAnalyzerResult in response.ResponseJToken)
            {
                ParseAnalysers.Add(tokAnalyzerResult["id"].ToString());
                ParseAnalyzerStringized += "'" + tokAnalyzerResult["id"].ToString() + "', ";
            }
            ParseAnalyzerStringized = ParseAnalyzerStringized.Substring(0, ParseAnalyzerStringized.Length - 2); // remove trailing "', "
#endif
            return(response);
        }
        public override async System.Threading.Tasks.Task <CallServiceResponse <IGenericServiceResponse> > CallServiceAsync(string text, System.Collections.Generic.Dictionary <string, string> apiArgs)
        {
            Log.WriteLine("MicrosoftCognitiveParseServices: Text:" + text);
            if (MicrosoftCognitiveParseAnalyzersServices.ParseAnalysers == null) // calling ParseAnalyzersService. It's a dependency of ParseService.
            {
                CallServiceResponse <IGenericServiceResponse> responseAnalyzers    = new CallServiceResponse <IGenericServiceResponse>(service);
                MicrosoftCognitiveParseAnalyzersServices      parseAnalyersService = new MicrosoftCognitiveParseAnalyzersServices(Options.services["MicrosoftCognitiveParseAnalyzersService"].service);
                responseAnalyzers = await parseAnalyersService.CallServiceAsync(text, apiArgs);

                if (MicrosoftCognitiveParseAnalyzersServices.ParseAnalysers == null || MicrosoftCognitiveParseAnalyzersServices.ParseAnalysers.Length == 0 || string.IsNullOrWhiteSpace(MicrosoftCognitiveParseAnalyzersServices.ParseAnalyzerStringized))
                {
                    throw new InvalidOperationException(); // can't continue without at least one Analyzer
                }
            }
            CallServiceResponse <IGenericServiceResponse> response = new CallServiceResponse <IGenericServiceResponse>(service);

            response.Request = Array.Find(service.requests, p => p.argType == "text");
            System.Collections.Generic.List <Tuple <string, string> > jsonSubstitutes = new System.Collections.Generic.List <Tuple <string, string> >()
            {
                new Tuple <string, string>("{AnalyzerStringized}", MicrosoftCognitiveParseAnalyzersServices.ParseAnalyzerStringized),
            };
            await HttpMethods.CallApiAsync(response, null, null, jsonSubstitutes, text, apiArgs); // example of using jsonSubstitutes

            await HttpMethods.ExtractResultAsync(response);

            Newtonsoft.Json.Linq.JArray arrayOfResults = Newtonsoft.Json.Linq.JArray.Parse(response.ResponseResult);
            foreach (Newtonsoft.Json.Linq.JToken s in arrayOfResults)
            {
                ConstituencyTreeNode root = ParseHelpers.ConstituencyTreeFromText(s.ToString());
                string textTree           = ParseHelpers.TextFromConstituencyTree(root);
                if (textTree != s.ToString())
                {
                    throw new FormatException();
                }
                string   words      = ParseHelpers.WordsFromConstituencyTree(root);
                string[] printLines = ParseHelpers.FormatConstituencyTree(root);
                foreach (string p in printLines)
                {
                    Console.WriteLine(p);
                }
            }
            return(response);
        }
示例#4
0
        public override async System.Threading.Tasks.Task <CallServiceResponse <IGenericServiceResponse> > CallServiceAsync(byte[] audioBytes, System.Collections.Generic.Dictionary <string, string> apiArgs)
        {
            int sampleRate = int.Parse(apiArgs["sampleRate"]);

            Log.WriteLine("audio file length:" + audioBytes.Length + " sampleRate:" + sampleRate);
            CallServiceResponse <IGenericServiceResponse> response = new CallServiceResponse <IGenericServiceResponse>(service);

            response.Request = Array.Find(service.requests, p => p.argType == "binary");

            await HttpMethods.CallApiAsync(response, null, null, null, audioBytes, apiArgs);

            string JobID = response.ResponseJToken.SelectToken(".jobID").ToString();
            Dictionary <string, string> dict = Helpers.stringToDictionary(service.requests[0].data.value, '&', '=');
            string ApiKey = dict["apikey"];
            // TODO: move url to settings file
            string JobUrl = $"https://api.havenondemand.com/1/job/result/{JobID}?apikey={ApiKey}"; // using C# 6.0 string interpolation
            await HttpMethods.CallApiAuthAsync(response, new Uri(JobUrl), "", null);

            await HttpMethods.ExtractResultAsync(response);

            return(response);
        }
示例#5
0
        private static JToken IntentConversationState = null; // need this to expire
                                                              // override is working but somewhat verbose. Need to explore other methods such as passing Type? Need to combine Command->Run->Call as they're all the same Type. Make tail of Command into virtual.
                                                              //public override async System.Threading.Tasks.Task<GenericCallServiceResponse<IHoundifyServiceResponse>> CallServiceAsync(byte[] bytes, System.Collections.Generic.Dictionary<string, string> apiArgs)

        public static async System.Threading.Tasks.Task HoundifyPostAsync(Settings.Service service, ServiceResponse response, Uri uri, byte[] RequestContentBytes, System.Collections.Generic.Dictionary <string, string> apiArgs)
        {
            Log.WriteLine("Content length:" + RequestContentBytes.Length);

            string  ClientID  = service.requests[0].headers[0].HoundifyAuthentication.ClientID;  // moot? throws Exception thrown: 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' in Microsoft.CSharp.dll
            string  ClientKey = service.requests[0].headers[0].HoundifyAuthentication.ClientKey; // moot? Exception thrown: 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' in Microsoft.CSharp.dll
            string  UserID    = service.requests[0].headers[0].HoundifyAuthentication.UserID;
            JObject RequestBodyObject;

            RequestBodyObject = JObject.FromObject(new
            {
                Latitude  = GeoLocation.latitude,
                Longitude = GeoLocation.longitude,
                City      = GeoLocation.town,
                Country   = GeoLocation.country,
                UserID    = UserID,
                ClientID  = ClientID,
                // audio specific
                PartialTranscriptsDesired = Options.services["HoundifyIntentAudioService"].service.requests[0].PartialTranscriptsDesired,
                //ConversationState = IntentConversationState,
            });
            if (IntentConversationState != null)
            {
                RequestBodyObject.Add(IntentConversationState);
            }

            string RequestBodyJson = JsonConvert.SerializeObject(RequestBodyObject); // no formatting. Could use ToString() but it formats (spaces, EOL).

            if (Options.options.debugLevel >= 4)
            {
                Log.WriteLine("RequestBodyJson:" + RequestBodyJson);
            }

            byte[] RequestBodyBytes = System.Text.Encoding.UTF8.GetBytes(RequestBodyJson).Concat(RequestContentBytes).ToArray();
            //byte[] RequestBodyBytes = System.Text.Encoding.UTF8.GetBytes(RequestBodyJson).Concat(ReadBytesFromFile("wb_male.wav")).ToArray();

            string   RequestID = System.Guid.NewGuid().ToString("D"); // Houndify requires lower case?
            TimeSpan t         = DateTime.UtcNow - new DateTime(1970, 1, 1);
            string   Timestamp = Math.Floor(t.TotalSeconds).ToString();

            string FriendlyClientKey = ClientKey.Replace('-', '+').Replace('_', '/'); // translate possible PHP encode style to FromBase64String style

#if WINDOWS_UWP
            Windows.Storage.Streams.IBuffer KeyBytes     = Windows.Security.Cryptography.CryptographicBuffer.DecodeFromBase64String(FriendlyClientKey);
            Windows.Storage.Streams.IBuffer MessageBytes = Windows.Security.Cryptography.CryptographicBuffer.ConvertStringToBinary(UserID + ";" + RequestID + Timestamp, Windows.Security.Cryptography.BinaryStringEncoding.Utf8);
            Windows.Security.Cryptography.Core.MacAlgorithmProvider MacAlg  = Windows.Security.Cryptography.Core.MacAlgorithmProvider.OpenAlgorithm(Windows.Security.Cryptography.Core.MacAlgorithmNames.HmacSha256);
            Windows.Security.Cryptography.Core.CryptographicHash    MacHash = MacAlg.CreateHash(KeyBytes);
            MacHash.Append(MessageBytes);
            Windows.Storage.Streams.IBuffer MacHashBuf = MacHash.GetValueAndReset();
            string HashSignature = Windows.Security.Cryptography.CryptographicBuffer.EncodeToBase64String(MacHashBuf).Replace('+', '-').Replace('/', '_');
#else
            byte[] KeyBytes      = Convert.FromBase64String(FriendlyClientKey);                                     // TODO: shouldn't this be System.Text.Encoding.UTF8.GetBytes?
            byte[] MessageBytes  = System.Text.Encoding.UTF8.GetBytes(UserID + ";" + RequestID + Timestamp);
            byte[] HashBytes     = new System.Security.Cryptography.HMACSHA256(KeyBytes).ComputeHash(MessageBytes); // always length of 32?
            string HashSignature = Convert.ToBase64String(HashBytes).Replace('+', '-').Replace('/', '_');
#endif
            string HoundRequestAuthentication = UserID + ";" + RequestID;
            string HoundClientAuthentication  = ClientID + ";" + Timestamp + ";" + HashSignature;

            if (Options.options.debugLevel >= 4)
            {
                Log.WriteLine("Uri:" + uri);
                Log.WriteLine("UserID:" + UserID);
                Log.WriteLine("RequestID:" + RequestID);
                Log.WriteLine("Timestamp:" + Timestamp);
                Log.WriteLine("ClientID:" + ClientID);
                Log.WriteLine("ClientKey:" + ClientKey);
                Log.WriteLine("FriendlyClientKey:" + FriendlyClientKey);
                Log.WriteLine("HashSignature:" + HashSignature);
                Log.WriteLine("HoundRequestAuthentication:" + HoundRequestAuthentication);
                Log.WriteLine("HoundClientAuthentication:" + HoundClientAuthentication);
            }
#if true
            // TODO: put curl into woundifysettings.json?
            Log.WriteLine("curl -X POST" + " --data-binary @computer.wav" + " --header \"Hound-Request-Authentication:" + HoundRequestAuthentication + "\" --header \"Hound-Client-Authentication:" + HoundClientAuthentication + "\" --header \"Hound-Request-Info:" + RequestBodyJson.Replace('"', '\'') + "\" " + uri);
#endif
            System.Collections.Generic.List <Tuple <string, string> > headers = new System.Collections.Generic.List <Tuple <string, string> >()
            {
                new Tuple <string, string>("Hound-Request-Info-Length", RequestBodyJson.Length.ToString()),
                new Tuple <string, string>("Hound-Request-Authentication", HoundRequestAuthentication),
                new Tuple <string, string>("Hound-Client-Authentication", HoundClientAuthentication)
            };
            await HttpMethods.CallApiAsync(response, uri, RequestBodyBytes, apiArgs, headers);

            ProcessResponse(response.ResponseJToken);
            await HttpMethods.ExtractResultAsync(response);
        }