|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Diagnostics; |
| 3 | +using TalonOne.Api; |
| 4 | +using TalonOne.Client; |
| 5 | +using TalonOne.Model; |
| 6 | + |
| 7 | +namespace _example |
| 8 | +{ |
| 9 | + class Program |
| 10 | + { |
| 11 | + static void Main(string[] args) |
| 12 | + { |
| 13 | + // Configure BasePath & API key authorization: api_key_v1 |
| 14 | + var integrationConfig = new Configuration { |
| 15 | + BasePath = "http://localhost:9000", |
| 16 | + ApiKey = new Dictionary<string, string> { |
| 17 | + { "Authorization", System.Environment.GetEnvironmentVariable("TALON_API_KEY") } |
| 18 | + }, |
| 19 | + ApiKeyPrefix = new Dictionary<string, string> { |
| 20 | + { "Authorization", "ApiKey-v1" } |
| 21 | + } |
| 22 | + }; |
| 23 | + |
| 24 | + // ************************************************ |
| 25 | + // Integration API example to send a session update |
| 26 | + // ************************************************ |
| 27 | + |
| 28 | + // When using the default approach, the next initiation of `IntegrationApi` |
| 29 | + // could be using the empty constructor |
| 30 | + var integrationApi = new IntegrationApi(integrationConfig); |
| 31 | + var customerSessionId = "my_unique_session_integration_id_2"; // string | The custom identifier for this session, must be unique within the account. |
| 32 | + |
| 33 | + // Preparing a NewCustomerSessionV2 object |
| 34 | + NewCustomerSessionV2 customerSession = new NewCustomerSessionV2 { |
| 35 | + ProfileId = "PROFILE_ID", |
| 36 | + CouponCodes = new List<string> { |
| 37 | + "Cool-Stuff-2020" |
| 38 | + }, |
| 39 | + CartItems = new List<CartItem> { |
| 40 | + new CartItem( |
| 41 | + name: "Hummus Tahini", |
| 42 | + sku: "hum-t", |
| 43 | + quantity: 1, |
| 44 | + price: (decimal)5.5, |
| 45 | + category: "Food" |
| 46 | + ), |
| 47 | + new CartItem( |
| 48 | + name: "Iced Mint Lemonade", |
| 49 | + sku: "ice-mn-lemon", |
| 50 | + quantity: 1, |
| 51 | + price: (decimal)3.5, |
| 52 | + category: "Beverages" |
| 53 | + ) |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + // Instantiating an IntegrationRequest object |
| 58 | + IntegrationRequest body = new IntegrationRequest( |
| 59 | + customerSession |
| 60 | + // Optional list of requested information to be present on the response. |
| 61 | + // See src/TalonOne/Model/IntegrationRequest#ResponseContentEnum for full list of supported values |
| 62 | + // new List<IntegrationRequest.ResponseContentEnum> { |
| 63 | + // IntegrationRequest.ResponseContentEnum.CustomerSession, |
| 64 | + // IntegrationRequest.ResponseContentEnum.CustomerProfile |
| 65 | + // } |
| 66 | + ); |
| 67 | + |
| 68 | + // Create/update a customer session using `UpdateCustomerSessionV2` function |
| 69 | + IntegrationStateV2 response = integrationApi.UpdateCustomerSessionV2(customerSessionId, body); |
| 70 | + Debug.WriteLine(response); |
| 71 | + |
| 72 | + // Parsing the returned effects list, please consult https://developers.talon.one/Integration-API/handling-effects-v2 for the full list of effects and their corresponding properties |
| 73 | + foreach (Effect effect in response.Effects) { |
| 74 | + switch(effect.EffectType) { |
| 75 | + case "setDiscount": |
| 76 | + // Initiating right props instance according to the effect type |
| 77 | + SetDiscountEffectProps setDiscountEffectProps = (SetDiscountEffectProps) Newtonsoft.Json.JsonConvert.DeserializeObject(effect.Props.ToString(), typeof(SetDiscountEffectProps)); |
| 78 | + |
| 79 | + // Access the specific effect's properties |
| 80 | + Debug.WriteLine("Set a discount '{0}' of {1:00.000}", setDiscountEffectProps.Name, setDiscountEffectProps.Value); |
| 81 | + break; |
| 82 | + // case "acceptCoupon": |
| 83 | + // AcceptCouponEffectProps acceptCouponEffectProps = (AcceptCouponEffectProps) Newtonsoft.Json.JsonConvert.DeserializeObject(effect.Props.ToString(), typeof(AcceptCouponEffectProps)); |
| 84 | + |
| 85 | + // Work with AcceptCouponEffectProps' properties |
| 86 | + // ... |
| 87 | + // break; |
| 88 | + default: |
| 89 | + Debug.WriteLine("Encounter unknown effect type: {0}", effect.EffectType); |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments