Commits

Maxim Moiseev committed 4e37cfc

optional fields in response arrays are handled for IUserDescriptor

  • Participants
  • Parent commits 5277747

Comments (0)

Files changed (4)

File src/Common/Data.fs

     abstract Timestamp : DateTime
     abstract Text : string
     abstract Sender : IUserDescriptor
-    //abstract Recipient : IUserDescriptor
+    abstract Recipient : IUserDescriptor
     abstract IsRead : bool
 
 [<AutoOpen>]
         else
             token |> string
     
-    let intBool (token:JToken) = 0 <> (token |> int)
+    let intBool (token:JToken) =
+        0 <> (token |> int)
 
     let UnixTimeStart = DateTime(1970, 1, 1)
     let fromUnixtime (x:int64) = 
             (parseGroup 1, parseGroup 2)
         else
             raise <| ArgumentException(sprintf "Invalid complex id format: %s" id)
+            
+    let optional<'T> (tokens:JToken[]) (index:int) convert =
+        if index < tokens.Length then
+            convert tokens.[index]
+        else
+            Unchecked.defaultof<'T>
+        
+            
          
 
 [<RequireQualifiedAccess>]
         
     let userDescriptor (ts:JToken[]) =
         { new IUserDescriptor with
-            override this.UserId = ts.[0] |> int64
-            override this.UserName = ts.[1] |> anyString
-            override this.PhotoUrl = ts.[2] |> anyString
-            override this.ThumbnailFileName = ts.[3] |> anyString
-            override this.Gender = ts.[4] |> int |> enum<Gender>
-            override this.IsOnline = ts.[5] |> intBool
+            override this.UserId = ts.[0] |> anyString |> int64
+            override this.UserName = optional<string> ts 1 string'
+            override this.PhotoUrl = optional<string> ts 2 anyString
+            override this.ThumbnailFileName = optional<string> ts 3 anyString
+            override this.Gender = optional<Gender> ts 4 (int >> enum<Gender>)
+            override this.IsOnline = optional<bool> ts 5 intBool
         }
         
     let message (ts:JToken[]) =
         { new IMessage with
             override this.MessageId = ts.[0] |> anyString |> int64
             override this.Timestamp = parseTimestamp ts.[1]
-            override this.Text = (Seq.toArray ts.[2]).[0] |> anyString
+            override this.Text = (Seq.toArray ts.[2]).[0] |> string'
             override this.Sender = userDescriptor (Seq.toArray ts.[3])
+            override this.Recipient = userDescriptor (Seq.toArray ts.[4])
             override this.IsRead = ts.[5] |> intBool
         }
-        
+       
+[<RequireQualifiedAccess>] 
 module Conversions =
     open Linq2vk.Core.Implementation.DataKind
     

File src/Common/Query.fs

             | UpdatesTaggedPhotos   -> "updates_tagged_photos"
             | Profile               -> "profile"
             | Inbox                 -> "inbox"
+            | Outbox                -> "outbox"
             | _                     -> failwith "Not implemented"
             
     // TODO: why does not point-free style work here?

File src/Common/UserAPI.fs

         with get () =
             query Action.Inbox |>
                 getAllItems Conversions.toMessage
+                
+    member this.Outbox 
+        with get () =
+            query Action.Outbox |>
+                getAllItems Conversions.toMessage
         

File src/Linq2vk.Core.Tests/SimpleTests.cs

         public void InboxTest() {
             var messages = _api.Inbox.Take(5);
             foreach (var msg in messages) {
-                Console.WriteLine("Message: {0}, From: {1}, Text:'{2}'", msg.MessageId, msg.Sender.UserName, msg.Text);
+                Console.WriteLine("Message: {0}, From: {1}, To: {3}, Text:'{2}'", msg.MessageId, msg.Sender.UserName, msg.Text, msg.Recipient.UserName);
                 Console.WriteLine("Sent at: {0}", msg.Timestamp);
             }
         }
 
+        [Test, Explicit]
+        public void OutboxTest() {
+            var messages = _api.Outbox.Take(5);
+            foreach (var msg in messages) {
+                Console.WriteLine("Message: {0}, From: {1}, To: {3}, Text:'{2}'", msg.MessageId, msg.Sender.UserName, msg.Text, msg.Recipient.UserName);
+                Console.WriteLine("Sent at: {0}", msg.Timestamp);
+            }
+        }
+
+
     }
 }