-
Notifications
You must be signed in to change notification settings - Fork 6
/
Program.cs
97 lines (86 loc) · 3.71 KB
/
Program.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
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Infusio;
using Infusio.Auth;
using Infusio.Http;
using Infusio.Model;
using LanguageExt;
using Newtonsoft.Json;
namespace Demo
{
using static Prelude;
using static TokenCache;
using static Formatting;
using static JsonConvert;
using static Authorization;
class Program
{
static async Task Main()
{
// These credentials are a test account.
// I don't care about the data.
// of course, you would provide your own.
var httpClient = new HttpClient();
var clientId = ClientId("tj7a3rtbs5dmsz2sbwxx3phd");
var clientSecret = ClientSecret("EEecE6bBYz");
var redirectUri = RedirectUri("https://localhost");
// These are meant to be created once and used throughout your application.
// Inject into your DI container, or whatever your preference.
AccessTokenRequest requestAccessToken = AccessTokenRequest(httpClient, clientId, clientSecret, redirectUri);
RefreshTokenRequest requestRefreshToken = RefreshTokenRequest(httpClient, clientId, clientSecret);
/*
* ===============
* STEP 1
* ===============
* Infusionsoft does not provide a way for an application to automatically authenticate.
* They currently require a user's consent.
*
* To grab an access code from infusionsoft, complete the following steps.
* 1. Point your browser to:
* https://accounts.infusionsoft.com/app/oauth/authorize?client_id=tj7a3rtbs5dmsz2sbwxx3phd&response_type=code&redirect_uri=https://localhost
* 2. Click the "Allow" button
* 3. Copy the code that is returned in the adddress bar of your browser
* 4. Paste the code below in the AccessCode
* ===============
*/
// Utilize cache so we can run this program many times without the above hassle.
// This is not part of the core library.
var authorization = await AuthorizationInfoFromCache().IfNoneAsync(() =>
requestAccessToken(AccessCode("72ycjp593vxzgaf9f7fxhus3")).Map(CacheAuthorizationInfo)
);
var client = new InfusioClient(httpClient, authorization);
/*
* ===============
* STEP 2
* ===============
* Describe the Infusionsoft operation that want to execute.
* InfusioOps are composable. You combine any number of InfusioOps together to form one InfusioOp.
* You're not stuck with running one operation at a time like you're probably used to traditionally.
* ===============
*/
InfusioOp<FullContact> operation = CustomOperations.AddTagToContact(
new Tag(name: "developers"),
new EmailAddress("chris+demo@caliberweb.com")
);
/*
* ===============
* STEP 3
* ===============
* Execute your operation.
* This returns a data type with two possible values.
* Either<InfusioError, T>
* ===============
*/
var result = await operation.RunWithLogs(client, List("Infusionsoft operations", "start"));
result.Match(
Left: error => Console.WriteLine($"error: {error.Value}"),
Right: res =>
{
res.Logs.Iter(Console.WriteLine);
Console.WriteLine($"contact: {SerializeObject(res.Value, Indented)}");
}
);
}
}
}