-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlickrAsync.cs
163 lines (144 loc) · 5.33 KB
/
FlickrAsync.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using FlickrNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Backupr
{
public partial class FlickrAsync
{
private Flickr _flickrService;
public FlickrAsync(Flickr flickrService)
{
_flickrService = flickrService;
}
//TaskCompletionSource<int>
private static void setResult<T>(TaskCompletionSource<T> tcs, FlickrResult<T> args)
{
if (args.HasError)
{
tcs.SetException(args.Error);
return;
}
if (tcs.Task.IsCanceled)
{
tcs.SetCanceled();
return;
}
try
{
tcs.SetResult(args.Result);
}
catch (Exception e)
{
tcs.SetException(e);
}
}
public class Args<TResult>
{
private TaskCompletionSource<TResult> tcs;
public Args(TaskCompletionSource<TResult> tcs, Flickr flickr)
{
this.tcs = tcs;
this.Service = flickr;
}
public Flickr Service { get; private set; }
public void SetResult(FlickrResult<TResult> args)
{
setResult(tcs, args);
}
}
private async Task<TResult> CallSvc<TResult>(Action<Args<TResult>> caller)
{
try
{
return await callSvcOnce<TResult>(caller);
//return task.ContinueWith<TResult>(t => t.IsFaulted ? callSvcOnce<TResult>(caller).Result : t.Result);
}
catch(Exception exx)
{
//zkus znovu
return await callSvcOnce<TResult>(caller);
}
}
private Task<TResult> callSvcOnce<TResult>(Action<Args<TResult>> caller)
{
var tcs = new TaskCompletionSource<TResult>();
caller(new Args<TResult>(tcs, _flickrService));
return tcs.Task;
}
public Task<Auth> AuthOAuthCheckToken()
{
return CallSvc<Auth>(a => a.Service.AuthOAuthCheckTokenAsync(a.SetResult));
}
public Task<PhotosetCollection> PhotosetsGetList()
{
return CallSvc<PhotosetCollection>(a => a.Service.PhotosetsGetListAsync(a.SetResult));
}
public Task<NoResponse> PhotosetsReorder(string[] photosetIds)
{
return CallSvc<NoResponse>(a => a.Service.PhotosetsOrderSetsAsync(photosetIds,a.SetResult));
}
public Task<NoResponse> PhotosetReorderPhotos(string photosetId,string[] photoIds)
{
return CallSvc<NoResponse>(a => a.Service.PhotosetsReorderPhotosAsync(photosetId,photoIds, a.SetResult));
}
public Task<PhotosetPhotoCollection> PhotosetsGetPhotos(string photosetId)
{
return CallSvc<PhotosetPhotoCollection>(a => a.Service.PhotosetsGetPhotosAsync(photosetId, PhotoSearchExtras.Tags | PhotoSearchExtras.Description | PhotoSearchExtras.DateTaken, a.SetResult));
}
public Task<PhotoCollection> PhotosGetNotInSet(int page, int perPage)
{
return CallSvc<PhotoCollection>(a => a.Service.PhotosGetNotInSetAsync(page, perPage, PhotoSearchExtras.Tags | PhotoSearchExtras.Description | PhotoSearchExtras.DateTaken, a.SetResult));
}
public Task<NoResponse> PhotosetsAddPhoto(string photosetId, string photoId)
{
return CallSvc<NoResponse>(a => a.Service.PhotosetsAddPhotoAsync(photosetId, photoId, a.SetResult));
}
public Task<NoResponse> PhotosetsDelete(string photosetId)
{
return CallSvc<NoResponse>(a => a.Service.PhotosetsDeleteAsync(photosetId, a.SetResult));
}
public Task<NoResponse> PhotoDelete(string photoId)
{
return CallSvc<NoResponse>(a => a.Service.PhotosDeleteAsync(photoId, a.SetResult));
}
public Task<Photoset> PhotosetsCreate(string title, string primaryPhotoId)
{
return CallSvc<Photoset>(a => a.Service.PhotosetsCreateAsync(title, primaryPhotoId, a.SetResult));
}
public Task<string> UploadPicture(IUploadPictureParams upload)
{
return CallSvc<string>(a =>
{
var s = upload.GetStream();
a.Service.UploadPictureAsync(s, upload.FileName, upload.Title, upload.Description, upload.Tags, upload.IsPublic, upload.IsFamily, upload.IsFriend, upload.ContentType, upload.SafetyLevel, upload.HiddenFromSearch,
args =>
{
a.SetResult(args);
if (s != null)
{
s?.Dispose();
s = null;
}
});
});
}
}
public interface IUploadPictureParams
{
string FileName { get; }
string Title { get; }
string Description { get; }
string Tags { get; }
bool IsPublic { get; }
bool IsFamily { get; }
bool IsFriend { get; }
ContentType ContentType { get; }
SafetyLevel SafetyLevel { get; }
HiddenFromSearch HiddenFromSearch { get; }
Stream GetStream();
}
}