Skip to content

Loading…

Added support for delta_beta2 API call #50

Closed
wants to merge 2 commits into from

2 participants

@orizzonte

Hi,

I've added support for the new delta_beta2 api call. This does not work with a regular straightforward conversion, so I had to do it "manually" as you will see in ModelHelper. I noticed the implementation for the previous delta version is also incomplete, but I did not correct it, because it will be discontinued on the 1st of March.

I only tested it with sandbox option and I have only provided the sandbox tests.

Feel free to comment or offer improvements.

Another note: It's my first try with git, so I hope everything works out.

Ciao,

Yves

@dkarzon
DropNet member

Not too bad. Few things though.
How come you changed the comments in ThumbnailSize.cs?
Also, with the deserialzation of the delta response I'm wondering if we could just implement a custom deserializer and attach that to the RestClient for that specific content-type? Unless it has the same value as the other calls.

@orizzonte
@orizzonte

Hi,

I've change the code to using a custom deserializer.

Ciao,

Yves

@dkarzon
DropNet member

Looks good, but did you have to replace the deserializer for every request or is it possible to just do it for the delta call? I'll give this a try tonight and merge the pull request.

@dkarzon dkarzon closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 24, 2012
  1. Added support for delta_beta2 API call

    unknown committed
Commits on Feb 26, 2012
  1. @orizzonte
View
17 DropNet.Tests/FileTests1.Sandbox.cs
@@ -5,6 +5,8 @@
using Ploeh.AutoFixture;
using DropNet.Exceptions;
using System;
+using System.Net;
+using DropNet.Models;
namespace DropNet.Tests
{
@@ -203,5 +205,20 @@ public void Can_Get_Thumbnail()
File.WriteAllBytes(@"C:\Temp\TestSandbox.png", rawBytes);
}
+ [TestMethod]
+ public void Can_Get_Delta2()
+ {
+ var delta = _client.GetDelta2(true, "");
+
+ Assert.IsNotNull(delta);
+ }
+
+ [TestMethod]
+ public void Can_Get_Delta2_Async()
+ {
+ _client.GetDelta2Async(true, "", Assert.IsNotNull, Assert.IsNull);
+
+ }
+
}
}
View
5 DropNet/Client/Client.cs
@@ -6,6 +6,7 @@
using DropNet.Exceptions;
using System.Net;
using DropNet.Authenticators;
+using DropNet.DeSerializers;
namespace DropNet
{
@@ -79,8 +80,8 @@ public DropNetClient(string apiKey, string appSecret, string userToken, string u
private void LoadClient()
{
_restClient = new RestClient(ApiBaseUrl);
- _restClient.ClearHandlers();
- _restClient.AddHandler("*", new JsonDeserializer());
+ _restClient.ClearHandlers();
+ _restClient.AddHandler("*", new CustomJsonDeserializer());
_requestHelper = new RequestHelper(Version);
View
22 DropNet/Client/Files.Async.cs
@@ -5,6 +5,9 @@
using System;
using DropNet.Authenticators;
using DropNet.Exceptions;
+using System.Net;
+using DropNet.Helpers;
+using System.Diagnostics;
namespace DropNet
{
@@ -313,6 +316,25 @@ public void GetDeltaAsync(bool IKnowThisIsBetaOnly, string path, Action<DeltaPag
}
/// <summary>
+ /// The beta2 delta function, gets updates for a given folder
+ /// </summary>
+ /// <param name="IKnowThisIsBetaOnly"></param>
+ /// <param name="path"></param>
+ /// <param name="success"></param>
+ /// <param name="failure"></param>
+ public void GetDelta2Async(bool IKnowThisIsBetaOnly, string path, Action<Delta2Page> success, Action<DropboxException> failure)
+ {
+ if (!IKnowThisIsBetaOnly) return;
+
+ //This has to be here as Dropbox change their base URL between calls
+ SetupBaseUrl();
+
+ var request = _requestHelper.CreateDelta2Request(path);
+
+ ExecuteAsync<Delta2Page>(request, success, failure);
+ }
+
+ /// <summary>
/// Gets the thumbnail of an image given its MetaData
/// </summary>
/// <param name="file">The MetaData</param>
View
22 DropNet/Client/Files.Sync.cs
@@ -6,6 +6,8 @@
using DropNet.Authenticators;
using System.Net;
using DropNet.Exceptions;
+using DropNet.Helpers;
+using RestSharp.Deserializers;
namespace DropNet
{
@@ -359,6 +361,26 @@ public DeltaPage GetDelta(bool IKnowThisIsBetaOnly, string path)
return Execute<DeltaPage>(request);
}
+ /// <summary>
+ /// The beta delta2 function
+ /// </summary>
+ /// <param name="IKnowThisIsBetaOnly"></param>
+ /// <param name="cursor"></param>
+ /// <returns></returns>
+ public Delta2Page GetDelta2(bool IKnowThisIsBetaOnly, string cursor)
+ {
+ if (!IKnowThisIsBetaOnly) return null;
+
+ //This has to be here as Dropbox change their base URL between calls
+ SetupBaseUrl();
+
+ var request = _requestHelper.CreateDelta2Request(cursor);
+
+ var response = Execute<Delta2Page>(request);
+
+ return response;
+ }
+
}
}
#endif
View
24 DropNet/DeSerializers/CustomJsonDeserializer.cs
@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using RestSharp.Deserializers;
+using DropNet.Models;
+
+namespace DropNet.DeSerializers
+{
+ class CustomJsonDeserializer : IDeserializer
+ {
+ public string DateFormat { get; set; }
+ public string Namespace { get; set; }
+ public string RootElement { get; set; }
+
+ public T Deserialize<T>(RestSharp.RestResponse response) where T : new()
+ {
+ if (typeof(T) == typeof(Delta2Page))
+ return new Delta2PageJsonDeserializer().Deserialize<T>(response);
+
+ return new JsonDeserializer().Deserialize<T>(response);
+ }
+ }
+}
View
76 DropNet/DeSerializers/Delta2PageJsonDeSerializer.cs
@@ -0,0 +1,76 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using RestSharp.Deserializers;
+using Newtonsoft.Json.Linq;
+using DropNet.Models;
+using Newtonsoft.Json;
+
+namespace DropNet.DeSerializers
+{
+ class Delta2PageJsonDeserializer : IDeserializer
+ {
+ public string DateFormat
+ {
+ get;
+ set;
+ }
+
+ public T Deserialize<T>(RestSharp.RestResponse response) where T : new ()
+ {
+ if (typeof(T) != typeof(Delta2Page))
+ throw new Exception("This deserializer only works for deserializing to a Delta2Page");
+
+ var o = JObject.Parse(response.Content);
+
+ var page = new T() as Delta2Page;
+ page.Reset = o["reset"].Value<bool>();
+ page.Cursor = o["cursor"].Value<string>();
+ page.Reset = o["has_more"].Value<bool>();
+
+ JArray entries = (JArray)o["entries"];
+
+ foreach (var fields in entries)
+ {
+ var entry = new Delta2Entry();
+
+ int i = 0;
+
+ foreach (var field in fields)
+ {
+ if (i == 0) //First element is path
+ entry.Path = field.Value<string>();
+
+ if (i == 1) //Second element can be null or contain a MetaDataObject
+ {
+ if (field.GetType() == typeof(JValue))
+ entry.MetaData = null;
+
+ if (field.GetType() == typeof(JObject))
+ entry.MetaData = new JsonSerializer().Deserialize(new JTokenReader(field), typeof(MetaData)) as MetaData;
+ }
+
+ i++;
+ }
+
+ page.Entries.Add(entry);
+ }
+
+ return (T)Convert.ChangeType(page, typeof(T));
+ }
+
+ public string Namespace
+ {
+ get;
+ set;
+ }
+
+ public string RootElement
+ {
+ get;
+ set;
+
+ }
+ }
+}
View
8 DropNet/DropNet.csproj
@@ -51,9 +51,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
- <Reference Include="Newtonsoft.Json, Version=4.0.5.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
- <SpecificVersion>False</SpecificVersion>
- <HintPath>..\packages\Newtonsoft.Json.4.0.5\lib\net35\Newtonsoft.Json.dll</HintPath>
+ <Reference Include="Newtonsoft.Json">
+ <HintPath>..\packages\Newtonsoft.Json.4.0.8\lib\net35\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="RestSharp, Version=102.5.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\Lib\net35\RestSharp.dll</HintPath>
@@ -76,17 +75,20 @@
<Compile Include="Client\Files.Async.cs" />
<Compile Include="Client\Files.Sync.cs" />
<Compile Include="Client\User.Sync.cs" />
+ <Compile Include="DeSerializers\CustomJsonDeserializer.cs" />
<Compile Include="Exceptions\DropboxException.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="Helpers\RequestHelper.cs" />
<Compile Include="Models\AccountCreationResult.cs" />
<Compile Include="Models\AccountInfo.cs" />
+ <Compile Include="Models\Delta2Page.cs" />
<Compile Include="Models\DeltaPage.cs" />
<Compile Include="Models\MetaData.cs" />
<Compile Include="Models\SharesResponse.cs" />
<Compile Include="Models\ThumbnailSize.cs" />
<Compile Include="Models\UserLogin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="DeSerializers\Delta2PageJsonDeSerializer.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
View
11 DropNet/Helpers/RequestHelper.cs
@@ -238,6 +238,17 @@ internal RestRequest CreateDeltaRequest(string path)
return request;
}
+ internal RestRequest CreateDelta2Request(string cursor)
+ {
+ var request = new RestRequest(Method.POST);
+ request.Resource = "{version}/delta_beta2";
+
+ request.AddParameter("version", _version, ParameterType.UrlSegment);
+ request.AddParameter("cursor", cursor, ParameterType.UrlSegment);
+
+ return request;
+ }
+
public RestRequest CreateThumbnailRequest(string path, ThumbnailSize size, string root)
{
var request = new RestRequest(Method.GET);
View
28 DropNet/Models/Delta2Page.cs
@@ -0,0 +1,28 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace DropNet.Models
+{
+ public class Delta2Page
+ {
+
+ public string Cursor { get; set; }
+ public bool HasMore { get; set; }
+ public bool Reset { get; set; }
+
+ private List<Delta2Entry> _entries = new List<Delta2Entry>();
+ public List<Delta2Entry> Entries
+ {
+ get { return _entries; }
+ set { _entries = value; }
+ }
+ }
+
+ public class Delta2Entry
+ {
+ public string Path { get; set; }
+ public MetaData MetaData { get; set; }
+ }
+}
View
10 DropNet/Models/ThumbnailSize.cs
@@ -3,23 +3,23 @@
public enum ThumbnailSize
{
/// <summary>
- /// 32px x 32px
+ /// 32px response 32px
/// </summary>
Small,
/// <summary>
- /// 64px x 64px
+ /// 64px response 64px
/// </summary>
Medium,
/// <summary>
- /// 128px x 128px
+ /// 128px response 128px
/// </summary>
Large,
/// <summary>
- /// 640px x 480px
+ /// 640px response 480px
/// </summary>
ExtraLarge,
/// <summary>
- /// 1024px x 768px
+ /// 1024px response 768px
/// </summary>
ExtraLarge2 //dumb name
}
Something went wrong with that request. Please try again.