.Net API Wrapper

Is there a .Net API wrapper for calling the machine translate API?  It would prove very useful.  The only documentation provided is a CURL request.  At the very least, I need a .Net example to follow specifically showing how to make the call.  I have setup some code to invoke the REST service but it is failing. Simple examples in calling sample service don't really help. I need a real-world example showing specifically how to make the BeGlobal call within .Net.  I can include my current code if desired.

That said, can someone please help me out.  Feeling very frustrated and need to get specific help to start using the product.

Thanks

Parents Reply Children
  • Try this in a new Console Application. You'll need to add some references:

    namespace LcApiExample

    {

        using System;

        using System.Net.Http;

        using System.Net.Http.Headers;

        using System.Runtime.Serialization.Json;

        using System.Text;

        using System.Xml;

        using System.Xml.Linq;

        using System.Xml.XPath;

     

        class Program

        {

            static void Main(string[] args)

            {

                string apiKey = "[ENTER YOUR API KEY HERE]";

                string sourceText = "Hello Developers";

                string sourceLanguage = "eng";

                string targetLanguage = "fra";

                string json = string.Format(@"{{""text"":""{0}"",""from"":""{1}"",""to"":""{2}""}}", sourceText, sourceLanguage, targetLanguage);

                var client = new HttpClient();

                var requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://lc-api.sdl.com/translate");

                requestMessage.Headers.Add("Accept", "application/json");

                requestMessage.Headers.Add("Authorization", string.Format("LC apiKey={0}", apiKey));

                requestMessage.Content = new ByteArrayContent(Encoding.UTF8.GetBytes(json));

                requestMessage.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=UTF-8");

                HttpResponseMessage response = client.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead).Result;

                if (response.IsSuccessStatusCode)

                {

                    byte[] responseBytes = response.Content.ReadAsByteArrayAsync().Result;

                    XmlReader reader = JsonReaderWriterFactory.CreateJsonReader(responseBytes, new XmlDictionaryReaderQuotas());

                    XElement root = XElement.Load(reader);

                    XElement translation = root.XPathSelectElement("//translation");

                    Console.WriteLine(translation.Value);

                }

                else

                {

                    Console.WriteLine("#ERROR: " + Encoding.UTF8.GetString(response.Content.ReadAsByteArrayAsync().Result));

                }

     

                Console.ReadKey();

            }

        }

    }

     

    David Pooley | Senior Product Manager | SDL