Represents a batch parameter for the creating batch requests.
http://developers.facebook.com/docs/api/batch/
        /// <summary>
        /// Makes an asynchronous batch request to the Facebook server.
        /// </summary>
        /// <param name="batchParameters">
        /// List of batch parameters.
        /// </param>
        /// <param name="userState">
        /// The user state.
        /// </param>
        /// <param name="cancellationToken">
        /// The cancellation token.
        /// </param>
        /// <returns>
        /// The json result task.
        /// </returns>
        public virtual Task<object> BatchTaskAsync(FacebookBatchParameter[] batchParameters, object userState, CancellationToken cancellationToken
#if ASYNC_AWAIT
, System.IProgress<FacebookUploadProgressChangedEventArgs> uploadProgress
#endif
)
        {
            return BatchTaskAsync(batchParameters, userState, null, cancellationToken
#if ASYNC_AWAIT
, uploadProgress
#endif
                );
        }
 public virtual Task<object> BatchTaskAsync(FacebookBatchParameter[] batchParameters, object userToken, CancellationToken cancellationToken
     #if ASYNC_AWAIT
     , System.IProgress<FacebookUploadProgressChangedEventArgs> uploadProgress
     #endif
     )
 {
     var parameters = PrepareBatchRequest(batchParameters);
     return PostTaskAsync(null, parameters, userToken, cancellationToken
     #if ASYNC_AWAIT
     , null
     #endif
         );
 }
Exemplo n.º 3
0
        private List<FacebookBatchParameter> BuildParameters()
        {
            List<FacebookBatchParameter> parameters = new List<FacebookBatchParameter>();

            foreach (long friend in shareList)
            {
                FacebookBatchParameter param = new FacebookBatchParameter();
                param.HttpMethod = HttpMethod.Post;
                param.Path = string.Format("/{0}/feed", friend.ToString());

                Dictionary<string, object> extenParam = new Dictionary<string, object>();
                extenParam.Add("link", "www.friendsflock.cloudapp.com");
                extenParam.Add("message", message);
                param.Parameters = extenParam;
                parameters.Add(param);
            }

            return parameters;
        }
        internal object PrepareBatchRequest(FacebookBatchParameter[] batchParameters)
        {
            if (batchParameters == null)
                throw new ArgumentNullException("batchParameters");
            if (batchParameters.Length == 0)
                throw new ArgumentNullException("batchParameters", AtLeastOneBatchParameterRequried);

            IDictionary<string, object> actualBatchParameter = new Dictionary<string, object>();
            IList<object> flatnedBatchParameters = new List<object>();
            actualBatchParameter["batch"] = flatnedBatchParameters;

            foreach (var batchParameter in batchParameters)
            {
                IDictionary<string, FacebookMediaObject> mediaObjects;
                IDictionary<string, FacebookMediaStream> mediaStreams;

                var data = ToDictionary(batchParameter.Data, out mediaObjects, out mediaStreams);

                if (mediaObjects.Count + mediaStreams.Count > 0)
                    throw new ArgumentException("Attachments (FacebookMediaObject/FacebookMediaStream) are only allowed in FacebookBatchParameter.Parameters");

                if (data == null)
                    data = new Dictionary<string, object>();

                if (!data.ContainsKey("method"))
                {
                    switch (batchParameter.HttpMethod)
                    {
                        case HttpMethod.Get:
                            data["method"] = "GET";
                            break;
                        case HttpMethod.Post:
                            data["method"] = "POST";
                            break;
                        case HttpMethod.Delete:
                            data["method"] = "DELETE";
                            break;
                        default:
                            throw new ArgumentOutOfRangeException();
                    }
                }

                IList<string> attachedFiles = new List<string>();

                var parameters = ToDictionary(batchParameter.Parameters, out mediaObjects, out mediaStreams) ?? new Dictionary<string, object>();
                bool containsEtag = false;
                string etag = null;
                if (parameters.ContainsKey(ETagKey))
                {
                    etag = (string)parameters[ETagKey];
                    containsEtag = true;
                    parameters.Remove(ETagKey);
                }

                bool hasAttachmentInBatchParameter = false;

                foreach (var attachment in mediaObjects)
                {
                    if (hasAttachmentInBatchParameter)
                        throw new ArgumentException(OnlyOneAttachmentAllowedPerBatchRequest, "batchParameters");
                    if (actualBatchParameter.ContainsKey(attachment.Key))
                        throw new ArgumentException(string.Format("Attachment (FacebookMediaObject/FacebookMediaStream) with key '{0}' already exists", attachment.Key));
                    attachedFiles.Add(HttpHelper.UrlEncode(attachment.Key));
                    actualBatchParameter.Add(attachment.Key, attachment.Value);
                    hasAttachmentInBatchParameter = true;
                }

                foreach (var attachment in mediaStreams)
                {
                    if (hasAttachmentInBatchParameter)
                        throw new ArgumentException(OnlyOneAttachmentAllowedPerBatchRequest, "batchParameters");
                    if (actualBatchParameter.ContainsKey(attachment.Key))
                        throw new ArgumentException(string.Format("Attachment (FacebookMediaObject/FacebookMediaStream) with key '{0}' already exists", attachment.Key));
                    attachedFiles.Add(HttpHelper.UrlEncode(attachment.Key));
                    actualBatchParameter.Add(attachment.Key, attachment.Value);
                    hasAttachmentInBatchParameter = true;
                }

                if (attachedFiles.Count > 0 && !data.ContainsKey("attached_files"))
                    data["attached_files"] = string.Join(",", attachedFiles.ToArray());

                string path;
                if (!data["method"].ToString().Equals("POST", StringComparison.OrdinalIgnoreCase))
                {
                    if (!data.ContainsKey("relative_url"))
                    {
                        path = ParseUrlQueryString(batchParameter.Path, parameters, false);
                        SerializeParameters(parameters);

                        var relativeUrl = new StringBuilder();
                        relativeUrl.Append(path).Append("?");
                        foreach (var kvp in parameters)
                            relativeUrl.AppendFormat("{0}={1}&", HttpHelper.UrlEncode(kvp.Key), HttpHelper.UrlEncode(BuildHttpQuery(kvp.Value, HttpHelper.UrlEncode)));
                        if (relativeUrl.Length > 0)
                            relativeUrl.Length--;
                        data["relative_url"] = relativeUrl.ToString();
                    }
                }
                else
                {
                    path = ParseUrlQueryString(batchParameter.Path, parameters, false);
                    SerializeParameters(parameters);

                    if (!data.ContainsKey("relative_url"))
                    {
                        if (path.Length > 0)
                            data["relative_url"] = path;
                    }

                    if (!data.ContainsKey("body"))
                    {
                        var sb = new StringBuilder();
                        foreach (var kvp in parameters)
                            sb.AppendFormat("{0}={1}&", HttpHelper.UrlEncode(kvp.Key), HttpHelper.UrlEncode(BuildHttpQuery(kvp.Value, HttpHelper.UrlEncode)));

                        if (sb.Length > 0)
                        {
                            sb.Length--;
                            data["body"] = sb.ToString();
                        }
                    }
                }

                if (containsEtag)
                    data[ETagKey] = etag;

                flatnedBatchParameters.Add(data);
            }

            return actualBatchParameter;
        }
 public virtual void BatchAsync(FacebookBatchParameter[] batchParameters)
 {
     BatchAsync(batchParameters, null);
 }
 public virtual void BatchAsync(FacebookBatchParameter[] batchParameters, object userToken)
 {
     var parameters = PrepareBatchRequest(batchParameters);
     PostAsync(null, parameters, userToken);
 }
 public virtual Task<object> BatchTaskAsync(FacebookBatchParameter[] batchParameters, object userToken, object parameters, CancellationToken cancellationToken)
 {
     return BatchTaskAsync(batchParameters, userToken, parameters, cancellationToken, null);
 }
Exemplo n.º 8
0
        /// <summary>
        /// Converts the facebook batch to POST parameters.
        /// </summary>
        /// <param name="batchParameter">
        /// The batch parameter.
        /// </param>
        /// <returns>
        /// The post parameters.
        /// </returns>
        protected IDictionary <string, object> ToParameters(FacebookBatchParameter batchParameter)
        {
            Contract.Requires(batchParameter != null);
            Contract.Ensures(Contract.Result <IDictionary <string, object> >() != null);

            IDictionary <string, object> returnResult = null;

            var defaultParameters = new Dictionary <string, object>();

            defaultParameters["method"] = FacebookUtils.ConvertToString(batchParameter.HttpMethod);

            IDictionary <string, object> parameters = null;

            if (batchParameter.Parameters == null)
            {
                parameters = new Dictionary <string, object>();
            }
            else
            {
                if (batchParameter.Parameters is IDictionary <string, object> )
                {
                    parameters = (IDictionary <string, object>)batchParameter.Parameters;
                }
                else
                {
                    parameters = FacebookUtils.ToDictionary(batchParameter.Parameters);
                }
            }

            var    path        = FacebookUtils.ParseQueryParametersToDictionary(batchParameter.Path, parameters);
            string queryString = string.Empty;

            if (batchParameter.HttpMethod == HttpMethod.Get)
            {
                queryString = FacebookUtils.ToJsonQueryString(parameters);
            }
            else
            {
                defaultParameters["body"] = FacebookUtils.ToJsonQueryString(parameters);
            }

            var relativeUrl = new StringBuilder(path);

            if (!string.IsNullOrEmpty(queryString))
            {
                relativeUrl.AppendFormat("?{0}", queryString);
            }

            defaultParameters["relative_url"] = relativeUrl.ToString();

            var data = batchParameter.Data;

            if (data == null)
            {
                returnResult = defaultParameters;
            }
            else
            {
                if (!(data is IDictionary <string, object>))
                {
                    data = FacebookUtils.ToDictionary(batchParameter.Data);
                }

                returnResult = FacebookUtils.Merge(defaultParameters, (IDictionary <string, object>)data);
            }

            return(returnResult);
        }
Exemplo n.º 9
0
 public virtual void BatchAsync(FacebookBatchParameter[] batchParameters, object userState)
 {
     BatchAsync(batchParameters, userState, null);
 }
Exemplo n.º 10
0
 public virtual void BatchAsync(FacebookBatchParameter[] batchParameters, object userState, object parameters)
 {
     var actualParameter = PrepareBatchRequest(batchParameters, parameters);
     PostAsync(null, actualParameter, userState);
 }
        public virtual void GivenNullFqlInQueryThrowsArgumentNullException()
        {
            var bp = new FacebookBatchParameter();

            Assert.Throws<ArgumentNullException>(() => bp.Query((string)null));
        }
        public virtual void GivenEmptyFqlInMultiQueryThrowsArgumentException()
        {
            var bp = new FacebookBatchParameter();

            Assert.Throws<ArgumentException>(() => bp.Query(new string[] { }));
        }