Exemplo n.º 1
0
            public void subscribe_to_endpoint(StreamingEndpoint endpoint)
            {
                if (endpoint != null)
                {
                    if (endpoint.needs_channel_id && string.IsNullOrEmpty(endpoint.channel_id))
                    {
                        throw new Exception("Channel endpoint is missing in subsciption");
                    }
                    if (string.IsNullOrEmpty(connection_id))
                    {
                        to_be_subscribed_endpoints.Add(endpoint);
                        return;
                    }

                    string request_url = string.Format("https://alpha-api.app.net{0}?connection_id={1}&ct={2}", endpoint.endpoint, System.Web.HttpUtility.UrlEncode(connection_id), "AppNet.net");
                    if (endpoint.options != null)
                    {
                        if (!string.IsNullOrEmpty(endpoint.options.query_string()))
                        {
                            request_url += "&" + endpoint.options.query_string();
                        }
                    }

                    Dictionary <string, string> header = new Dictionary <string, string>();
                    header.Add("Authorization", "Bearer " + access_token);

                    Helper.Response subscription_response = Helper.SendGetRequest(request_url, header);

                    if (subscription_response.Success)
                    {
                        Tuple <string, ApiCallResponse> apiCallResponse = Helper.getData <string>(subscription_response);
                        if (apiCallResponse != null)
                        {
                            if (apiCallResponse.Item2 != null)
                            {
                                if (apiCallResponse.Item2.meta != null)
                                {
                                    string subscription_id = apiCallResponse.Item2.meta.subscription_id;
                                    endpoint.subscription_id = subscription_id;
                                    if (!string.IsNullOrEmpty(subscription_id))
                                    {
                                        subscribed_endpoints.Add(subscription_id, endpoint);
                                    }
                                }
                            }
                        }
                    }
                }
            }
Exemplo n.º 2
0
            private void ParseMessage(string json)
            {
                Helper.Response api_response = new Helper.Response();
                api_response.Success = true;
                api_response.Content = json;

                ApiCallResponse apiCallResponse = new ApiCallResponse();

                apiCallResponse.success = true;
                apiCallResponse.content = json;

                Newtonsoft.Json.Linq.JObject responseJson = Newtonsoft.Json.Linq.JObject.Parse(json);
                JsonSerializerSettings       settings     = new JsonSerializerSettings();

                settings.Error += delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
                {
                    throw args.ErrorContext.Error;
                };
                if (responseJson != null)
                {
                    try
                    {
                        apiCallResponse.meta = JsonConvert.DeserializeObject <Model.Meta>(responseJson["meta"].ToString(), settings);
                    }
                    catch (Exception exp) {
                        apiCallResponse.success          = false;
                        apiCallResponse.errorMessage     = exp.Message;
                        apiCallResponse.errorDescription = exp.StackTrace;
                        return;
                    }
                }

                if (apiCallResponse.meta != null)
                {
                    if (apiCallResponse.meta.subscription_ids != null)
                    {
                        string data = null;
                        try
                        {
                            data = responseJson["data"].ToString();
                        }
                        catch { return; }
                        if (string.IsNullOrEmpty(data))
                        {
                            return;
                        }
                        foreach (string subscription_id in apiCallResponse.meta.subscription_ids)
                        {
                            if (subscribed_endpoints.ContainsKey(subscription_id))
                            {
                                StreamingEndpoint endpoint = subscribed_endpoints[subscription_id];
                                if (endpoint != null)
                                {
                                    switch (endpoint.title)
                                    {
                                    case "Following":
                                        List <User> following = JsonConvert.DeserializeObject <List <User> >(data, settings);
                                        if (following != null && followingCallback != null)
                                        {
                                            followingCallback(following, is_deleted: apiCallResponse.meta.is_deleted);
                                        }
                                        break;

                                    case "Followers":
                                        List <User> followers = JsonConvert.DeserializeObject <List <User> >(data, settings);
                                        if (followers != null && followersCallback != null)
                                        {
                                            followersCallback(followers, is_deleted: apiCallResponse.meta.is_deleted);
                                        }
                                        break;

                                    case "Posts":
                                        List <Post> posts = JsonConvert.DeserializeObject <List <Post> >(data, settings);
                                        if (posts != null && postsCallback != null)
                                        {
                                            postsCallback(posts, is_deleted: apiCallResponse.meta.is_deleted);
                                        }
                                        break;

                                    case "Mentions":
                                        List <Post> mentions = JsonConvert.DeserializeObject <List <Post> >(data, settings);
                                        if (mentions != null && mentionsCallback != null)
                                        {
                                            mentionsCallback(mentions, is_deleted: apiCallResponse.meta.is_deleted);
                                        }
                                        break;

                                    case "Stream":
                                        List <Post> stream = JsonConvert.DeserializeObject <List <Post> >(data, settings);
                                        if (stream != null && streamCallback != null)
                                        {
                                            streamCallback(stream, is_deleted: apiCallResponse.meta.is_deleted);
                                        }
                                        break;

                                    case "Unified":
                                        List <Post> unified_posts = JsonConvert.DeserializeObject <List <Post> >(data, settings);
                                        if (unified_posts != null && unifiedCallback != null)
                                        {
                                            unifiedCallback(unified_posts, is_deleted: apiCallResponse.meta.is_deleted);
                                        }
                                        break;

                                    case "Channels":
                                        List <Message> channels = JsonConvert.DeserializeObject <List <Message> >(data, settings);
                                        if (channels != null && channelsCallback != null)
                                        {
                                            channelsCallback(channels, is_deleted: apiCallResponse.meta.is_deleted);
                                        }
                                        break;

                                    case "Channel subscribers":
                                        List <User> subscribers = JsonConvert.DeserializeObject <List <User> >(data, settings);
                                        if (subscribers != null && channelSubscribersCallback != null)
                                        {
                                            channelSubscribersCallback(subscribers, is_deleted: apiCallResponse.meta.is_deleted);
                                        }
                                        break;

                                    case "Channel messages":
                                        List <Message> messages = JsonConvert.DeserializeObject <List <Message> >(data, settings);
                                        if (messages != null && channelMessagesCallback != null)
                                        {
                                            channelMessagesCallback(messages, is_deleted: apiCallResponse.meta.is_deleted);
                                        }
                                        break;

                                    case "Files":
                                        List <AppNetDotNet.Model.File> files = JsonConvert.DeserializeObject <List <AppNetDotNet.Model.File> >(data, settings);
                                        if (files != null && filesCallback != null)
                                        {
                                            filesCallback(files, is_deleted: apiCallResponse.meta.is_deleted);
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
Exemplo n.º 3
0
            private void set_streaming_endpoints()
            {
                Dictionary <string, StreamingEndpoint> endpoints = new Dictionary <string, StreamingEndpoint>();

                StreamingEndpoint following = new StreamingEndpoint();

                following.title       = "Following";
                following.endpoint    = "/stream/0/users/me/following";
                following.return_type = typeof(List <User>);
                endpoints.Add("Following", following);

                StreamingEndpoint followers = new StreamingEndpoint();

                followers.title       = "Followers";
                followers.endpoint    = "/stream/0/users/me/followers";
                followers.return_type = typeof(List <User>);
                endpoints.Add("Followers", followers);

                StreamingEndpoint posts = new StreamingEndpoint();

                posts.title       = "Posts";
                posts.endpoint    = "/stream/0/users/me/posts";
                posts.return_type = typeof(List <Post>);
                endpoints.Add("Posts", posts);

                StreamingEndpoint mentions = new StreamingEndpoint();

                mentions.title       = "Mentions";
                mentions.endpoint    = "/stream/0/users/me/mentions";
                mentions.return_type = typeof(List <Post>);
                endpoints.Add("Mentions", mentions);

                StreamingEndpoint stream = new StreamingEndpoint();

                stream.title       = "Stream";
                stream.endpoint    = "/stream/0/posts/stream";
                stream.return_type = typeof(List <Post>);
                endpoints.Add("Stream", stream);

                StreamingEndpoint unified = new StreamingEndpoint();

                unified.title       = "Unified";
                unified.endpoint    = "/stream/0/posts/stream/unified";
                unified.return_type = typeof(List <Post>);
                endpoints.Add("Unified", unified);

                StreamingEndpoint channels = new StreamingEndpoint();

                channels.title       = "Channels";
                channels.endpoint    = "/stream/0/channels";
                channels.return_type = typeof(List <Message>);
                endpoints.Add("Channels", channels);

                StreamingEndpoint channel_subscribers = new StreamingEndpoint();

                channel_subscribers.title            = "Channel subscribers";
                channel_subscribers.endpoint         = "/stream/0/channels/{0}/subscribers";
                channel_subscribers.return_type      = typeof(List <User>);
                channel_subscribers.needs_channel_id = true;
                endpoints.Add("Channel subscribers", channel_subscribers);

                StreamingEndpoint channel_messages = new StreamingEndpoint();

                channel_messages.title            = "Channel messages";
                channel_messages.endpoint         = "/stream/0/channels/{0}/messages";
                channel_messages.return_type      = typeof(List <Message>);
                channel_messages.needs_channel_id = true;
                endpoints.Add("Channel messages", channel_messages);

                StreamingEndpoint files = new StreamingEndpoint();

                files.title       = "Files";
                files.endpoint    = "/stream/0/users/me/files";
                files.return_type = typeof(List <AppNetDotNet.Model.File>);
                endpoints.Add("Files", files);

                _available_endpoints = endpoints;
            }