Beispiel #1
0
        /// <summary>
        /// Tells asana to delete the specified object
        /// </summary>
        /// <param name="obj"></param>
        internal Task Delete <T>(T obj) where T : AsanaObject
        {
            AsanaFunction func;

            IAsanaData idata = obj as IAsanaData;

            if (idata == null)
            {
                throw new NullReferenceException("All AsanaObjects must implement IAsanaData in order to Delete themselves.");
            }

            AsanaRequest             request = null;
            AsanaFunctionAssociation afa     = AsanaFunction.GetFunctionAssociation(obj.GetType());

            if (idata.IsObjectLocal == false)
            {
                func = afa.Delete;
            }
            else
            {
                throw new Exception("Object is local, cannot delete.");
            }

            if (Object.ReferenceEquals(func, null))
            {
                throw new NotImplementedException("This object cannot delete itself.");
            }

            request = GetBaseRequest(func, obj);
            return(request.Go((o, h) => RepackAndCallback(o, obj), ErrorCallback));
        }
Beispiel #2
0
        public Task Get <AsanaT>(AsanaWorkspace arg1, AsanaCollectionResponseEventHandler callback) where AsanaT : AsanaObject
        {
            AsanaRequest request = default(AsanaRequest);

            if (typeof(AsanaT) == typeof(AsanaUser))
            {
                request = GetBaseRequest(AsanaFunction.GetFunction(Function.GetUsersInWorkspace), arg1);
                return(request.Go((o, h) => PackAndSendResponseCollection <AsanaUser>(o, callback), ErrorCallback));
            }


            if (typeof(AsanaT) == typeof(AsanaProject))
            {
                request = GetBaseRequest(AsanaFunction.GetFunction(Function.GetProjectsInWorkspace), arg1);
                return(request.Go((o, h) => PackAndSendResponseCollection <AsanaProject>(o, callback), ErrorCallback));
            }


            if (typeof(AsanaT) == typeof(AsanaTag))
            {
                request = GetBaseRequest(AsanaFunction.GetFunction(Function.GetTagsInWorkspace), arg1);
                return(request.Go((o, h) => PackAndSendResponseCollection <AsanaTag>(o, callback), ErrorCallback));
            }


            if (typeof(AsanaT) == typeof(AsanaTeam))
            {
                request = GetBaseRequest(AsanaFunction.GetFunction(Function.GetTeamsInWorkspace), arg1);
                return(request.Go((o, h) => PackAndSendResponseCollection <AsanaTeam>(o, callback), ErrorCallback));
            }


            throw new TypeAccessException("Unknown type for this request: " + typeof(AsanaT).Name);
        }
Beispiel #3
0
        public Task Get <AsanaT>(AsanaTask arg1, String arg2, AsanaResponseEventHandler callback) where AsanaT : AsanaObject
        {
            AsanaRequest request = default(AsanaRequest);

            if (typeof(AsanaT) == typeof(AsanaEvent))
            {
                request = GetBaseRequest(AsanaFunction.GetFunction(Function.GetEventsInTask), arg1, arg2);
                return(request.Go((o, h) => PackAndSendResponse <AsanaEvent>(o, callback), ErrorCallback));
            }


            throw new TypeAccessException("Unknown type for this request: " + typeof(AsanaT).Name);
        }
Beispiel #4
0
        public Task Get <AsanaT>(Int64 arg1, AsanaResponseEventHandler callback) where AsanaT : AsanaObject
        {
            AsanaRequest request = default(AsanaRequest);

            if (typeof(AsanaT) == typeof(AsanaUser))
            {
                request = GetBaseRequest(AsanaFunction.GetFunction(Function.GetUserById), arg1);
                return(request.Go((o, h) => PackAndSendResponse <AsanaUser>(o, callback), ErrorCallback));
            }


            if (typeof(AsanaT) == typeof(AsanaWorkspace))
            {
                request = GetBaseRequest(AsanaFunction.GetFunction(Function.GetWorkspaceById), arg1);
                return(request.Go((o, h) => PackAndSendResponse <AsanaWorkspace>(o, callback), ErrorCallback));
            }


            if (typeof(AsanaT) == typeof(AsanaTask))
            {
                request = GetBaseRequest(AsanaFunction.GetFunction(Function.GetTaskById), arg1);
                return(request.Go((o, h) => PackAndSendResponse <AsanaTask>(o, callback), ErrorCallback));
            }


            if (typeof(AsanaT) == typeof(AsanaStory))
            {
                request = GetBaseRequest(AsanaFunction.GetFunction(Function.GetStoryById), arg1);
                return(request.Go((o, h) => PackAndSendResponse <AsanaStory>(o, callback), ErrorCallback));
            }


            if (typeof(AsanaT) == typeof(AsanaProject))
            {
                request = GetBaseRequest(AsanaFunction.GetFunction(Function.GetProjectById), arg1);
                return(request.Go((o, h) => PackAndSendResponse <AsanaProject>(o, callback), ErrorCallback));
            }


            if (typeof(AsanaT) == typeof(AsanaTag))
            {
                request = GetBaseRequest(AsanaFunction.GetFunction(Function.GetTagById), arg1);
                return(request.Go((o, h) => PackAndSendResponse <AsanaTag>(o, callback), ErrorCallback));
            }


            throw new TypeAccessException("Unknown type for this request: " + typeof(AsanaT).Name);
        }
Beispiel #5
0
        public Task Get <AsanaT>(AsanaTask arg1, AsanaCollectionResponseEventHandler callback) where AsanaT : AsanaObject
        {
            AsanaRequest request = default(AsanaRequest);

            if (typeof(AsanaT) == typeof(AsanaStory))
            {
                request = GetBaseRequest(AsanaFunction.GetFunction(Function.GetStoriesInTask), arg1);
                return(request.Go((o, h) => PackAndSendResponseCollection <AsanaStory>(o, callback), ErrorCallback));
            }


            if (typeof(AsanaT) == typeof(AsanaProject))
            {
                request = GetBaseRequest(AsanaFunction.GetFunction(Function.GetProjectsOnATask), arg1);
                return(request.Go((o, h) => PackAndSendResponseCollection <AsanaProject>(o, callback), ErrorCallback));
            }


            throw new TypeAccessException("Unknown type for this request: " + typeof(AsanaT).Name);
        }
Beispiel #6
0
        /// <summary>
        /// Begins the request
        /// </summary> <Task>
        public Task Go(Action <string, WebHeaderCollection> callback, Action <string, string, string> error)
        {
            _callback = callback;
            _error    = error;

            if (_throttling)
            {
                _throttlingWaitHandle.WaitOne();
            }

            return(Task.Factory.FromAsync <WebResponse>(
                       _request.BeginGetResponse,
                       _request.EndGetResponse,
                       null).ContinueWith((requestTask) =>
            {
                HttpWebRequest request = (HttpWebRequest)_request;
                AsanaRequest state = (AsanaRequest)requestTask.AsyncState;

                if (requestTask.IsFaulted)
                {
                    _error(request.Address.AbsoluteUri, requestTask.Exception.InnerException.Message, "");
                    return;
                }

                WebResponse result = requestTask.Result;

                if (result.Headers["Retry-After"] != null)
                {
                    string retryAfter = result.Headers["Retry-After"];
                    ThrottleFor(Convert.ToInt32(retryAfter));
                    Go(callback, error);
                    return;
                }
                string responseContent = GetResponseContent(result);
                _callback(responseContent, result.Headers);
            }
                                          ));
        }
Beispiel #7
0
        /// <summary>
        /// Tells the asana object to save the specified object
        /// </summary>
        /// <param name="obj"></param>
        internal Task Save <T>(T obj, AsanaFunction func, Dictionary <string, object> data = null) where T : AsanaObject
        {
            IAsanaData idata = obj as IAsanaData;

            if (idata == null)
            {
                throw new NullReferenceException("All AsanaObjects must implement IAsanaData in order to Save themselves.");
            }

            if (data == null)
            {
                data = Parsing.Serialize(obj, true, !idata.IsObjectLocal);
            }
            AsanaRequest             request = null;
            AsanaFunctionAssociation afa     = AsanaFunction.GetFunctionAssociation(obj.GetType());

            if (func == null)
            {
                func = idata.IsObjectLocal ? afa.Create : afa.Update;
            }

            request = GetBaseRequestWithParams(func, data, obj);
            return(request.Go((o, h) => RepackAndCallback(o, obj), ErrorCallback));
        }
Beispiel #8
0
        /// <summary>
        /// Callback for the end of the async operation.
        /// </summary>
        /// <param name="result"></param>
        private void ResponseCallback(IAsyncResult result)
        {
            AsanaRequest   state   = (AsanaRequest)result.AsyncState;
            HttpWebRequest request = (HttpWebRequest)_request;

            HttpWebResponse response = null;

            try
            {
                response = (HttpWebResponse)request.EndGetResponse(result);
            }
            catch (System.Net.WebException ex)
            {
                string responseContent = GetResponseContent(ex.Response);
                _error(ex.Response.ResponseUri.AbsoluteUri, ex.Message, responseContent);
                return;
            }

            string output = GetResponseContent(response);

            response.Close();

            _callback(output, response.Headers);
        }