public static async Task <Unit> Run(InfusioClient client) { var address = new EmailAddress(email: "*****@*****.**"); /* * The methods on client return a data type with two possible values. * Either<InfusioError, T> */ var either = await client.ListContacts(email : address.Email); /* * Or you can use the unsafe versions which will throw exceptions as they occur */ var list = await client.ListContactsUnsafe(email : address.Email); var contact = list.Contacts.FirstOrDefault(); if (contact != null) { var tags = await client.ListTagsUnsafe(); var remoteTag = tags.InnerTags.FirstOrDefault(x => x.Name == "developers"); if (remoteTag == null) { remoteTag = await client.CreateTagUnsafe(new CreateTag(name : "developers")); } await client.ApplyTagsToContactIdUnsafe(new TagId(List(remoteTag.Id ?? 0)), contact.Id ?? 0); contact = contact.Copy(tagIds: contact.TagIds.Add(remoteTag.Id ?? 0)); } /* * Of course, you can use the client in a monadic manner as well. * One of the differences between this and the DSL is that the DSL is lazy and only * constructs somewhat of a syntax tree. You can also interpret the DSL anyway you please. * You can't do that with the client. It's executed always. */ // var clientProgram = // from c in client.ListContacts(email: address.Email).ToAsync().Match( // Left: _ => Left<InfusioError, FullContact>(_), // Right: x => x.Contacts.HeadOrNone().Map(identity).IfNone(() => // // I give up ;) // client.CreateContact(new RequestContact(emailAddresses: List(address))) // ) // ) return(unit); }
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(new HttpLogger()); 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("PASTE CODE HERE")) .Map(CacheAuthorizationInfo) ); var client = new InfusioClient(httpClient, new InfusioConfig(authorization.Token)); // DSL Demo await InfusioDslDemo.Run(client); // Api Client Demo await ClassicApiClientDemo.Run(client); }
public static async Task <Unit> Run(InfusioClient client) { /* * =============== * STEP 1 * =============== * Describe the Infusionsoft operation that want to execute. * InfusioOp objects 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. * You can, however, use this library as a traditional API client as well. * See the Classic Api Client demo for more information. * =============== */ InfusioOp <FullContact> operation = CustomOperations.AddTagToContact( new Tag(name: "developers"), new EmailAddress("*****@*****.**") ); /* * =============== * STEP 2 * =============== * Execute your operation. * This returns a data type with two possible values. * Either<InfusioError, T> * =============== */ var result = await operation.RunWith(client); return(result.Match( Left: error => Console.WriteLine($"error: {error.Value}"), Right: contact => Console.WriteLine($"contact: {JsonConvert.SerializeObject(contact, Formatting.Indented)}") )); }
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("*****@*****.**") ); /* * =============== * 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)}"); } ); }