Пример #1
0
        // make REST call to VK services
        private void MakeRestCall(VkFunction function, String uri, VkRestContext context)
        {
            if (OnData == null || OnError == null) 
                throw new ArgumentException("OnData and OnError handlers must be provided");

            try
            {
                // Create URI 
                var address = new Uri(uri);
                Debug.WriteLine("REST call: " + address);

                // Create the web request 
                var request = WebRequest.Create(address) as HttpWebRequest;

                if (request == null)
                {
                    var args = new OnErrorEventArgs(function, context, CriticalErrorCode, CriticalErrorText, "Request object is null");
                    OnError(this, args);
                    return;
                }

                // Set type to Get 
                request.Method = GetMethod;
                request.ContentType = ContentType;
                request.Accept = ContentAccept;

                // Get response 
                using (var response = request.GetResponse() as HttpWebResponse)
                {
                    if (response == null)
                    {
                        var args = new OnErrorEventArgs(function, context, CriticalErrorCode, CriticalErrorText, "Response object is null");
                        OnError(this, args);
                        return;
                    }

                    var responseStream = response.GetResponseStream();
                    if (responseStream == null)
                    {
                        var args = new OnErrorEventArgs(function, context, CriticalErrorCode, CriticalErrorText, "Response stream is null");
                        OnError(this, args);
                        return;
                    }

                    var responseCode = (int)response.StatusCode;
                    if (responseCode < 300)
                    {

                        string responseBody;

                        try
                        {
                            responseBody = ((new StreamReader(responseStream)).ReadToEnd());
                        }
                        catch (IOException e)
                        {
                            var args = new OnErrorEventArgs(function, context, CriticalErrorCode, 
                                CriticalErrorText, e.Message);
                            OnError(this, args);
                            return;                            
                        }

                        //var contentType = response.ContentType;
                        var o = JObject.Parse(responseBody);
                        if (o[ResponseBody] != null)
                        {
                            var args = new OnDataEventArgs(function, o, context.Cookie);
                            OnData(this, args);
                        }
                        else if (o[ErrorBody] != null)
                        {
                            long code = 0;
                            var error = "";
                            if (o[ErrorBody]["error_code"] != null)
                            {
                                code = o[ErrorBody]["error_code"].ToObject<long>();
                            }
                            if (o[ErrorBody]["error_msg"] != null)
                            {
                                error = o[ErrorBody]["error_msg"].ToString();
                            }

                            var args = new OnErrorEventArgs(function, context, code, error, o[ErrorBody].ToString());
                            OnError(this, args);
                        }
                    }
                    else
                    {
                        var args = new OnErrorEventArgs(function, context, CriticalErrorCode, CriticalErrorText, "Unexpected response code: " + responseCode);
                        OnError(this, args);
                    }
                }
            }
            catch (WebException exception)
            {
                HandleWebException(function, exception, context);
            }
        }
Пример #2
0
 private void HandleWebException(VkFunction function, WebException ex, VkRestContext context)
 {
     if (ex.Status == WebExceptionStatus.ProtocolError)
     {
         var statusCode = (int)((HttpWebResponse)ex.Response).StatusCode;
         var responseStream = ex.Response.GetResponseStream();
         var responseText = responseStream!= null ? (new StreamReader(responseStream)).ReadToEnd() : "";
         var args = new OnErrorEventArgs(function, context, statusCode, responseText);
         OnError(this, args);
     }
     else
     {
         var args = new OnErrorEventArgs(function, context, CriticalErrorCode, CriticalErrorText, ex.Message);
         OnError(this, args);
     }
 }
Пример #3
0
 // functions switch
 public void CallVkFunction(VkFunction function, VkRestContext context)
 {
     switch (function)
     {
         case VkFunction.GetProfiles:
             MakeVkCall(GetProfiles, context);
             break;
         case VkFunction.LoadFriends:
             MakeVkCall(LoadFriends, context);
             break;
         case VkFunction.FriendsGet:
             MakeVkCall(FriendsGet, context);
             break;
         case VkFunction.FriendsGetMutual:
             MakeVkCall(FriendsGetMutual, context);
             break;
         case VkFunction.UsersSearch:
             MakeVkCall(UsersSearch, context);
             break;
         case VkFunction.WallGet:
             MakeVkCall(WallGet, context);
             break;
         case VkFunction.WallGetComments:
             MakeVkCall(WallGetComments, context);
             break;
         case VkFunction.StatsGet:
             MakeVkCall(StatsGet, context);
             break;
         case VkFunction.GroupsGetMembers:
             MakeVkCall(GroupsGetMembers, context);
             break;
         case VkFunction.GroupsGetById:
             MakeVkCall(GroupsGetById, context);
             break;
         case VkFunction.LikesGetList:
             MakeVkCall(LikesGetList, context);
             break;
         case VkFunction.UsersGet:
             MakeVkCall(UsersGet, context);
             break;
         case VkFunction.DatabaseGetCountries:
             MakeVkCall(DatabaseGetCountries, context);
             break;
         case VkFunction.DatabaseGetRegions:
             MakeVkCall(DatabaseGetRegions, context);
             break;
         case VkFunction.DatabaseGetCities:
             MakeVkCall(DatabaseGetCities, context);
             break;
         case VkFunction.BoardGetTopics:
             MakeVkCall(BoardGetTopics, context);
             break;
         case VkFunction.BoardGetComments:
             MakeVkCall(BoardGetComments, context);
             break;
         case VkFunction.PhotosSearch:
             MakeVkCall(PhotosSearch, context);
             break;
     }
 }
Пример #4
0
 public OnErrorEventArgs(VkFunction function, VkRestContext context, long code, String error, String details)
 {
     Function = function;
     Context = context;
     Code = code;
     Error = error;
     Details = details;
 }
Пример #5
0
 public OnErrorEventArgs(VkFunction function, VkRestContext context, long code, String error) :
     this(function, context, code, error, "")
 {
 }
Пример #6
0
 public Method(VkFunction fid, string name, string userParam, string version, bool isOpen)
 {
     Fid = fid;
     Name = name;
     UserParam = userParam;
     Version = version;
     IsOpen = isOpen;
 }
Пример #7
0
 public OnDataEventArgs(VkFunction function, JObject data, String cookie)
 {
     Function = function;
     Data = data;
     Cookie = cookie;
 }