Commits

Maxim Moiseev committed 441eeb7 Merge

blocking network access for silverlight implemented. UserApi refactored a bit.

  • Participants
  • Parent commits 969cf9f, 38b0b36

Comments (0)

Files changed (3)

src/Linq2vk.Core/Api.fs

             seq {
                 let url = q |> Query.addFrom a |> Query.addTo b |> Query.toUrl
                 printfn "Url: %s" url
-                let text = Net.requestText url |> Async.RunSynchronously
+                let text = Net.requestTextSync url
                 match text with
                 | JsonArr arr ->
                     yield! Seq.map string arr
     let getOne q =
         let url = q |> Query.toUrl
         printfn "Url: %s" url
-        Net.requestText url |> Async.RunSynchronously
+        Net.requestTextSync url

src/Linq2vk.Core/Net.fs

 namespace Linq2vk.Core.Implementation
 
 module Net =
+    open System
     open System.IO
     open System.Net
-    
+
+#if ASYNC    
     let getUrlResponse (url:string) =
-        let setRequestProperties (req:HttpWebRequest) =
-#if SILVERLIGHT
-#else
-            req.AllowAutoRedirect <- false
-#endif
-            ()
-        
         async {
             let req = HttpWebRequest.Create(url)
-            setRequestProperties (req :?> HttpWebRequest)
             let! resp = req.AsyncGetResponse()
             return resp :?> HttpWebResponse
         }
             use stream = resp.GetResponseStream()
             use reader = new StreamReader(stream)
             return reader.ReadToEnd()
-        }
+        }
+#endif
+
+    let getUrlResponseSync (url:string) =
+        let req = HttpWebRequest.Create(url) :?> HttpWebRequest
+        use allDone = new System.Threading.AutoResetEvent(false)
+        
+        let (resp:WebResponse ref) = ref null
+        let onResponse (asyncRes:IAsyncResult) =
+            resp := req.EndGetResponse(asyncRes)
+            
+            let httpResp = !resp :?> HttpWebResponse
+            printfn "%A" httpResp.ResponseUri
+            
+            allDone.Set() |> ignore
+            ()
+            
+        let asyncRes = req.BeginGetResponse(AsyncCallback(onResponse), null)
+        
+        // TODO: Possible problems with infinite lock.
+        allDone.WaitOne() |> ignore
+        !resp :?> HttpWebResponse
+   
+    let processUrlResponseSync (url:string) f =
+        let resp = getUrlResponseSync url
+        f resp
+        
+    let requestTextSync url =
+        let resp = getUrlResponseSync url
+        use stream = resp.GetResponseStream()
+        use reader = new StreamReader(stream)
+        reader.ReadToEnd()

src/Linq2vk.Core/UserAPI.fs

 
 type UserAPI (prjId:int64) =
     let WindowSize = 10
-    let InvalidUid = Int64.MinValue
     
     let mutable _sid = String.Empty
-    let mutable _uid = InvalidUid
     
     let query act =
         Query.init _sid |> Query.addAction act
     let userQuery uid act =
         query act |> Query.addId uid
         
-    let myQuery act =
-        userQuery _uid act
-    
     let getItem conv q =
         q |> Api.getOne |> conv
         
         q |> Api.getAll |> Seq.map conv
     
     member this.LoggedIn
-        with get () = _uid <> InvalidUid
+        with get () = String.IsNullOrEmpty(_sid)
         
-    member this.UserId
-        with get () = _uid
-    
     member this.Login(usr:string, pwd:string) =
         let query =
             sprintf "http://login.userapi.com/auth?login=force&site=%d&email=%s&pass=%s" prjId usr pwd
             
-        let getIds (resp:HttpWebResponse) =        
-            let sidRx = Regex("sid=(.+)$")
-            let uidRx = Regex("remixmid=([0-9]+);")
+        let getSid (resp:HttpWebResponse) =        
+            let sidRx = Regex("sid=([a-z0-9]+)")
             
             let getMatchedGroup (rx:Regex) =
                 rx.Match >> (fun m -> m.Groups.[1].Value)
 
             let sid =
-                resp.Headers.["Location"] |> getMatchedGroup sidRx                   
-            let uid = 
-                resp.Headers.["Set-Cookie"] |> getMatchedGroup uidRx |> int64
-                
-//            printfn "%s" resp.Headers.["Set-Cookie"]
-            (sid, uid)
+                resp.ResponseUri.AbsoluteUri |> getMatchedGroup sidRx                   
+            sid
             
-        let (sid, uid) =
-            let task = Net.processUrlResponse query getIds
-            try
-                Async.RunSynchronously task
-            with
-            | :? NotImplementedException as ex ->
-                    System.Diagnostics.Debug.WriteLine(ex.ToString())
-                    if ex.InnerException <> null then
-                        System.Diagnostics.Debug.WriteLine(ex.InnerException.ToString())
-                    ("", InvalidUid)
+        let sid =
+            Net.processUrlResponseSync query getSid
         
         _sid <- sid
-        _uid <- uid
         this.LoggedIn
+
+    member private this.GetFriendsFrom(queryF) =
+        queryF Action.Friends
+        |> getItem Conversions.toFriend
     
     member private this.GetFriends(uid:int64) =
-        userQuery uid Action.Friends |>
-            getAllItems Conversions.toFriend
+        this.GetFriendsFrom (userQuery uid)
             
     member this.MyFriends
-        with get () = this.GetFriends(_uid)
+        with get () =
+            this.GetFriendsFrom query
         
     member this.StatusUpdates
         with get () =
-            myQuery Action.UpdatesActivity |>
+            query Action.UpdatesActivity |>
                 getAllItems Conversions.toStatus
                 
     member this.FriendsOfFriends
         with get () =
-            myQuery Action.UpdatesFriends |>
+            query Action.UpdatesFriends |>
                 getAllItems Conversions.toFriend
             
     member this.FriendsPhotos
         with get () =
-            myQuery Action.UpdatesPhotos |>
+            query Action.UpdatesPhotos |>
                 getAllItems Conversions.toPhoto
             
     member this.FriendsTaggedPhotos
         with get () =
-            myQuery Action.UpdatesTaggedPhotos |>
+            query Action.UpdatesTaggedPhotos |>
                 getAllItems Conversions.toPhoto
-            
+
+    member private this.GetProfileFrom(queryF) =
+        queryF Action.Profile
+        |> getItem Conversions.toUserProfile
+    
     member this.GetUserProfile (uid:int64) =
-        userQuery uid Action.Profile |>
-            getItem Conversions.toUserProfile
+        this.GetProfileFrom (userQuery uid)
         
     member this.MyProfile
-        with get () = this.GetUserProfile(_uid)
+        with get () = 
+            this.GetProfileFrom query