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
         );
 }
Esempio 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);
 }
Esempio n. 8
0
 public virtual void BatchAsync(FacebookBatchParameter[] batchParameters, object userState)
 {
     BatchAsync(batchParameters, userState, null);
 }
Esempio n. 9
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[] { }));
        }