Beispiel #1
0
        private List <T> PostAnalyticData <T>(string url, string data)
        {
            AuthResponse authResponse = this.RunRequest(QueryMethod.POST, url, data);

            int    status  = (int)authResponse.Status;
            string message = authResponse.Data;

            List <T> obj = new List <T>();

            switch (status)
            {
            case 200:
            {
                switch (_format)
                {
                case "json":
                {
                    List <T> result = this.ProcessGetResponse <List <T> >(authResponse);
                    if (result != null)
                    {
                        obj = result;
                    }
                }
                break;

                case "xml":
                {
                    T result = this.ProcessGetResponse <T>(authResponse);
                    if (result != null)
                    {
                        obj = (List <T>)result.GetType().InvokeMember("Data", BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty, Type.DefaultBinder, result, null);
                    }
                }
                break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                return(obj);
            }

            case 202:
                return(obj);

            default:
                this.ResolveError <T>(authResponse);
                return(null);
            }
        }
Beispiel #2
0
        public dynamic GetSupportedFeatures(string language = null)
        {
            //GET https://api.semantria.com/features.json
            string url = String.Format("{0}/features.{1}", _host, _format);

            if (!String.IsNullOrEmpty(language))
            {
                url = String.Format("{0}/features.{1}?language={2}", _host, _format, language);
            }

            AuthResponse authResponse = this.RunRequest(QueryMethod.GET, url, null);
            dynamic      obj          = this.ProcessGetResponse(authResponse);

            return(obj);
        }
Beispiel #3
0
        private int Delete(QueryMethod method, dynamic items, string tag, string configId = null)
        {
            string url = String.Format("{0}/{1}.{2}", _host, tag, _format);

            if (!String.IsNullOrEmpty(configId))
            {
                url = String.Format("{0}/{1}.{2}?config_id={3}", _host, tag, _format, configId);
            }

            string data = _serializer.Serialize(items);

            AuthResponse authResponse = this.RunRequest(method, url, data);

            return(this.ProcessPostResponse(authResponse));
        }
Beispiel #4
0
        private AuthResponse WebRequest(QueryMethod method, string url, string postData, string authheader, bool isBinary)
        {
            HttpWebRequest webRequest    = null;
            StreamWriter   requestWriter = null;

            webRequest             = (HttpWebRequest)System.Net.WebRequest.Create(url);
            webRequest.Method      = method.ToString();
            webRequest.Credentials = CredentialCache.DefaultCredentials;

            webRequest.Headers.Add("x-app-name", _appName);
            webRequest.Headers.Add("x-api-version", _apiVersion);
            webRequest.Headers.Add("Authorization", authheader);

            if (_useCompression == true)
            {
                webRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
                webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            }

            if (method == QueryMethod.POST || method == QueryMethod.PUT || method == QueryMethod.DELETE)
            {
                webRequest.ContentType = "application/x-www-form-urlencoded";

                //Write the data.
                requestWriter = new StreamWriter(webRequest.GetRequestStream());
                try
                {
                    requestWriter.Write(postData);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    requestWriter.Close();
                    requestWriter = null;
                }
            }

            AuthResponse authResponse = WebResponseGet(webRequest, isBinary);

            authResponse.Method = method;

            webRequest = null;

            return(authResponse);
        }
Beispiel #5
0
        private int ProcessPostResponse(AuthResponse authResponse)
        {
            int    status  = (int)authResponse.Status;
            string message = authResponse.Data;

            switch (status)
            {
            case 200:
            case 202:
                return(status);

            default:
                this.ResolveError(authResponse);
                return(status);
            }
        }
Beispiel #6
0
 private static void readProtocolError(WebException e, AuthResponse authResponse)
 {
     using (HttpWebResponse response = (HttpWebResponse)e.Response)
     {
         authResponse.Status = response.StatusCode;
         authResponse.Data   = response.StatusDescription;
         using (Stream stream = response.GetResponseStream())
         {
             if (null != stream && stream.CanRead)
             {
                 using (StreamReader responseReader = new StreamReader(stream))
                 {
                     authResponse.Data = responseReader.ReadToEnd();
                 }
             }
         }
     }
 }
Beispiel #7
0
        private T ProcessGetResponse<T>(AuthResponse authResponse)
        {
            int status = (int)authResponse.Status;
            string message = authResponse.Data;
            T obj = default(T);

            switch (status)
            {
                case 200:
                    obj = ConvertResponse<T>(message);
                    return obj;
                case 202:
                    return obj;
                default:
                    ResolveError(authResponse);
                    return obj;
            }
        }
Beispiel #8
0
        private AuthResponse WebRequest(QueryMethod method, string url, string postData, string authheader)
        {
            HttpWebRequest webRequest    = null;
            StreamWriter   requestWriter = null;

            webRequest             = (HttpWebRequest)System.Net.WebRequest.Create(url);
            webRequest.Method      = method.ToString();
            webRequest.Credentials = CredentialCache.DefaultCredentials;

            webRequest.Headers.Add("x-api-version", "2.1");
            webRequest.Headers.Add("x-app-name", _appName);

            //Add authorization header
            webRequest.Headers.Add("Authorization", authheader);

            if (method == QueryMethod.POST)
            {
                webRequest.ContentType = "application/x-www-form-urlencoded";

                //POST the data.
                requestWriter = new StreamWriter(webRequest.GetRequestStream());
                try
                {
                    requestWriter.Write(postData);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    requestWriter.Close();
                    requestWriter = null;
                }
            }

            AuthResponse authResponse = WebResponseGet(webRequest);

            authResponse.Method = method;

            webRequest = null;

            return(authResponse);
        }
Beispiel #9
0
        private dynamic ProcessGetResponse(AuthResponse authResponse)
        {
            int     status  = (int)authResponse.Status;
            string  message = authResponse.Data;
            dynamic obj     = null;

            switch (status)
            {
            case 200:
                obj = _serializer.Deserialize(message);
                return(obj);

            case 202:
                return(obj);

            default:
                this.ResolveError(authResponse);
                return(obj);
            }
        }
Beispiel #10
0
        private int Update <T>(QueryMethod method, List <T> items, string configId = null)
        {
            string tag = this.GetTag <T>();

            string url = String.Format("{0}/{1}.{2}", _host, tag, _format);

            if (!String.IsNullOrEmpty(configId))
            {
                url = String.Format("{0}/{1}.{2}?config_id={3}", _host, tag, _format, configId);
            }

            string data = null;

            if (method == QueryMethod.DELETE)
            {
                IStub <T> xstub = this.GetStub <T>(items) as IStub <T>;
                xstub.ToKeys();
                if (_format == "xml")
                {
                    data = _serializer.Serialize(xstub);
                    data = xstub.ConvertTag(data);
                }
                else
                {
                    data = _serializer.Serialize(xstub.Keys);
                }
            }
            else
            {
                object stub = items;
                if (_format == "xml")
                {
                    stub = this.GetStub <T>(items);
                }
                data = _serializer.Serialize(stub);
            }

            AuthResponse authResponse = this.RunRequest(method, url, data);

            return(this.ProcessPostResponse <T>(authResponse));
        }
Beispiel #11
0
        public IList <FeaturesSet> GetSupportedFeatures(string language = null)
        {
            //GET https://api.semantria.com/features.json
            string url = String.Format("{0}/features.{1}", _host, _format);

            if (!String.IsNullOrEmpty(language))
            {
                url = String.Format("{0}/features.{1}?language={2}", _host, _format, language);
            }

            AuthResponse       authResponse = this.RunRequest(QueryMethod.GET, url, null);
            List <FeaturesSet> obj          = new List <FeaturesSet>();

            switch (_format)
            {
            case "json":
            {
                List <FeaturesSet> result = this.ProcessGetResponse <List <FeaturesSet> >(authResponse);
                if (result != null)
                {
                    obj = result;
                }
            }
            break;

            case "xml":
            {
                FeaturesSetList result = this.ProcessGetResponse <FeaturesSetList>(authResponse);
                if (result != null)
                {
                    obj = result.Data;
                }
            }
            break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(obj);
        }
Beispiel #12
0
        public dynamic GetStatistics(string configId = null, string interval = null)
        {
            //GET https://api.semantria.com/statistics.json
            string url = String.Format("{0}/statistics.{1}", _host, _format);

            if (!String.IsNullOrEmpty(configId) && !String.IsNullOrEmpty(interval))
            {
                url = String.Format("{0}/statistics.{1}?config_id={2}&interval={3}", _host, _format, configId, interval);
            }
            else if (!String.IsNullOrEmpty(configId))
            {
                url = String.Format("{0}/statistics.{1}?config_id={2}", _host, _format, configId);
            }
            else if (!String.IsNullOrEmpty(interval))
            {
                url = String.Format("{0}/statistics.{1}?interval={2}", _host, _format, interval);
            }

            AuthResponse authResponse = this.RunRequest(QueryMethod.GET, url, null);

            return(this.ProcessGetResponse(authResponse));
        }
Beispiel #13
0
        private AuthResponse WebResponseGet(HttpWebRequest webRequest, bool isBinary)
        {
            AuthResponse authResponse = new AuthResponse();

            try
            {
                readGoodResponse(webRequest, authResponse, isBinary);
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    readProtocolError(e, authResponse);
                }
                else
                {
                    authResponse.Status = HttpStatusCode.BadRequest;
                    authResponse.Data   = e.Message;
                }
            }

            return(authResponse);
        }
Beispiel #14
0
        private List<UserEntity> RequestEntity(string url)
        {
            AuthResponse authResponse = this.RunRequest(QueryMethod.GET, url, null);
            List<UserEntity> obj = new List<UserEntity>();

            switch (_format)
            {
                case "json":
                    {
                        List<UserEntity> result = ProcessGetResponse<List<UserEntity>>(authResponse);
                        if (result != null)
                        {
                            foreach (UserEntity item in result)
                            {
                                obj.Add(item);
                            }
                        }
                    }
                    break;
                case "xml":
                    {
                        UserEntities result = ProcessGetResponse<UserEntities>(authResponse);
                        if (result != null)
                        {
                            foreach (UserEntity item in result.Data)
                            {
                                obj.Add(item);
                            }
                        }
                    }
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            return obj;
        }
Beispiel #15
0
        /// <summary>
        /// Sign a web request using Auth.
        /// </summary>
        /// <param name="method">Http method GET, POST or DELETE</param>
        /// <param name="url">The full url, including the querystring.</param>
        /// <param name="postData">Data to post in xml or json fromat</param>
        /// <returns>The web server response.</returns>
        public AuthResponse AuthWebRequest(QueryMethod method, string url, string postData, bool isBinary)
        {
            string querystring = string.Empty;
            string authheader  = string.Empty;

            Uri uri = new Uri(url);

            string nonce     = this.GenerateNonce();
            string timeStamp = this.GenerateTimeStamp();

            authheader = this.GenerateAuthorizationHeader(uri,
                                                          method.ToString(),
                                                          timeStamp,
                                                          nonce);

            querystring = this.GenerateQueryString(uri,
                                                   method.ToString(),
                                                   timeStamp,
                                                   nonce);

            AuthResponse authResponse = WebRequest(method, querystring, postData, authheader, isBinary);

            return(authResponse);
        }
Beispiel #16
0
 private void ResolveError(AuthResponse authResponse)
 {
     int status = (int)authResponse.Status;
     string message = authResponse.Data;
     OnError(this, new ResponseErrorEventArgs(null, authResponse.Status, message));
 }
Beispiel #17
0
        public dynamic GetTaxonomy(string configId = null)
        {
            AuthResponse authResponse = this.Get("taxonomy", configId);

            return(this.ProcessGetResponse(authResponse));
        }
Beispiel #18
0
        public dynamic GetSentimentPhrases(string configId = null)
        {
            AuthResponse authResponse = this.Get("phrases", configId);

            return(this.ProcessGetResponse(authResponse));
        }
Beispiel #19
0
        public dynamic GetEntities(string configId = null)
        {
            AuthResponse authResponse = this.Get("entities", configId);

            return(this.ProcessGetResponse(authResponse));
        }
Beispiel #20
0
        public dynamic GetQueries(string configId = null)
        {
            AuthResponse authResponse = Get("queries", configId);

            return(ProcessGetResponse(authResponse));
        }
Beispiel #21
0
        public byte[] GetUserDirectory(string configId, string format)
        {
            AuthResponse authResponse = GetBinary("salience/user-directory." + format, configId);

            return(authResponse.BinaryData);
        }
Beispiel #22
0
        private dynamic ProcessGetResponse(AuthResponse authResponse)
        {
            int status = (int)authResponse.Status;
            string message = authResponse.Data;
            dynamic obj = null;

            switch (status)
            {
                case 200:
                    obj = _serializer.Deserialize(message);
                    return obj;
                case 202:
                    return obj;
                default:
                    this.ResolveError(authResponse);
                    return obj;
            }
        }
Beispiel #23
0
        private int ProcessPostResponse(AuthResponse authResponse)
        {
            int status = (int)authResponse.Status;
            string message = authResponse.Data;

            switch (status)
            {
                case 200:
                case 202:
                    return status;
                default:
                    this.ResolveError(authResponse);
                    return status;
            }
        }
Beispiel #24
0
        private AuthResponse WebResponseGet(HttpWebRequest webRequest)
        {
            StreamReader responseReader = null;
            AuthResponse authResponse = new AuthResponse();
            Stream stream = null;

            try
            {
                HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
                using (response)
                {
                    authResponse.Status = response.StatusCode;
                    stream = response.GetResponseStream();
                    responseReader = new StreamReader(stream);
                    authResponse.Data = responseReader.ReadToEnd();
                }
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    HttpWebResponse response = (HttpWebResponse)e.Response;
                    using (response)
                    {
                        authResponse.Status = response.StatusCode;
                        authResponse.Data = response.StatusDescription;

                        stream = response.GetResponseStream();
                        if (null != stream && stream.CanRead)
                        {
                            responseReader = new StreamReader(stream);
                            authResponse.Data = responseReader.ReadToEnd();
                        }
                    }
                }
                else
                {
                    authResponse.Status = HttpStatusCode.BadRequest;
                    authResponse.Data = e.Message;
                }
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
                if (responseReader != null)
                {
                    responseReader.Close();
                    responseReader = null;
                }
            }

            return authResponse;
        }
Beispiel #25
0
 private void ResolveError(AuthResponse authResponse)
 {
     int status = (int)authResponse.Status;
     string message = authResponse.Data;
     OnError(this, new ResponseEventArgs(authResponse.Status, message));
 }