public static IEnumerable <Cronofy.AvailablePeriod> Availability(Models.Availability availability)
        {
            IEnumerable <Cronofy.AvailablePeriod> availablePeriods = null;

            var participants = new ParticipantGroupBuilder()
                               .AddMember(availability.AccountId1);

            if (availability.AccountId2 != null)
            {
                participants.AddMember(availability.AccountId2);
            }

            if (availability.RequiredParticipants == "All")
            {
                participants.AllRequired();
            }

            var builtAvailabilityRequest = new AvailabilityRequestBuilder()
                                           .AddParticipantGroup(participants)
                                           .RequiredDuration(availability.Duration)
                                           .AddAvailablePeriod(availability.Start, availability.End)
                                           .Build();

            try
            {
                availablePeriods = CronofyAccountRequest <IEnumerable <Cronofy.AvailablePeriod> >(() => { return(AccountClient.GetAvailability(builtAvailabilityRequest)); });
                LogHelper.Log("Availability success - accountId1=`{availability.AccountId1}` - accountId2=`{availability.AccountId2}` - requiredParticipants=`{availability.RequiredParticipants}` - duration=`{availability.Duration}` - start=`{availability.Start}` - end=`{availability.End}` - periods=`{JsonConvert.SerializeObject(availablePeriods)}`");
            }
            catch (CronofyException)
            {
                LogHelper.Log("Availability failure - accountId1=`{availability.AccountId1}` - accountId2=`{availability.AccountId2}` - requiredParticipants=`{availability.RequiredParticipants}` - duration=`{availability.Duration}` - start=`{availability.Start}` - end=`{availability.End}`");
                throw;
            }

            return(availablePeriods);
        }
        public void CanPerformComplexAvailabilityQuery()
        {
            const string requestBody = @"
                {
                  ""participants"": [
                    {
                      ""members"": [
                        { ""sub"": ""acc_567236000909002"" },
                        {
                          ""sub"": ""acc_678347111010113"",
                          ""available_periods"": [
                            {
                              ""start"": ""2017-01-03 09:00:00Z"",
                              ""end"": ""2017-01-03 12:00:00Z""
                            }
                          ],
                          ""calendar_ids"": [
                            ""cal_1234_5678"",
                            ""cal_9876_5432""
                          ]
                        }
                      ],
                      ""required"": ""all""
                    },
                    {
                      ""members"": [
                        { ""sub"": ""acc_678910200909001"" },
                        { ""sub"": ""acc_879082061010114"" }
                      ],
                      ""required"": 1
                    }
                  ],
                  ""required_duration"": { ""minutes"": 60 },
                  ""available_periods"": [
                    {
                      ""start"": ""2017-01-03 09:00:00Z"",
                      ""end"": ""2017-01-03 18:00:00Z""
                    }
                  ]
                }";

            var member = new AvailabilityMemberBuilder()
                         .Sub("acc_678347111010113")
                         .AddAvailablePeriod(
                new DateTimeOffset(2017, 1, 3, 9, 0, 0, TimeSpan.Zero),
                new DateTimeOffset(2017, 1, 3, 12, 0, 0, TimeSpan.Zero))
                         .CalendarIds(new[] { "cal_1234_5678", "cal_9876_5432" });

            var requiredGroup = new ParticipantGroupBuilder()
                                .AddMember("acc_567236000909002")
                                .AddMember(member)
                                .AllRequired();

            var representedGroup = new ParticipantGroupBuilder()
                                   .AddMember("acc_678910200909001")
                                   .AddMember("acc_879082061010114")
                                   .Required(1);

            var builder = new AvailabilityRequestBuilder()
                          .RequiredDuration(60)
                          .AddAvailablePeriod(
                new DateTimeOffset(2017, 1, 3, 9, 0, 0, TimeSpan.Zero),
                new DateTimeOffset(2017, 1, 3, 18, 0, 0, TimeSpan.Zero))
                          .AddParticipantGroup(requiredGroup)
                          .AddParticipantGroup(representedGroup);

            const string responseBody = @"
                {
                  ""available_periods"": [
                    {
                      ""start"": ""2017-01-03T09:00:00Z"",
                      ""end"": ""2017-01-03T11:00:00Z"",
                      ""participants"": [
                        { ""sub"": ""acc_567236000909002"" },
                        { ""sub"": ""acc_678347111010113"" },
                        { ""sub"": ""acc_6789010200909001"" }
                      ]
                    }
                  ]
                }";

            Http.Stub(
                HttpPost
                .Url("https://api.cronofy.com/v1/availability")
                .RequestHeader("Authorization", "Bearer " + AccessToken)
                .JsonRequest(requestBody)
                .ResponseCode(200)
                .ResponseBody(responseBody)
                );

            var availability = Client.GetAvailability(builder);

            var expected = new[]
            {
                new AvailablePeriod
                {
                    Start        = new DateTimeOffset(2017, 1, 3, 9, 0, 0, TimeSpan.Zero),
                    End          = new DateTimeOffset(2017, 1, 3, 11, 0, 0, TimeSpan.Zero),
                    Participants = new[]
                    {
                        new AvailablePeriod.Participant {
                            Sub = "acc_567236000909002"
                        },
                        new AvailablePeriod.Participant {
                            Sub = "acc_678347111010113"
                        },
                        new AvailablePeriod.Participant {
                            Sub = "acc_6789010200909001"
                        },
                    },
                },
            };

            Assert.AreEqual(expected, availability);
        }