Beispiel #1
0
        public static JObject updateContent(JObject paramsObject, String path)
        {
            //the same as in the createContent method
            String securityToken = NetmeraClient.getSecurityToken();

            if (securityToken != null && securityToken.Trim() != "")
            {
                StringBuilder strBuild = new StringBuilder()
                                         .Append(NetmeraConstants.Netmera_Domain_Url)
                                         .Append(NetmeraConstants.Netmera_Domain_Rest_Url)
                                         .Append(NetmeraConstants.Netmera_UpdateContent_Url)
                                         .Append(NetmeraConstants.SecurityToken_Params).Append("=").Append(securityToken).Append("&")
                                         .Append(NetmeraConstants.ContentType_Params).Append("=").Append(NetmeraConstants.Default_ContentType).Append("&")
                                         .Append(NetmeraConstants.Path_Params).Append("=").Append(path).Append("&")
                                         .Append(NetmeraConstants.ContentPrivacy_Params).Append("=").Append(paramsObject[NetmeraConstants.ContentPrivacy_Params]).Append("&")
                                         .Append(NetmeraConstants.Service_Params).Append("=").Append(NetmeraConstants.Default_Service).Append("&")
                                         .Append(NetmeraConstants.Action_Params).Append("=").Append(NetmeraConstants.Update_Action).Append("&")
                                         .Append(NetmeraConstants.Content_Params).Append("=").Append(JsonConvert.SerializeObject(paramsObject));

                JObject responseHttp = sendHttp(strBuild.ToString(), NetmeraConstants.Http_Method_Post);
                return((JObject)responseHttp[NetmeraConstants.Entry_Params]);
            }
            else
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_API_KEY_NOT_FOUND, "You didn't set your api key. Please use NetmeraClient.init(apiKey).");
            }
        }
Beispiel #2
0
        public static JObject locationSearch(RequestItem item)
        {
            String securityToken = NetmeraClient.getSecurityToken();

            if (securityToken != null && securityToken.Trim() != "")
            {
                StringBuilder strBuild = new StringBuilder()
                                         .Append(NetmeraConstants.Netmera_Domain_Url)
                                         .Append(NetmeraConstants.Netmera_Domain_Rest_Url)
                                         .Append(NetmeraConstants.Netmera_LocationSearchContent_Url)
                                         .Append(NetmeraConstants.SecurityToken_Params).Append("=").Append(securityToken).Append("&")
                                         .Append(NetmeraConstants.Path_Params).Append("=").Append(NetmeraConstants.Default_ParentPath).Append("&")
                                         .Append(NetmeraConstants.ContentType_Params).Append("=").Append(NetmeraConstants.Default_ContentType).Append("&")
                                         .Append(NetmeraConstants.CustomCondition_Params).Append("=").Append(item.customCondition).Append("&")
                                         .Append(NetmeraConstants.SearchText_Params).Append("=").Append(item.searchText).Append("&")
                                         .Append(NetmeraConstants.Max_Params).Append("=").Append(item.max).Append("&")
                                         .Append(NetmeraConstants.Page_Params).Append("=").Append(item.page).Append("&")
                                         .Append(NetmeraConstants.SortBy_Params).Append("=").Append(item.sortBy).Append("&")
                                         .Append(NetmeraConstants.SortOrder_Params).Append("=").Append(item.sortOrder).Append("&")
                                         .Append(NetmeraConstants.LocationSearchType_Params).Append("=").Append(item.locationSearchType).Append("&")
                                         .Append(NetmeraConstants.LocationSearchField_Params).Append("=").Append(item.locationSearchField + NetmeraConstants.LocationField_Suffix).Append("&")
                                         .Append(NetmeraConstants.LocationLatitude_Params).Append("=").Append(item.latitudes).Append("&")
                                         .Append(NetmeraConstants.LocationLongitude_Params).Append("=").Append(item.longitudes).Append("&")
                                         .Append(NetmeraConstants.LocationDistance_Params).Append("=").Append(item.distance).Append("&");

                return(sendHttp(strBuild.ToString(), NetmeraConstants.Http_Method_Post));
            }
            else
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_API_KEY_NOT_FOUND, "You didn't set your api key. Please use NetmeraClient.init(apiKey).");
            }
        }
Beispiel #3
0
        public static JObject createBulkContent(JObject paramsObject)
        {
            //security token is stored in a temporary variable because createActionToken resets the security token
            //inside NetmeraClien due to the method sendHttp which contains NetmeraClient.finish();
            String securityToken = NetmeraClient.getSecurityToken();

            if (securityToken != null && securityToken.Trim() != "")
            {
                long contentNameRnd = currentTimeMillis();

                StringBuilder strBuild = new StringBuilder()
                                         .Append(NetmeraConstants.Netmera_Domain_Url)
                                         .Append(NetmeraConstants.Netmera_Domain_Rpc_Url)
                                         //.Append(NetmeraConstants.Netmera_CreateBulkContent_Url)
                                         .Append(NetmeraConstants.SecurityToken_Params).Append("=").Append(securityToken);
                //.Append("&")
                //.Append(NetmeraConstants.ContentType_Params).Append("=").Append(NetmeraConstants.Default_ContentType).Append("&")
                //.Append(NetmeraConstants.ContentName_Params).Append("=").Append(contentNameRnd).Append("&")
                //.Append(NetmeraConstants.ContentPrivacy_Params).Append("=").Append(paramsObject[NetmeraConstants.ContentPrivacy_Params]).Append("&")
                //.Append(NetmeraConstants.Path_Params).Append("=").Append(NetmeraConstants.Default_ParentPath).Append("&")
                //.Append(NetmeraConstants.Service_Params).Append("=").Append(NetmeraConstants.Default_Service).Append("&")
                //.Append(NetmeraConstants.Action_Params).Append("=").Append(NetmeraConstants.Create_Action).Append("&")
                //.Append(NetmeraConstants.Content_Params);
                //.Append("=").Append(JsonConvert.SerializeObject(paramsObject));
                String strBody = JsonConvert.SerializeObject(paramsObject);

                JObject responseHttp = sendHttpWithJson(strBuild.ToString(), strBody, NetmeraConstants.Http_Method_Post);
                return(responseHttp);
            }
            else
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_API_KEY_NOT_FOUND, "You didn't set your api key. Please use NetmeraClient.init(apiKey).");
            }
        }
Beispiel #4
0
        public static JObject accountUpdate(RequestItem item)
        {
            String securityToken = NetmeraClient.getSecurityToken();

            if (securityToken != null && securityToken.Trim() != "")
            {
                StringBuilder strBuildForJson = new StringBuilder()
                                                .Append("{\"" + NetmeraConstants.Params_Params + "\":{")
                                                .Append("\"" + NetmeraConstants.Netmera_UserEmail + "\" : \"" + item.getEmail() + "\"").Append(",")
                                                .Append("\"" + NetmeraConstants.Netmera_UserPassword + "\" : \"" + item.getPassword() + "\"").Append(",")
                                                .Append("\"" + NetmeraConstants.Netmera_UserName + "\" : \"" + item.getName() + "\"").Append(",")
                                                .Append("\"" + NetmeraConstants.Netmera_UserSurname + "\" : \"" + item.getSurname() + "\"")
                                                .Append(" },\"" + NetmeraConstants.Method_Params + "\":\"" + NetmeraConstants.Netmera_PeopleAccountUpdate_Method + "\"}");


                StringBuilder strBuildForUrl = new StringBuilder()
                                               .Append(NetmeraConstants.Netmera_Domain_Url)
                                               .Append(NetmeraConstants.Netmera_Domain_Rpc_Url)
                                               .Append(NetmeraConstants.SecurityToken_Params).Append("=").Append(securityToken);

                return(sendHttpWithJson(strBuildForUrl.ToString(), strBuildForJson.ToString(), NetmeraConstants.Http_Method_Post));
            }
            else
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_API_KEY_NOT_FOUND, "You didn't set your api key. Please use NetmeraClient.init(apiKey).");
            }
        }
Beispiel #5
0
        public static JObject searchUser(RequestItem item)
        {
            String securityToken = NetmeraClient.getSecurityToken();

            if (securityToken != null && securityToken.Trim() != "")
            {
                StringBuilder strBuild = new StringBuilder()
                                         .Append(NetmeraConstants.Netmera_Domain_Url)
                                         .Append(NetmeraConstants.Netmera_Domain_Rest_Url)
                                         .Append(NetmeraConstants.Netmera_PeopleSearch_Url)
                                         .Append(NetmeraConstants.SecurityToken_Params).Append("=").Append(securityToken).Append("&")
                                         .Append(NetmeraConstants.Path_Params).Append("=").Append(item.path).Append("&")
                                         .Append(NetmeraConstants.SearchText_Params).Append("=").Append(item.searchText).Append("&")
                                         .Append(NetmeraConstants.CustomCondition_Params).Append("=").Append(item.customCondition).Append("&")
                                         .Append(NetmeraConstants.Max_Params).Append("=").Append(item.max).Append("&")
                                         .Append(NetmeraConstants.Page_Params).Append("=").Append(item.page).Append("&")
                                         .Append(NetmeraConstants.SortBy_Params).Append("=").Append(item.sortBy).Append("&")
                                         .Append(NetmeraConstants.SortOrder_Params).Append("=").Append(item.sortOrder).Append("&")
                                         .Append(NetmeraConstants.Filter_Params).Append("=").Append(item.filterBy).Append("&")
                                         .Append(NetmeraConstants.FilterValue_Params).Append("=").Append(item.filterValue).Append("&")
                                         .Append(NetmeraConstants.FilterOperation_Params).Append("=").Append(item.filterOperation).Append("&");

                return(sendHttp(strBuild.ToString(), NetmeraConstants.Http_Method_Post));
            }
            else
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_API_KEY_NOT_FOUND, "You didn't set your api key. Please use NetmeraClient.init(apiKey).");
            }
        }
Beispiel #6
0
        private JObject createMedia(NetmeraContent content)
        {
            JObject json = null;

            try
            {
                JObject mediaObject      = new JObject();
                bool    isMediaFileAdded = false;

                IEnumerable <JProperty> jlist = mediaData.Properties();

                foreach (JProperty token in jlist)
                {
                    String key = token.Name;
                    String tmpMediaDataJson = mediaData.Value <String>(key);

                    byte[] tmpMediaData = JsonConvert.DeserializeObject <byte[]>(tmpMediaDataJson);

                    NetmeraMedia file = new NetmeraMedia(tmpMediaData);

                    isMediaFileAdded = true;

                    String appId       = content.getContent().Value <string>(NetmeraConstants.Site);
                    String apiKey      = NetmeraClient.getSecurityToken();
                    String contentPath = content.getPath();
                    String viewerId    = content.getOwner().Value <String>(NetmeraConstants.Netmera_Owner_Node_Name);
                    String mediaUrl    = file.save(appId, apiKey, contentPath, viewerId);

                    mediaObject.Add(key, mediaUrl);
                }
                if (isMediaFileAdded)
                {
                    json = NetmeraHttpUtils.updateContent(mediaObject, content.getPath());
                }
            }
            catch (JsonException)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_JSON, "Json in the response of creating media file is invalid");
            }
            catch (WebException)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_REQUEST, "Request exception occurred while saving media data");
            }
            catch (IOException)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_IO_EXCEPTION, "IO Exception occurred while saving media data");
            }
            return(json);
        }
Beispiel #7
0
 /// <summary>
 /// Sets the owner to the content.
 /// </summary>
 /// <param name="user">Current logged user</param>
 public void setOwner(NetmeraUser user)
 {
     if (user != null)
     {
         if (user.GetType() == typeof(NetmeraUser))
         {
             NetmeraClient.setLoggedUserSecurityToken(NetmeraUser.securityToken);
         }
         else
         {
             throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_DATA_TYPE, "You should set the object with the type of NetmeraUser.");
         }
     }
     else
     {
         throw new NetmeraException(NetmeraException.ErrorCode.EC_NULL_EXCEPTION, "User cannot be null.You should set current logged user.");
     }
 }
Beispiel #8
0
        public static bool sendHttpWithJsonwithReturn(String url, String jsonCall, String method)
        {
            bool flag = false;

            try
            {
                StringBuilder strBuild = new StringBuilder(url);
                strBuild.Append("&").Append(NetmeraConstants.Netmera_SDK_Params).Append(NetmeraConstants.Netmera_SDK_Value);
                strBuild.Append("&").Append(NetmeraConstants.Netmera_SDKVERSION_Params).Append(NetmeraConstants.Netmera_SDKVERSION_Value);

                WebRequest webRequest = WebRequest.Create(url);
                webRequest.Method      = method;
                webRequest.ContentType = NetmeraConstants.Http_Content_Type;

                // it is for json call (add json body to the request. e.g. for user update operation)
                if (!String.IsNullOrEmpty(jsonCall))
                {
                    Stream       writerStream = ((HttpWebRequest)webRequest).GetRequestStream();
                    StreamWriter writer       = new StreamWriter(writerStream);
                    writer.Write(jsonCall);
                    writer.Flush();
                    writer.Close();
                }

                HttpWebResponse b = (HttpWebResponse)webRequest.GetResponse();

                Stream       readerStream = b.GetResponseStream();
                StreamReader uberReader   = new StreamReader(readerStream);
                String       response     = uberReader.ReadToEnd();
                b.Close();

                NetmeraClient.finish();
                if (response.IndexOf("true") != -1)
                {
                    flag = true;
                }
                return(flag);
            }
            catch (WebException)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_REQUEST, "A general Web exception occurred.");
            }
        }
Beispiel #9
0
        public static JObject get(RequestItem item)
        {
            String securityToken = NetmeraClient.getSecurityToken();

            if (securityToken != null && securityToken.Trim() != "")
            {
                StringBuilder strBuild = new StringBuilder()
                                         .Append(NetmeraConstants.Netmera_Domain_Url)
                                         .Append(NetmeraConstants.Netmera_Domain_Rest_Url)
                                         .Append(NetmeraConstants.Netmera_GetContent_Url)
                                         .Append(NetmeraConstants.SecurityToken_Params).Append("=").Append(securityToken).Append("&")
                                         .Append(NetmeraConstants.Path_Params).Append("=").Append(item.path);

                return(sendHttp(strBuild.ToString(), NetmeraConstants.Http_Method_Get));
            }
            else
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_API_KEY_NOT_FOUND, "You didn't set your api key. Please use NetmeraClient.init(apiKey).");
            }
        }
Beispiel #10
0
        public static JObject loginAsGuest()
        {
            String securityToken = NetmeraClient.getSecurityToken();

            if (securityToken != null && securityToken.Trim() != "")
            {
                StringBuilder strBuildForJson = new StringBuilder()
                                                .Append("{\"" + NetmeraConstants.Params_Params + "\":{")
                                                .Append(" },\"" + NetmeraConstants.Method_Params + "\":\"" + NetmeraConstants.Netmera_LoginGuestUser_Method + "\"}");

                StringBuilder strBuildForUrl = new StringBuilder()
                                               .Append(NetmeraConstants.Netmera_Domain_Url)
                                               .Append(NetmeraConstants.Netmera_Domain_Rpc_Url)
                                               .Append(NetmeraConstants.SecurityToken_Params).Append("=").Append(securityToken);

                return(sendHttpWithJson(strBuildForUrl.ToString(), strBuildForJson.ToString(), NetmeraConstants.Http_Method_Post));
            }
            else
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_API_KEY_NOT_FOUND, "You didn't set your api key. Please use NetmeraClient.init(apiKey).");
            }
        }
Beispiel #11
0
        public static bool deleteContent(String path)
        {
            bool   flag          = false;
            String securityToken = NetmeraClient.getSecurityToken();

            if (securityToken != null && securityToken.Trim() != "")
            {
                StringBuilder strBuild = new StringBuilder()
                                         .Append(NetmeraConstants.Netmera_Domain_Url)
                                         .Append(NetmeraConstants.Netmera_Domain_Rest_Url)
                                         .Append(NetmeraConstants.Netmera_RemoveContent_Url)
                                         .Append(NetmeraConstants.SecurityToken_Params).Append("=").Append(securityToken).Append("&")
                                         .Append(NetmeraConstants.Path_Params).Append("=").Append(path);

                flag = sendHttpwithReturn(strBuild.ToString(), NetmeraConstants.Http_Method_Post);
            }
            else
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_API_KEY_NOT_FOUND, "You didn't set your api key. Please use NetmeraClient.init(apiKey).");
            }
            return(flag);
        }
Beispiel #12
0
        public static void activateUser(RequestItem item)
        {
            String securityToken = NetmeraClient.getSecurityToken();

            if (securityToken != null && securityToken.Trim() != "")
            {
                StringBuilder strBuildForJson = new StringBuilder()
                                                .Append("{\"" + NetmeraConstants.Method_Params + "\":{")
                                                .Append("\"" + NetmeraConstants.Netmera_UserEmail + "\" : \"" + item.getEmail() + "\"")
                                                .Append(" },\"" + NetmeraConstants.Method_Params + "\":\"" + NetmeraConstants.Netmera_ActivateUser_Method + "\"}");

                StringBuilder strBuildForUrl = new StringBuilder()
                                               .Append(NetmeraConstants.Netmera_Domain_Url)
                                               .Append(NetmeraConstants.Netmera_Domain_Rpc_Url)
                                               .Append(NetmeraConstants.SecurityToken_Params).Append("=").Append(securityToken);

                sendHttpWithJson(strBuildForUrl.ToString(), strBuildForJson.ToString(), NetmeraConstants.Http_Method_Post);
            }
            else
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_API_KEY_NOT_FOUND, "You didn't set your api key. Please use NetmeraClient.init(apiKey).");
            }
        }
Beispiel #13
0
        public static JObject registerUser(RequestItem item)
        {
            String securityToken = NetmeraClient.getSecurityToken();

            if (securityToken != null && securityToken.Trim() != "")
            {
                StringBuilder strBuild = new StringBuilder()
                                         .Append(NetmeraConstants.Netmera_Domain_Url)
                                         .Append(NetmeraConstants.Netmera_Domain_Rest_Url)
                                         .Append(NetmeraConstants.Netmera_RegisterUser_Url)
                                         .Append(NetmeraConstants.SecurityToken_Params).Append("=").Append(securityToken).Append("&")
                                         .Append(NetmeraConstants.Netmera_UserEmail).Append("=").Append(item.getEmail()).Append("&")
                                         .Append(NetmeraConstants.Netmera_UserPassword).Append("=").Append(item.getPassword()).Append("&")
                                         .Append(NetmeraConstants.Netmera_UserNickname).Append("=").Append(item.getNickname()).Append("&")
                                         .Append(NetmeraConstants.Netmera_UserName).Append("=").Append(item.getName()).Append("&")
                                         .Append(NetmeraConstants.Netmera_UserSurname).Append("=").Append(item.getSurname());

                return(sendHttp(strBuild.ToString(), NetmeraConstants.Http_Method_Post));
            }
            else
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_API_KEY_NOT_FOUND, "You didn't set your api key. Please use NetmeraClient.init(apiKey).");
            }
        }
Beispiel #14
0
        /// <summary>
        /// Adds key,value pairs into the object. If the object contains key, the old value is replaced.
        /// </summary>
        /// <param name="key">Key to identify specified value</param>
        /// <param name="value">Value associates with the specified key</param>
        /// <exception cref="NetmeraException">Throws exception if key is null, if value is null</exception>
        public void add(String key, Object value)
        {
            if (key == null)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_NULL_EXCEPTION, NetmeraConstants.ContentKey);
            }

            if (value == null)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_NULL_EXCEPTION, NetmeraConstants.ContentValue);
            }

            if ((value.GetType() == typeof(JObject)) == false && (value.GetType() == typeof(JArray)) == false && (value.GetType() == typeof(String)) == false &&
                (value.GetType() == typeof(Boolean)) == false && (value.GetType() == typeof(DateTime)) == false && (value.GetType() == typeof(byte[])) == false &&
                (value.GetType() == typeof(double)) == false && (value.GetType() == typeof(Double)) == false && (value.GetType() == typeof(float)) == false &&
                (value.GetType() == typeof(long)) == false && (value.GetType() == typeof(int) == false) && (value.GetType() == typeof(Int16)) == false &&
                (value.GetType() == typeof(Int32)) == false && (value.GetType() == typeof(Int64)) == false && (value.GetType() == typeof(NetmeraMedia)) == false && (value.GetType() == typeof(NetmeraGeoLocation)) == false && (value.GetType() == typeof(NetmeraUser)) == false)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_DATA_TYPE, value.GetType().ToString());
            }

            try
            {
                if (value.GetType() == typeof(NetmeraGeoLocation))
                {
                    double lat = ((NetmeraGeoLocation)value).getLatitude();
                    double lng = ((NetmeraGeoLocation)value).getLongitude();

                    //Replacements are done because ToString() method convert points in a decimal number to comma.
                    String location = lat.ToString().Replace(',', '.') + "," + lng.ToString().Replace(',', '.');

                    JProperty prLocationField = new JProperty(key + NetmeraConstants.LocationField_Suffix, location);

                    //if data already contains a node with name "key + NetmeraConstants.LocationField_Suffix"
                    data.Remove(key + NetmeraConstants.LocationField_Suffix);

                    data.Add(prLocationField);

                    JProperty prLocationLatitudeField = new JProperty(key + NetmeraConstants.LocationLatitude_Suffix, lat);

                    //if data already contains a node with name "key + NetmeraConstants.LocationLatitude_Suffix"
                    data.Remove(key + NetmeraConstants.LocationLatitude_Suffix);

                    data.Add(prLocationLatitudeField);

                    JProperty prLocationLongitudeField = new JProperty(key + NetmeraConstants.LocationLongitude_Suffix, lng);

                    //if data already contains a node with name "key + NetmeraConstants.LocationLongitude_Suffix"
                    data.Remove(key + NetmeraConstants.LocationLongitude_Suffix);

                    data.Add(prLocationLongitudeField);
                }
                else if (value.GetType() == typeof(NetmeraUser))
                {
                    NetmeraClient.setLoggedUserSecurityToken(NetmeraUser.securityToken);
                }
                else if (value.GetType() == typeof(NetmeraMedia))
                {
                    //mediaData.Add(key, value);

                    NetmeraMedia tmpMedia     = (NetmeraMedia)value;
                    byte[]       tmpMediaData = tmpMedia.getData(); //url is already null, so no need to add to json

                    JProperty prop = new JProperty(key, JsonConvert.SerializeObject(tmpMediaData));

                    //if mediaData already contains a node with name "key"
                    mediaData.Remove(key);

                    mediaData.Add(prop);
                }
                else
                {
                    JProperty prop = new JProperty(key, value);

                    //if data already contains a node with name "key"
                    data.Remove(key);

                    data.Add(prop);
                }
            }
            catch (JsonException e)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_JSON, "Json with key : [" + key + "], value : [" + value + "] pairs is invalid.", e.Message);
            }
        }
Beispiel #15
0
        /// <summary>
        /// Login with Twitter account
        /// </summary>
        /// <param name="callback">Method to be called when login operation finishes</param>
        public static void login(Action <NetmeraUser, Exception> callback)
        {
            String securityToken = NetmeraClient.getSecurityToken();

            if (securityToken != null && securityToken.Trim() != "")
            {
                Popup            popup   = new Popup();
                LoginUserControl control = new LoginUserControl();
                control.webBrowser.Source = null;
                control.btnCancel.Click  += new RoutedEventHandler((s, e) =>
                {
                    if (callback != null)
                    {
                        callback(null, null);
                    }
                });
                try
                {
                    NetmeraUser.clearSocialSessions((logout, ex) =>
                    {
                        if (logout && ex == null)
                        {
                            control.webBrowser.Loaded += new RoutedEventHandler((s, e) =>
                            {
                                twitter.getTwitterLoginUri((loginUri, ex1) =>
                                {
                                    if (loginUri != null && ex1 == null)
                                    {
                                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                                        {
                                            control.webBrowser.Navigate(loginUri);
                                        });
                                    }
                                    else
                                    {
                                        if (callback != null)
                                        {
                                            callback(null, ex1);
                                        }
                                    }
                                });
                            });

                            control.webBrowser.LoadCompleted += new LoadCompletedEventHandler((s, e) =>
                            {
                                if (e.Uri.AbsoluteUri == Twitter.AuthorizeUrl)
                                {
                                    String pinHtml = control.webBrowser.SaveToString();

                                    twitter.getTwitterPin(pinHtml, (pin, ex1) =>
                                    {
                                        if (!String.IsNullOrEmpty(pin))
                                        {
                                            twitter.getTwitterAccessToken(e.Uri, (accessToken, ex2) =>
                                            {
                                                Deployment.Current.Dispatcher.BeginInvoke(() =>
                                                {
                                                    popup.IsOpen = false;
                                                    if (ex2 != null)
                                                    {
                                                        if (callback != null)
                                                        {
                                                            callback(null, ex2);
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (!String.IsNullOrEmpty(accessToken))
                                                        {
                                                            getTwitterUser((user, exc) =>
                                                            {
                                                                if (user != null && exc == null)
                                                                {
                                                                    TwitterSession.save(twitter);
                                                                }
                                                                callback(user, exc);
                                                            });
                                                        }
                                                        else
                                                        {
                                                            login(callback);
                                                        }
                                                    }
                                                });
                                            });
                                        }
                                    });
                                }
                            });
                            Deployment.Current.Dispatcher.BeginInvoke(() =>
                            {
                                popup.Child  = control;
                                popup.IsOpen = true;
                            });
                        }
                        else
                        {
                            if (callback != null)
                            {
                                callback(null, ex);
                            }
                        }
                    });
                }
                catch (Exception e)
                {
                    if (callback != null)
                    {
                        callback(null, e);
                    }
                }
            }
            else
            {
                if (callback != null)
                {
                    callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_API_KEY_NOT_FOUND, "You didn't set your api key. Please use NetmeraClient.init(apiKey)."));
                }
            }
        }
        /// <summary>
        /// Adds key,value pairs into the object. If the object contains key, the old value is replaced.
        /// </summary>
        /// <param name="key">Key to identify specified value</param>
        /// <param name="value">value associates with the specified key</param>
        /// <exception cref="NetmeraException">Throws exception if key is null, if value is null</exception>
        public void add(String key, Object value)
        {
            if (key == null)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_NULL_EXCEPTION, NetmeraConstants.ContentKey);
            }

            if (value == null)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_NULL_EXCEPTION, NetmeraConstants.ContentValue);
            }

            if ((value.GetType() == typeof(JObject)) == false && (value.GetType() == typeof(JArray)) == false && (value.GetType() == typeof(String)) == false &&
                (value.GetType() == typeof(Boolean)) == false && (value.GetType() == typeof(DateTime)) == false && (value.GetType() == typeof(byte[])) == false &&
                (value.GetType() == typeof(double)) == false && (value.GetType() == typeof(Double)) == false && (value.GetType() == typeof(float)) == false &&
                (value.GetType() == typeof(long)) == false && (value.GetType() == typeof(int) == false) && (value.GetType() == typeof(Int16)) == false &&
                (value.GetType() == typeof(Int32)) == false && (value.GetType() == typeof(Int64)) == false && (value.GetType() == typeof(NetmeraMedia)) == false && (value.GetType() == typeof(NetmeraGeoLocation)) == false && (value.GetType() == typeof(NetmeraUser)) == false)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_DATA_TYPE, value.GetType().ToString());
            }

            try
            {
                if (value.GetType() == typeof(NetmeraGeoLocation))
                {
                    double lat = ((NetmeraGeoLocation)value).getLatitude();
                    double lng = ((NetmeraGeoLocation)value).getLongitude();

                    //Replacements are done because ToString() method convert points in a decimal number to comma.
                    String location = lat.ToString().Replace(',', '.') + "," + lng.ToString().Replace(',', '.');

                    JProperty prLocationField = new JProperty(key + NetmeraConstants.LocationField_Suffix, location);

                    //if data already contains a node with name "key + NetmeraConstants.LocationField_Suffix"
                    data.Remove(key + NetmeraConstants.LocationField_Suffix);

                    data.Add(prLocationField);

                    JProperty prLocationLatitudeField = new JProperty(key + NetmeraConstants.LocationLatitude_Suffix, lat);

                    //if data already contains a node with name "key + NetmeraConstants.LocationLatitude_Suffix"
                    data.Remove(key + NetmeraConstants.LocationLatitude_Suffix);

                    data.Add(prLocationLatitudeField);

                    JProperty prLocationLongitudeField = new JProperty(key + NetmeraConstants.LocationLongitude_Suffix, lng);

                    //if data already contains a node with name "key + NetmeraConstants.LocationLongitude_Suffix"
                    data.Remove(key + NetmeraConstants.LocationLongitude_Suffix);

                    data.Add(prLocationLongitudeField);
                }
                else if (value.GetType() == typeof(NetmeraUser))
                {
                    NetmeraClient.setLoggedUserSecurityToken(NetmeraUser.securityToken);
                }
                else if (value.GetType() == typeof(NetmeraMedia))
                {
                    //mediaData.Add(key, value);

                    NetmeraMedia tmpMedia     = (NetmeraMedia)value;
                    byte[]       tmpMediaData = tmpMedia.getData(); //url is already null, so no need to add to json

                    JProperty prop = new JProperty(key, JsonConvert.SerializeObject(tmpMediaData));

                    //if mediaData already contains a node with name "key"
                    mediaData.Remove(key);

                    mediaData.Add(prop);
                }
                else
                {
                    if (value is string)
                    {
                        var allow = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890&-()%'!@[]{}#^*;:/?<>,.|~^$";

                        var allowedChars = allow.ToCharArray();
                        var valueArray   = value.ToString().ToCharArray();
                        var newValue     = new List <char>();
                        for (int i = 0; i < valueArray.Length; i++)
                        {
                            if (allowedChars.Contains(valueArray[i]))
                            {
                                newValue.Add(valueArray[i]);
                            }
                        }

                        var s = new string(newValue.ToArray()).Trim();

                        value = HttpUtility.UrlEncode(s);
                    }

                    JProperty prop = new JProperty(key, value);

                    //if data already contains a node with name "key"
                    data.Remove(key);

                    data.Add(prop);
                }
            }
            catch (JsonException e)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_JSON, "Json with key : [" + key + "], value : [" + value + "] pairs is invalid.", e.Message);
            }
        }
        /// <summary>
        /// Finds the number of <see cref="NetmeraContent"/> objects that matches with the query.
        /// </summary>
        /// <param name="callback">Method called when count operation finishes</param>
        public void count(Action <long, Exception> callback)
        {
            RequestItem item = new RequestItem();

            if (searchText != null)
            {
                item.searchText = searchText;
            }

            item.path            = NetmeraConstants.Default_ParentPath;
            item.contentType     = NetmeraConstants.Default_ContentType;
            item.max             = max;
            item.page            = page;
            item.sortBy          = sortBy;
            item.sortOrder       = sortOrder.ToString();
            item.customCondition = getCustomCondition();

            NetmeraHttpUtils.search(item, (lno, ex) =>
            {
                if (ex != null)
                {
                    if (callback != null)
                    {
                        callback(0, ex);
                    }
                }
                else if (lno != null)
                {
                    try
                    {
                        long count = getNumberOfTotalResults(new JArray(lno));
                        if (callback != null)
                        {
                            callback(count, null);
                        }
                    }
                    catch (JsonException e)
                    {
                        if (callback != null)
                        {
                            callback(0, new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_JSON, "Json in the response of search method is invalid.", e.Message));
                        }
                    }
                    catch (IOException e)
                    {
                        if (callback != null)
                        {
                            callback(0, new NetmeraException(NetmeraException.ErrorCode.EC_IO_EXCEPTION, "IO Exception occurred in search method.", e.Message));
                        }
                    }
                    catch (Exception e)
                    {
                        if (callback != null)
                        {
                            callback(0, new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_REQUEST, "Request exception occurred in search method.", e.Message));
                        }
                    }
                    NetmeraClient.finish();
                }
                NetmeraClient.finish();
            });
        }
Beispiel #18
0
        /// <summary>
        /// Logs a user into the registered application with Facebook account with Facebook permissions.
        /// </summary>
        /// <param name="permissionArray">Permission array</param>
        /// <param name="callback">Method to be called when login operation finishes</param>
        public static void login(String[] permissionArray, Action <NetmeraUser, Exception> callback)
        {
            String securityToken = NetmeraClient.getSecurityToken();

            if (securityToken != null && securityToken.Trim() != "")
            {
                Popup            popup   = new Popup();
                LoginUserControl control = new LoginUserControl();
                control.btnCancel.Click += new RoutedEventHandler((s, e) =>
                {
                    NetmeraUser user = null;
                    user             = NetmeraUser.getCurrentUser();
                    if (callback != null)
                    {
                        callback(user, null);
                    }
                });
                try
                {
                    NetmeraUser.clearSocialSessions((logout, ex) =>
                    {
                        if (logout && ex == null)
                        {
                            control.webBrowser.Loaded += new RoutedEventHandler((s, e) =>
                            {
                                facebook.getFacebookLoginUri(permissionArray, (loginUri, ex1) =>
                                {
                                    if (loginUri != null && ex1 == null)
                                    {
                                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                                        {
                                            control.webBrowser.Navigate(loginUri);
                                        });
                                    }
                                    else
                                    {
                                        if (callback != null)
                                        {
                                            callback(null, ex1);
                                        }
                                    }
                                });
                            });

                            control.webBrowser.LoadCompleted += new LoadCompletedEventHandler((s, e) =>
                            {
                                String ss = control.webBrowser.SaveToString();
                                facebook.getFacebookAccessToken(e.Uri, (accessToken, ex2) =>
                                {
                                    if (!String.IsNullOrEmpty(accessToken))
                                    {
                                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                                        {
                                            popup.IsOpen = false;
                                            getFacebookUser((user, ex3) =>
                                            {
                                                if (user != null && ex3 == null)
                                                {
                                                    FacebookSession.save(facebook);
                                                }
                                                callback(user, ex3);
                                            });
                                        });
                                    }
                                    else if (ex2 != null)
                                    {
                                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                                        {
                                            popup.IsOpen = false;
                                            if (callback != null)
                                            {
                                                callback(null, ex2);
                                            }
                                        });
                                    }
                                });
                            });
                            Deployment.Current.Dispatcher.BeginInvoke(() =>
                            {
                                popup.Child  = control;
                                popup.IsOpen = true;
                            });
                        }
                        else
                        {
                            if (callback != null)
                            {
                                callback(null, ex);
                            }
                        }
                    });
                }
                catch (Exception)
                {
                    if (callback != null)
                    {
                        callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_FB_ERROR, "Error occured while logging in facebook."));
                    }
                }
            }
            else
            {
                if (callback != null)
                {
                    callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_API_KEY_NOT_FOUND, "You didn't set your api key. Please use NetmeraClient.init(apiKey)."));
                }
            }
        }
        private void searchFromNetwork(Action <List <NetmeraContent>, Exception> callback)
        {
            RequestItem item = new RequestItem();

            item.path            = NetmeraConstants.Default_ParentPath;
            item.contentType     = NetmeraConstants.Default_ContentType;
            item.max             = max;
            item.page            = page;
            item.sortBy          = sortBy;
            item.sortOrder       = sortOrder.ToString();
            item.customCondition = getCustomCondition();

            if (searchText != null)
            {
                item.searchText = searchText;
            }

            NetmeraHttpUtils.search(item, (lno, ex) =>
            {
                if (ex != null)
                {
                    if (callback != null)
                    {
                        callback(null, ex);
                    }
                }
                else if (lno != null)
                {
                    try
                    {
                        List <NetmeraContent> contentList = convertJsonArrayToNetmeraContent(new JArray(lno));
                        if (callback != null)
                        {
                            callback(contentList, null);
                        }
                    }
                    catch (JsonException e)
                    {
                        if (callback != null)
                        {
                            callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_JSON, "Json in the response of search method is invalid.", e.Message));
                        }
                    }
                    catch (IOException e)
                    {
                        if (callback != null)
                        {
                            callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_IO_EXCEPTION, "IO Exception occurred in search method.", e.Message));
                        }
                    }
                    catch (Exception e)
                    {
                        if (callback != null)
                        {
                            callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_REQUEST, "Request exception occurred in search method.", e.Message));
                        }
                    }
                    NetmeraClient.finish();
                }
            });
        }
        /// <summary>
        /// Searches the content by taking given location as a base and retrieves the contents that located given distance far away.
        /// </summary>
        /// <param name="startLocation">Base location to search near it.</param>
        /// <param name="distance">Distance used to create circle by taking the startLocation as a center.</param>
        /// <param name="locationSearchField">Name of the field that holds location data.</param>
        /// <param name="callback">Method called when circle search operation finishes</param>
        public void circleSearch(NetmeraGeoLocation startLocation, double distance, String locationSearchField, Action <List <NetmeraContent>, Exception> callback)
        {
            RequestItem item = new RequestItem();

            item.path            = NetmeraConstants.Default_ParentPath;
            item.contentType     = NetmeraConstants.Default_ContentType;
            item.max             = max;
            item.page            = page;
            item.sortBy          = sortBy;
            item.sortOrder       = sortOrder.ToString();
            item.customCondition = getCustomCondition();

            if (searchText != null)
            {
                item.searchText = searchText;
            }

            item.locationSearchType  = NetmeraConstants.LocationSearchType_Circle;
            item.distance            = distance;
            item.latitudes           = startLocation.getLatitude().ToString();
            item.longitudes          = startLocation.getLongitude().ToString();
            item.locationSearchField = locationSearchField;

            NetmeraHttpUtils.locationSearch(item, (lno, ex) =>
            {
                if (ex != null)
                {
                    if (callback != null)
                    {
                        callback(null, ex);
                    }
                }
                else if (lno != null)
                {
                    try
                    {
                        List <NetmeraContent> contentList = convertJsonArrayToNetmeraContent(new JArray(lno));
                        if (callback != null)
                        {
                            callback(contentList, null);
                        }
                    }
                    catch (JsonException e)
                    {
                        if (callback != null)
                        {
                            callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_JSON, "Json in the response of search method is invalid.", e.Message));
                        }
                    }
                    catch (IOException e)
                    {
                        if (callback != null)
                        {
                            callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_IO_EXCEPTION, "IO Exception occurred in search method.", e.Message));
                        }
                    }
                    catch (Exception e)
                    {
                        if (callback != null)
                        {
                            callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_REQUEST, "Request exception occurred in search method.", e.Message));
                        }
                    }

                    NetmeraClient.finish();
                }
            });
        }
        /// <summary>
        /// Gets the <see cref="NetmeraContent"/> from the network, not the cache.
        /// </summary>
        /// <param name="path">Content path to get</param>
        /// <param name="callback">Method finding the content as the result of get operation and exception if exists.</param>
        public void getFromNetwork(String path, Action <NetmeraContent, Exception> callback)
        {
            RequestItem item = new RequestItem();

            if (path == null || path.Length == 0)
            {
                if (callback != null)
                {
                    callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_NULL_EXCEPTION, "Path cannot be null or empty"));
                }
            }

            item.path            = path;
            item.contentType     = NetmeraConstants.Default_ContentType;
            item.customCondition = getCustomCondition();

            NetmeraHttpUtils.get(item, (lno, ex) =>
            {
                if (ex != null)
                {
                    if (callback != null)
                    {
                        callback(null, ex);
                    }
                }
                else if (lno != null)
                {
                    try
                    {
                        List <NetmeraContent> contentList = convertJsonArrayToNetmeraContent(new JArray(lno));
                        if (callback != null)
                        {
                            if (contentList != null && contentList.Count != 0)
                            {
                                callback(contentList.ToArray()[0], null);
                            }
                        }
                    }
                    catch (JsonException e)
                    {
                        if (callback != null)
                        {
                            callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_JSON, "Json in the response of search method is invalid.", e.Message));
                        }
                    }
                    catch (IOException e)
                    {
                        if (callback != null)
                        {
                            callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_IO_EXCEPTION, "IO Exception occurred in search method.", e.Message));
                        }
                    }
                    catch (Exception e)
                    {
                        if (callback != null)
                        {
                            callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_REQUEST, "Request exception occurred in search method.", e.Message));
                        }
                    }

                    NetmeraClient.finish();
                }
            });
        }