Ejemplo n.º 1
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);
     }
 }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
0
        private void MakeVkCall( Method method, VkRestContext context)
        {
            var sb = new StringBuilder(ApiUrl);
            sb.Append(method.Name).Append('?');
            
            if (method.UserParam.Length > 0)
            {
                // if method uses user id - get id name from method and user id from context
                sb.Append(method.UserParam).Append("=").Append(context.UserId).Append('&');
            }
            
            if (!method.IsOpen)
            {
                // if method is secure - apply access token from context
                sb.Append("access_token=").Append(context.AuthToken).Append('&');                
            }
            
            if (context.Parameters.Length > 0)
            {
                // apply additional parameters from the context
                sb.Append(context.Parameters).Append('&');
            }

            sb.Append("v=").Append(method.Version).Append('&');
            sb.Append("lang=").Append(context.Lang);

            // make rest call
            MakeRestCall(method.Fid, sb.ToString(), context);
            
        }
Ejemplo n.º 4
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;
     }
 }
Ejemplo n.º 5
0
 public OnErrorEventArgs(VkFunction function, VkRestContext context, long code, String error, String details)
 {
     Function = function;
     Context = context;
     Code = code;
     Error = error;
     Details = details;
 }
Ejemplo n.º 6
0
 public OnErrorEventArgs(VkFunction function, VkRestContext context, long code, String error) :
     this(function, context, code, error, "")
 {
 }