Skip to content

Commit cb67976

Browse files
author
daniel
committed
Demo code to verify functionality of WP7 multipart POST with streams (isolated storage, and manifest resource)
1 parent 06d71b6 commit cb67976

File tree

10 files changed

+187
-25
lines changed

10 files changed

+187
-25
lines changed

lib/Configuration.dll

22 KB
Binary file not shown.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.Practices.Mobile.Configuration;
2+
3+
namespace Demo.WindowsPhone.Configuration
4+
{
5+
public class AppSettingElement : ConfigurationElement
6+
{
7+
[ConfigurationProperty("key", IsRequired = true)]
8+
public string Key
9+
{
10+
get
11+
{
12+
return (string)this["key"];
13+
}
14+
set
15+
{
16+
this["key"] = value;
17+
}
18+
}
19+
20+
[ConfigurationProperty("value", IsRequired = true)]
21+
public string Value
22+
{
23+
get
24+
{
25+
return (string)this["value"];
26+
}
27+
set
28+
{
29+
this["value"] = value;
30+
}
31+
}
32+
}
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.Practices.Mobile.Configuration;
2+
3+
namespace Demo.WindowsPhone.Configuration
4+
{
5+
public class AppSettingsElementCollection : ConfigurationElementCollection
6+
{
7+
protected override ConfigurationElement CreateNewElement()
8+
{
9+
return new AppSettingElement();
10+
}
11+
12+
protected override object GetElementKey(ConfigurationElement element)
13+
{
14+
var e = (AppSettingElement)element;
15+
16+
return e.Key;
17+
}
18+
19+
public new AppSettingElement this[string name]
20+
{
21+
get
22+
{
23+
return (AppSettingElement)BaseGet(name);
24+
}
25+
}
26+
}
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.Practices.Mobile.Configuration;
2+
3+
namespace Demo.WindowsPhone.Configuration
4+
{
5+
public class ApplicationSettingsSection : ConfigurationSection
6+
{
7+
/// <summary>
8+
/// Get the connection Items.
9+
/// </summary>
10+
[ConfigurationProperty("appSettings")]
11+
public AppSettingsElementCollection AppSettings
12+
{
13+
get
14+
{
15+
return (AppSettingsElementCollection)(this["appSettings"]);
16+
}
17+
}
18+
}
19+
}

src/Demo.WindowsPhone/Demo.WindowsPhone.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
<WarningLevel>4</WarningLevel>
4949
</PropertyGroup>
5050
<ItemGroup>
51+
<Reference Include="Configuration">
52+
<HintPath>..\..\lib\Configuration.dll</HintPath>
53+
</Reference>
5154
<Reference Include="Microsoft.Phone" />
5255
<Reference Include="Microsoft.Phone.Interop" />
5356
<Reference Include="Newtonsoft.Json.Silverlight">
@@ -64,6 +67,9 @@
6467
<Compile Include="App.xaml.cs">
6568
<DependentUpon>App.xaml</DependentUpon>
6669
</Compile>
70+
<Compile Include="Configuration\ApplicationSettingsSection.cs" />
71+
<Compile Include="Configuration\AppSettingElement.cs" />
72+
<Compile Include="Configuration\AppSettingsElementCollection.cs" />
6773
<Compile Include="MainPage.xaml.cs">
6874
<DependentUpon>MainPage.xaml</DependentUpon>
6975
</Compile>
@@ -86,10 +92,18 @@
8692
</Page>
8793
</ItemGroup>
8894
<ItemGroup>
95+
<Content Include="..\net35\Hammock.Tests\app.config">
96+
<Link>app.config</Link>
97+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
98+
</Content>
8999
<None Include="Properties\AppManifest.xml" />
90100
<None Include="Properties\WMAppManifest.xml" />
91101
</ItemGroup>
92102
<ItemGroup>
103+
<EmbeddedResource Include="..\net35\Hammock.Tests\_failwhale.jpg">
104+
<Link>_failwhale.jpg</Link>
105+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
106+
</EmbeddedResource>
93107
<Content Include="ApplicationIcon.png">
94108
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
95109
</Content>

src/Demo.WindowsPhone/MainPage.xaml.cs

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,121 @@
11
using System.Collections.Generic;
2+
using System.Diagnostics;
23
using System.IO;
4+
using System.IO.IsolatedStorage;
35
using System.Linq;
4-
using System.Text;
6+
using System.Net;
7+
using System.Reflection;
58
using System.Windows;
9+
using Demo.WindowsPhone.Configuration;
610
using Demo.WindowsPhone.Models;
711
using Demo.WindowsPhone.Serialization;
812
using Demo.WindowsPhone.ViewModels;
913
using Hammock;
14+
using Hammock.Authentication.OAuth;
1015
using Hammock.Silverlight.Compat;
1116
using Hammock.Web;
17+
using Microsoft.Practices.Mobile.Configuration;
1218

1319
namespace Demo.WindowsPhone
1420
{
1521
public partial class MainPage
1622
{
23+
private readonly string _consumerKey;
24+
private readonly string _consumerSecret;
25+
private readonly string _accessToken;
26+
private readonly string _accessTokenSecret;
27+
private readonly string _twitPicKey;
28+
1729
public MainPage()
1830
{
1931
InitializeComponent();
2032

33+
var section = (ApplicationSettingsSection)ConfigurationManager.GetSection("ApplicationSettings");
34+
_consumerKey = section.AppSettings["ConsumerKey"].Value;
35+
_consumerSecret = section.AppSettings["ConsumerSecret"].Value;
36+
_accessToken = section.AppSettings["AccessToken"].Value;
37+
_accessTokenSecret = section.AppSettings["AccessTokenSecret"].Value;
38+
_twitPicKey = section.AppSettings["TwitPicKey"].Value;
39+
40+
PreloadResources();
41+
2142
Loaded += MainPageLoaded;
2243
}
44+
45+
private static void PreloadResources()
46+
{
47+
var store = IsolatedStorageFile.GetUserStoreForApplication();
48+
if(store.FileExists("_failwhale.jpg"))
49+
{
50+
store.DeleteFile("_failwhale.jpg");
51+
}
52+
MoveToIsolatedStorage("_failwhale.jpg");
53+
}
54+
55+
private static void MoveToIsolatedStorage(string path)
56+
{
57+
var store = IsolatedStorageFile.GetUserStoreForApplication();
58+
59+
if (store.GetFileNames(path).Length != 0)
60+
{
61+
return;
62+
}
63+
64+
using (var target = new IsolatedStorageFileStream(path, FileMode.Create, store))
65+
{
66+
path = string.Format("Demo.WindowsPhone.{0}", path);
67+
68+
using(var source = Assembly.GetExecutingAssembly().GetManifestResourceStream(path))
69+
{
70+
if (source != null)
71+
{
72+
var content = new byte[source.Length];
73+
source.Read(content, 0, content.Length);
74+
target.Write(content, 0, content.Length);
75+
}
76+
}
77+
}
78+
}
2379

2480
private void MainPageLoaded(object sender, RoutedEventArgs e)
2581
{
2682
LoadPublicTweets();
2783

28-
RestClient client = new RestClient
84+
var client = new RestClient
85+
{
86+
Authority = "http://api.twitpic.com/",
87+
VersionPath = "2"
88+
};
89+
90+
// Prepare an OAuth Echo request to TwitPic
91+
var request = PrepareEchoRequest();
92+
request.Path = "uploadAndPost.xml";
93+
request.AddField("key", _twitPicKey); // <-- Sign up with TwitPic to get an API key
94+
request.AddField("message", "Failwhale!");
95+
request.AddFile("media", "failwhale", "_failwhale.jpg", "image/jpeg"); // <-- This overload uses IsolatedStorage
96+
97+
client.BeginRequest(request, (req, resp, state) => Debug.Assert(resp.StatusCode == HttpStatusCode.OK));
98+
}
99+
100+
public RestRequest PrepareEchoRequest()
101+
{
102+
var client = new RestClient
29103
{
30-
Authority = "http://www.tumblr.com/api"
104+
Authority = "https://api.twitter.com",
105+
VersionPath = "1",
106+
UserAgent = "TweetSharp"
31107
};
32108

33-
RestRequest request = new RestRequest
109+
var request = new RestRequest
34110
{
35-
Path = "/write",
36-
Method = WebMethod.Post
111+
Method = WebMethod.Get,
112+
Path = "account/verify_credentials.json",
113+
Credentials = OAuthCredentials.ForProtectedResource(
114+
_consumerKey, _consumerSecret, _accessToken, _accessTokenSecret
115+
)
37116
};
38117

39-
const string data = "This is a test stream";
40-
var capturedImage = new MemoryStream(Encoding.UTF8.GetBytes(data));
41-
42-
request.AddParameter("email", "email");
43-
request.AddParameter("password", "password");
44-
request.AddParameter("generator", "YABA - Windows Phone 7");
45-
request.AddParameter("type", "photo");
46-
request.AddParameter("caption", "caption");
47-
request.AddFile("data", "image1.jpg", capturedImage, "image/jpeg");
48-
client.BeginRequest(request);
118+
return OAuthCredentials.DelegateWith(client, request);
49119
}
50120

51121
private void LoadPublicTweets()

src/Hammock.sln

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,17 @@ Global
241241
HideSolutionNode = FALSE
242242
EndGlobalSection
243243
GlobalSection(NestedProjects) = preSolution
244-
{6679078A-C585-4CB6-96E9-908DBDAA2716} = {0F12EB19-73E5-4F38-BEF2-070C625508B7}
244+
{0F12EB19-73E5-4F38-BEF2-070C625508B7} = {A08E9715-A3CE-41E1-AACF-37B4961DB700}
245+
{3FB92FF7-A9F8-4F15-864B-69E7EBC3E16B} = {A08E9715-A3CE-41E1-AACF-37B4961DB700}
246+
{6052AEDE-F8B1-4E24-B791-DBB7FF9621D3} = {A08E9715-A3CE-41E1-AACF-37B4961DB700}
245247
{E0461346-CB62-489E-9893-CFEA249F8380} = {0F12EB19-73E5-4F38-BEF2-070C625508B7}
246248
{3BB0DC97-ECE3-4AC8-B2D0-E4C14D600179} = {0F12EB19-73E5-4F38-BEF2-070C625508B7}
247249
{0D6CBA8E-3846-4559-AA4E-AEE3373A03CB} = {0F12EB19-73E5-4F38-BEF2-070C625508B7}
250+
{6679078A-C585-4CB6-96E9-908DBDAA2716} = {0F12EB19-73E5-4F38-BEF2-070C625508B7}
248251
{265F976E-7BAE-408A-82EE-1E692F5662DF} = {0F12EB19-73E5-4F38-BEF2-070C625508B7}
249252
{73015806-7BE4-4BAB-AA07-ACC719EFE85A} = {0F12EB19-73E5-4F38-BEF2-070C625508B7}
250-
{2E420750-6124-473B-808D-41755C907648} = {0D6CBA8E-3846-4559-AA4E-AEE3373A03CB}
251253
{3808F0EB-14CF-4919-87F3-97FB9D225A80} = {0D6CBA8E-3846-4559-AA4E-AEE3373A03CB}
254+
{2E420750-6124-473B-808D-41755C907648} = {0D6CBA8E-3846-4559-AA4E-AEE3373A03CB}
252255
{CD569558-9092-466B-8670-EA1B151150E0} = {3FB92FF7-A9F8-4F15-864B-69E7EBC3E16B}
253256
{487B7E3C-9689-47BC-8785-73CCD92A3749} = {3FB92FF7-A9F8-4F15-864B-69E7EBC3E16B}
254257
{7465B7B5-3E20-4EA2-8785-BDE71CAFB59D} = {3FB92FF7-A9F8-4F15-864B-69E7EBC3E16B}
@@ -263,10 +266,7 @@ Global
263266
{960E427F-67E0-42A5-86ED-FC6A342CDCB2} = {265F976E-7BAE-408A-82EE-1E692F5662DF}
264267
{F9695086-8EF6-43E3-AC5F-275FE918FA3A} = {DBEBA14C-000D-464E-8CC8-CF44CAE866F4}
265268
{80647B8C-E162-4E34-A758-590280F11B97} = {6052AEDE-F8B1-4E24-B791-DBB7FF9621D3}
266-
{6052AEDE-F8B1-4E24-B791-DBB7FF9621D3} = {A08E9715-A3CE-41E1-AACF-37B4961DB700}
267-
{0F12EB19-73E5-4F38-BEF2-070C625508B7} = {A08E9715-A3CE-41E1-AACF-37B4961DB700}
268-
{3FB92FF7-A9F8-4F15-864B-69E7EBC3E16B} = {A08E9715-A3CE-41E1-AACF-37B4961DB700}
269-
{D75610E8-9074-4073-AECD-EF5FCE97E428} = {61197CB4-50C4-4D41-AB80-B0C27767C29E}
270269
{50851227-5502-4F59-B821-8F474E81A2B1} = {61197CB4-50C4-4D41-AB80-B0C27767C29E}
270+
{D75610E8-9074-4073-AECD-EF5FCE97E428} = {61197CB4-50C4-4D41-AB80-B0C27767C29E}
271271
EndGlobalSection
272272
EndGlobal

src/net20/Hammock/Hammock.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<DebugType>full</DebugType>
1919
<Optimize>false</Optimize>
2020
<OutputPath>bin\Debug\</OutputPath>
21-
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<DefineConstants>TRACE;DEBUG;NET20</DefineConstants>
2222
<ErrorReport>prompt</ErrorReport>
2323
<WarningLevel>4</WarningLevel>
2424
</PropertyGroup>

src/net35/Hammock.ClientProfile/Hammock.ClientProfile.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<DebugType>full</DebugType>
2020
<Optimize>false</Optimize>
2121
<OutputPath>bin\Debug\</OutputPath>
22-
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<DefineConstants>TRACE;DEBUG;ClientProfiles</DefineConstants>
2323
<ErrorReport>prompt</ErrorReport>
2424
<WarningLevel>4</WarningLevel>
2525
</PropertyGroup>

src/net35/Hammock/Authentication/OAuth/OAuthWebQuery.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ private string GetOAuthUrl(string url)
9090

9191
protected override string AppendParameters(string url)
9292
{
93-
// Base behavior
9493
return AppendParameters(url, true /* escape */, false /* skipOAuth */);
9594
}
9695

0 commit comments

Comments
 (0)