Example #1
0
        /// <summary>
        /// Get a list of Historics queries in your account.
        /// </summary>
        /// <param name="user">The user making the request.</param>
        /// <param name="page">The page number to get.</param>
        /// <param name="per_page">The number of items per page.</param>
        /// <returns>A list of Historic objects.</returns>
        public static HistoricList list(User user, int page = 1, int per_page = 20)
        {
            try
            {
                Dictionary<string, string> parameters = new Dictionary<string, string>();
                parameters.Add("page", page.ToString());
                parameters.Add("per_page", per_page.ToString());
                JSONdn res = user.callApi("historics/get", parameters);

                if (!res.has("count"))
                {
                    throw new ApiException("No count in the response");
                }
                HistoricList retval = new HistoricList(res.getIntVal("count"));

                if (!res.has("data") && retval.TotalCount > 0)
                {
                    throw new ApiException("No historics in the response");
                }
                JToken[] children = res.getChildren("data");
                for (int i = 0; i < children.Length; i++)
                {
                    retval.Add(new Historic(user, new JSONdn(children[i])));
                }
                return retval;
            }
            catch (ApiException e)
            {
                if (e.Code == 400)
                {
                    throw new InvalidDataException(e.Message);
                }
                throw new ApiException("Unexpected API response code: " + e.Code.ToString() + " " + e.Message);
            }
        }
Example #2
0
        private void tabs_Selecting(object sender, TabControlCancelEventArgs e)
        {
            string username = txtUsername.Text.Trim();
            string api_key = txtAPIKey.Text.Trim();

            if (e.TabPageIndex != 0)
            {
                if (username.Length == 0 || api_key.Length == 0)
                {
                    MessageBox.Show("Please enter your DataSift username and API key.", "Error");
                    e.Cancel = true;
                    return;
                }
            }

            if (m_user == null || m_user.getUsername() != username || m_user.getApiKey() != api_key)
            {
                m_user = null;
                m_user = new User(username, api_key);
            }

            if (e.TabPage.Text.EndsWith("*"))
            {
                e.TabPage.Text = e.TabPage.Text.TrimEnd(new char[] { '*' });
            }
        }
        /// <summary>
        /// A factory method for creating a consumer from an array of hashes.
        /// </summary>
        /// <param name="user">The user object that's creating this consumer.</param>
        /// <param name="type">The consumer type.</param>
        /// <param name="hashes">An array of stream hashes.</param>
        /// <param name="event_handler">An object that implements IEventHandler which will receive the events.</param>
        /// <returns>An object of a type derived from this class.</returns>
        public static StreamConsumer factory(User user, string type, string[] hashes, IEventHandler event_handler)
        {
            switch (type)
            {
                case "http":
                    return new StreamConsumer_Http(user, hashes, event_handler);

                default:
                    throw new InvalidDataException("Consumer type \"" + type + "\" is unknown");
            }
        }
 public void InitTest()
 {
     TestData.init();
     m_user = new User(TestData.username, TestData.api_key);
     m_user.setApiClient(new MockApiClient(TestData.username, TestData.api_key));
     m_historic = m_user.createHistoric(
         TestData.definition_hash,
         TestData.historic_start,
         TestData.historic_end,
         TestData.historic_sources,
         TestData.historic_sample,
         TestData.historic_name);
 }
Example #5
0
        public bool LoadCredentials()
        {
            m_username = Properties.Settings.Default.username;
            m_api_key = Properties.Settings.Default.api_key;

            // Update the main menu button with our username
            menu.btnAccount.Text = m_username.Length == 0 ? "DataSift Account" : username;

            // Create the user object
            if (m_username.Length > 0 && m_api_key.Length > 0)
            {
                m_user = new User(username, api_key);
            }

            return m_username.Length > 0 && m_api_key.Length > 0;
        }
Example #6
0
 /// <summary>
 /// Constructor. Creates a Historic object by fetching an existing query from the API.
 /// </summary>
 /// <param name="user">The User creating the object.</param>
 /// <param name="playback_id">The Historics query playback ID.</param>
 public Historic(User user, string playback_id)
 {
     m_user = user;
     m_playback_id = playback_id;
     reloadData();
 }
Example #7
0
 /// <summary>
 /// Constructor. Note that if you create a Definition with both a
 /// non-empty CSDL and a non-empty hash the hash will be ignored.
 /// </summary>
 /// <param name="u">The user to whom this Definition should belong.</param>
 /// <param name="csdl">An initial CSDL string.</param>
 /// <param name="hash">An initial hash string.</param>
 public Definition(User user, string csdl = "", string hash = "")
 {
     m_user = user;
     m_hash = hash;
     set(csdl);
 }
        /// <summary>
        /// Get a page of Push subscriptions in the given user's account,
        /// where each page contains up to per_page items. Results will be
        /// ordered according to the supplied ordering parameters.
        /// </summary>
        /// <param name="user">The user object making the request.</param>
        /// <param name="page">The page number to get.</param>
        /// <param name="per_page">The number of items per page.</param>
        /// <param name="order_by">The field by which to order the results.</param>
        /// <param name="order_dir">Ascending or descending.</param>
        /// <param name="include_finished">True to include subscriptions against finished Historics queries.</param>
        /// <param name="hash_type">Optional hash type to look for (hash is also required)</param>
        /// <param name="hash">Optional hash to look for (hash_type is also required)</param>
        /// <returns>A PushSubscriptionList object.</returns>
        public static PushSubscriptionList list(User user, int page, int per_page, string order_by = ORDERBY_CREATED_AT, string order_dir = ORDERDIR_ASC, bool include_finished = false, string hash_type = "", string hash = "")
        {
            Dictionary<string, string> parameters = new Dictionary<string, string>();

            if (hash_type.Length > 0)
            {
                if (hash_type != "hash" && hash_type != "playback_id")
                {
                    throw new InvalidDataException("Hash type is invalid");
                }
                if (hash.Length == 0)
                {
                    throw new InvalidDataException("Hash type given but the hash is empty");
                }
                parameters.Add(hash_type, hash);
            }

            if (page < 1)
            {
                throw new InvalidDataException("The specified page number is invalid");
            }

            if (per_page < 1)
            {
                throw new InvalidDataException("The specified per_page value is invalid");
            }

            if (order_by != ORDERBY_ID && order_by != ORDERBY_CREATED_AT)
            {
                throw new InvalidDataException("The specified order_by is not supported");
            }

            if (order_dir != ORDERDIR_ASC && order_dir != ORDERDIR_DESC)
            {
                throw new InvalidDataException("The specified order_dir is not supported");
            }

            parameters.Add("page", page.ToString());
            parameters.Add("per_page", per_page.ToString());
            parameters.Add("order_by", order_by);
            parameters.Add("order_dir", order_dir);

            if (include_finished)
            {
                parameters.Add("include_finished", "1");
            }

            JSONdn res = user.callApi("push/get", parameters);

            if (!res.has("count"))
            {
                throw new ApiException("No count in the response");
            }
            PushSubscriptionList retval = new PushSubscriptionList(res.getIntVal("count"));

            if (!res.has("subscriptions") && retval.TotalCount > 0)
            {
                throw new ApiException("No subscriptions in the response");
            }
            JToken[] children = res.getChildren("subscriptions");
            for (int i = 0; i < children.Length; i++)
            {
                retval.Add(new PushSubscription(user, new JSONdn(children[i])));
            }
            return retval;
        }
 /// <summary>
 /// Construct a consumer for the given definition.
 /// </summary>
 /// <param name="user">The User object which is creating this consumer.</param>
 /// <param name="definition">The Definition to be consumed.</param>
 /// <param name="event_handler">The object that will receive events.</param>
 public StreamConsumer(User user, Definition definition, IEventHandler event_handler)
 {
     init(user, event_handler);
     m_hashes.Add(definition.getHash());
 }
 /// <summary>
 /// Initialise this consumer. This is used by both constructors.
 /// </summary>
 /// <param name="user">The user object which is creating this consumer.</param>
 /// <param name="event_handler">The object that will receive events.</param>
 public void init(User user, IEventHandler event_handler)
 {
     m_user = user;
     m_event_handler = event_handler;
     m_state = State.Stopped;
 }
 /// <summary>
 /// Get a page of Push subscriptions for the given stream hash in the
 /// given user's account, where each page contains up to per_page
 /// items. Results will be ordered according to the supplied ordering
 /// parameters.
 /// </summary>
 /// <param name="user">The user object making the request.</param>
 /// <param name="hash">The stream hash.</param>
 /// <param name="page">The page number to get.</param>
 /// <param name="per_page">The number of items per page.</param>
 /// <param name="order_by">The field by which to order the results.</param>
 /// <param name="order_dir">Ascending or descending.</param>
 /// <returns>A PushSubscriptionList object.</returns>
 public static PushSubscriptionList listByStreamHash(User user, string hash, int page, int per_page, string order_by = ORDERBY_CREATED_AT, string order_dir = ORDERDIR_ASC)
 {
     return list(user, page, per_page, order_by, order_dir, false, "hash", hash);
 }
 /// <summary>
 /// Get a Push subscription by ID.
 /// </summary>
 /// <param name="user">The user who owns the subscription.</param>
 /// <param name="id">The subscription ID.</param>
 /// <returns>A PushSubscription object.</returns>
 public static PushSubscription get(User user, string id)
 {
     Dictionary<string, string> parameters = new Dictionary<string, string>();
     parameters.Add("id", id);
     return new PushSubscription(user, user.callApi("push/get", parameters));
 }
 /// <summary>
 /// Construct a PushSubscription object from a JSON object.
 /// </summary>
 /// <param name="user"></param>
 /// <param name="json"></param>
 public PushSubscription(User user, JSONdn json)
     : base(user)
 {
     init(json);
 }
        /// <summary>
        /// Page through recent Push log entries.
        /// </summary>
        /// <param name="user">The user making the request.</param>
        /// <param name="page">Which page to fetch.</param>
        /// <param name="per_page">How many entries per page.</param>
        /// <param name="order_by">Which field to sort by.</param>
        /// <param name="order_dir">In asc[ending] or desc[ending] order.</param>
        /// <param name="id">Optional subscription ID.</param>
        /// <returns>A PushLog object.</returns>
        public static PushLog getLogs(User user, int page, int per_page, string order_by = ORDERBY_REQUEST_TIME, string order_dir = ORDERDIR_DESC, string id = "")
        {
            Dictionary<string, string> parameters = new Dictionary<string, string>();

            if (page < 1)
            {
                throw new InvalidDataException("The specified page number is invalid");
            }

            if (per_page < 1)
            {
                throw new InvalidDataException("The specified per_page value is invalid");
            }

            if (order_by != ORDERBY_REQUEST_TIME)
            {
                throw new InvalidDataException("The specified order_by is not supported");
            }

            if (order_dir != ORDERDIR_ASC && order_dir != ORDERDIR_DESC)
            {
                throw new InvalidDataException("The specified order_dir is not supported");
            }

            if (id.Length > 0)
            {
                parameters.Add("id", id);
            }

            parameters.Add("page", page.ToString());
            parameters.Add("per_page", per_page.ToString());
            parameters.Add("order_by", order_by);
            parameters.Add("order_dir", order_dir);

            JSONdn res = user.callApi("push/log", parameters);

            if (!res.has("count"))
            {
                throw new ApiException("No count in the response");
            }
            PushLog retval = new PushLog(res.getIntVal("count"));

            if (!res.has("log_entries") && retval.TotalCount > 0)
            {
                throw new ApiException("No log entries in the response");
            }
            JToken[] children = res.getChildren("log_entries");
            for (int i = 0; i < children.Length; i++)
            {
                retval.Add(new PushLogEntry(new JSONdn(children[i])));
            }
            return retval;
        }
 /// <summary>
 /// Page through recent log entries for the given subscription ID.
 /// </summary>
 /// <param name="user">The user making the request.</param>
 /// <param name="id">The subscription ID.</param>
 /// <param name="page">Which page to fetch.</param>
 /// <param name="per_page">How many entries per page.</param>
 /// <param name="order_by">Which field to sort by.</param>
 /// <param name="order_dir">In asc[ending] or desc[ending] order.</param>
 /// <returns>A PushLog object.</returns>
 public static PushLog getLogsBySubscriptionId(User user, string id, int page, int per_page, string order_by = ORDERBY_REQUEST_TIME, string order_dir = ORDERDIR_DESC)
 {
     return getLogs(user, page, per_page, order_by, order_dir, id);
 }
Example #16
0
 /// <summary>
 /// Constrcutor. Creates a Historic object from the data contained within a JSON object.
 /// </summary>
 /// <param name="user">The User creating the object.</param>
 /// <param name="data">The JSON data.</param>
 public Historic(User user, JSONdn data)
 {
     m_user = user;
     init(data);
 }
Example #17
0
        /// <summary>
        /// Constructor. All parameters are required.
        /// </summary>
        /// <param name="user">The user creating the Historic object.</param>
        /// <param name="hash">The stream hash that this Historic will use.</param>
        /// <param name="start_date">The start date for the query.</param>
        /// <param name="end_date">The end date for the query.</param>
        /// <param name="sources">An array of data sources for the query.</param>
        /// <param name="sample">The sample rate for the query.</param>
        /// <param name="name">A friendly name for the query.</param>
        public Historic(User user, string hash, DateTime start_date, DateTime end_date, List<string> sources, double sample, string name)
        {
            m_user        = user;
			m_stream_hash = hash;
			m_start_date  = start_date;
			m_end_date    = end_date;
			m_sources     = sources;
			m_name        = name;
			m_sample      = sample;
			m_progress    = 0;
        }
Example #18
0
        public bool SaveCredentials()
        {
            if (m_remember_credentials)
            {
                Properties.Settings.Default.username = m_username;
                Properties.Settings.Default.api_key = m_api_key;
            }
            else
            {
                Properties.Settings.Default.username = "";
                Properties.Settings.Default.api_key = "";
            }
            Properties.Settings.Default.Save();

            // Update/create the user object
            if (m_user != null)
            {
                m_user = null;
            }
            m_user = new User(username, api_key);

            // Validate the user credentials
            try
            {
                m_user.getUsage();
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }
 /// <summary>
 /// Constructor. Takes the user creating the object.
 /// </summary>
 /// <param name="user">The user creating the object.</param>
 public PushDefinition(User user)
 {
     m_user = user;
 }
 /// <summary>
 /// Construct a consumer for the given definition.
 /// </summary>
 /// <param name="user">The User object which is creating this consumer.</param>
 /// <param name="definition">The Definition to be consumed.</param>
 /// <param name="event_handler">The object that will receive events.</param>
 public StreamConsumer_Http(User user, Definition definition, IEventHandler event_handler)
     : base(user, definition, event_handler)
 {
 }
 /// <summary>
 /// Construct a consumer for the given array of stream hashes.
 /// </summary>
 /// <param name="user">The User object which is creating this consumer.</param>
 /// <param name="hashes">The array of stream hashes to be consumed.</param>
 /// <param name="event_handler">The object that will receive events.</param>
 public StreamConsumer_Http(User user, string[] hashes, IEventHandler event_handler)
     : base(user, hashes, event_handler)
 {
 }
Example #22
0
 public void InitTest()
 {
     m_user = new User(TestData.username, TestData.api_key);
     m_user.setApiClient(new MockApiClient(TestData.username, TestData.api_key));
 }
 /// <summary>
 /// Construct a consumer for the given array of stream hashes.
 /// </summary>
 /// <param name="user">The User object which is creating this consumer.</param>
 /// <param name="hashes">The array of stream hashes to be consumed.</param>
 /// <param name="event_handler">The object that will receive events.</param>
 public StreamConsumer(User user, string[] hashes, IEventHandler event_handler)
 {
     init(user, event_handler);
     foreach (string hash in hashes)
     {
         m_hashes.Add(hash);
     }
 }
Example #24
0
 public void CleanupTest()
 {
     m_user = null;
 }
 public void CleanupTest()
 {
     m_historic = null;
     m_user = null;
 }
 /// <summary>
 /// Get a page of Push subscriptions for the given Historics query
 /// playback ID in the given user's account, where each page contains
 /// up to per_page items. Results will be ordered according to the
 /// supplied ordering parameters.
 /// </summary>
 /// <param name="user">The user object making the request.</param>
 /// <param name="playback_id">The playback ID.</param>
 /// <param name="page">The page number to get.</param>
 /// <param name="per_page">The number of items per page.</param>
 /// <param name="order_by">The field by which to order the results.</param>
 /// <param name="order_dir">Ascending or descending.</param>
 /// <param name="include_finished">True to include subscriptions against finished Historics queries.</param>
 /// <returns>A PushSubscriptionList object.</returns>
 public static PushSubscriptionList listByPlaybackId(User user, string playback_id, int page, int per_page, string order_by = ORDERBY_CREATED_AT, string order_dir = ORDERDIR_ASC, bool include_finished = false)
 {
     return list(user, page, per_page, order_by, order_dir, include_finished, "playback_id", playback_id);
 }