Пример #1
0
        public async Task <string> GetSpartanImageUrl(string gamertag, ImageSize size = ImageSize.ExtraSmall, Enumeration.CropType crop = Enumeration.CropType.Full)
        {
            gamertag = gamertag.ToUpper();
            var data = await _db.FindAsync <SpartanImageData>(gamertag);

            if (data == null || (DateTime.UtcNow - data.Timestamp.ToUniversalTime()).TotalDays > 3)
            {
                var query = new GetSpartanImage().ForPlayer(gamertag).Size((int)size).Crop(crop);
                var image = await _session.Query(query);

                if (image == null)
                {
                    return(null);
                }

                if (data == null)
                {
                    data = new SpartanImageData(gamertag, image.Uri, DateTime.UtcNow);
                    _db.InsertAsync(data);
                }
                else
                {
                    data.Data      = image.Uri;
                    data.Timestamp = DateTime.UtcNow;
                    //await _db.UpdateAsync(data);
                }
            }

            return(data.Data);
        }
Пример #2
0
        public void GetConstructedUri_NoParamaters_MatchesExpected()
        {
            var query = new GetSpartanImage();

            var uri = query.GetConstructedUri();

            Assert.AreEqual($"profile/h5/profiles/{null}/spartan{null}", uri);
        }
Пример #3
0
        public async Task GetSpartanImage_MissingGamertag()
        {
            var query = new GetSpartanImage();

            await Global.Session.Query(query);

            Assert.Fail("An exception should have been thrown");
        }
Пример #4
0
        public void GetConstructedUri_NoParamaters_MatchesExpected()
        {
            var query = new GetSpartanImage();

            var uri = query.GetConstructedUri();

            Assert.AreEqual(string.Format(BaseUri, null, null), uri);
        }
Пример #5
0
        public async Task GetSpartanImage_InvalidGamertag(string gamertag)
        {
            var query = new GetSpartanImage(gamertag);

            await Global.Session.Query(query);

            Assert.Fail("An exception should have been thrown");
        }
Пример #6
0
        public void GetConstructedUri_NoParameters_MatchesExpected()
        {
            var query = new GetSpartanImage();

            var uri = query.GetConstructedUri();

            Assert.AreEqual($"profile/h5/profiles/{null}/spartan{null}", uri);
        }
Пример #7
0
        public async Task GetSpartanImage(string gamertag)
        {
            var query = new GetSpartanImage()
                .ForPlayer(gamertag);

            var result = await Global.Session.Query(query);

            Assert.IsInstanceOf(typeof (GetImage), result);
        }
Пример #8
0
        public void GetConstructedUri_ForPlayer_MatchesExpected(string gamertag)
        {
            var query = new GetSpartanImage()
                        .ForPlayer(gamertag);

            var uri = query.GetConstructedUri();

            Assert.AreEqual($"profile/h5/profiles/{gamertag}/spartan{null}", uri);
        }
Пример #9
0
        public void GetConstructedUri_Crop_MatchesExpected(Enumeration.CropType cropType)
        {
            var query = new GetSpartanImage()
                .Crop(cropType);

            var uri = query.GetConstructedUri();

            Assert.AreEqual(string.Format(BaseUri, null, $"?crop={cropType}"), uri);
        }
Пример #10
0
        public void GetConstructedUri_ForPlayer_MatchesExpected(string gamertag)
        {
            var query = new GetSpartanImage()
                .ForPlayer(gamertag);

            var uri = query.GetConstructedUri();

            Assert.AreEqual($"profile/h5/profiles/{gamertag}/spartan{null}", uri);
        }
Пример #11
0
        public void GetConstructedUri_Size_MatchesExpected(int size)
        {
            var query = new GetSpartanImage()
                .Size(size);

            var uri = query.GetConstructedUri();

            Assert.AreEqual($"profile/h5/profiles/{null}/spartan?size={size}", uri);
        }
Пример #12
0
        public void GetConstructedUri_Size_MatchesExpected(int size)
        {
            var query = new GetSpartanImage()
                        .Size(size);

            var uri = query.GetConstructedUri();

            Assert.AreEqual($"profile/h5/profiles/{null}/spartan?size={size}", uri);
        }
Пример #13
0
        public void GetConstructedUri_Crop_MatchesExpected(Enumeration.CropType cropType)
        {
            var query = new GetSpartanImage()
                        .Crop(cropType);

            var uri = query.GetConstructedUri();

            Assert.AreEqual($"profile/h5/profiles/{null}/spartan?crop={cropType}", uri);
        }
Пример #14
0
        public async Task GetSpartanImage(string gamertag)
        {
            var query = new GetSpartanImage()
                        .ForPlayer(gamertag);

            var result = await Global.Session.Query(query);

            Assert.IsInstanceOf(typeof(GetImage), result);
        }
Пример #15
0
        public async Task GetSpartanImage_IsSerializable(string gamertag)
        {
            var query = new GetSpartanImage()
                        .ForPlayer("Furiousn00b");

            var result = await Global.Session.Query(query);

            SerializationUtility <GetImage> .AssertRoundTripSerializationIsPossible(result);
        }
Пример #16
0
        public void GetConstructedUri_Crop_MatchesExpected(Enumeration.CropType cropType)
        {
            var query = new GetSpartanImage()
                .Crop(cropType);

            var uri = query.GetConstructedUri();

            Assert.AreEqual($"profile/h5/profiles/{null}/spartan?crop={cropType}", uri);
        }
Пример #17
0
        public void GetConstructedUri_ForPlayer_MatchesExpected(string gamertag)
        {
            var query = new GetSpartanImage()
                .ForPlayer(gamertag);

            var uri = query.GetConstructedUri();

            Assert.AreEqual(string.Format(BaseUri, gamertag, null), uri);
        }
Пример #18
0
        public void GetConstructedUri_Size_MatchesExpected(int size)
        {
            var query = new GetSpartanImage()
                .Size(size);

            var uri = query.GetConstructedUri();

            Assert.AreEqual(string.Format(BaseUri, null, $"?size={size}"), uri);
        }
Пример #19
0
        public async Task GetSpartanImage_Crop()
        {
            var query = new GetSpartanImage()
                .ForPlayer("Furiousn00b")
                .Crop(Enumeration.CropType.Portrait);

            var result = await Session.Query(query);

            Assert.IsInstanceOf(typeof(Image), result);
        }
Пример #20
0
        public async Task GetSpartanImage_Size(int size)
        {
            var query = new GetSpartanImage()
                        .ForPlayer("Furiousn00b")
                        .Size(size);

            var result = await Global.Session.Query(query);

            Assert.IsInstanceOf(typeof(GetImage), result);
        }
Пример #21
0
        public async Task GetSpartanImage_Size(int size)
        {
            var query = new GetSpartanImage()
                .ForPlayer("Furiousn00b")
                .Size(size);

            var result = await Session.Query(query);

            Assert.IsInstanceOf(typeof(Image), result);
        }
Пример #22
0
        public async Task GetSpartanImage_InvalidSize(int size)
        {
            var query = new GetSpartanImage()
                        .ForPlayer("Furiousn00b")
                        .Size(size);

            await Global.Session.Query(query);

            Assert.Fail("An exception should have been thrown");
        }
Пример #23
0
        public async Task GetSpartanImage_Crop()
        {
            var query = new GetSpartanImage()
                        .ForPlayer("Furiousn00b")
                        .Crop(Enumeration.CropType.Portrait);

            var result = await Global.Session.Query(query);

            Assert.IsInstanceOf(typeof(GetImage), result);
        }
Пример #24
0
        public void GetConstructedUri_Complex_MatchesExpected(string gamertag, int size, Enumeration.CropType cropType)
        {
            var query = new GetSpartanImage()
                        .ForPlayer(gamertag)
                        .Size(size)
                        .Crop(cropType);

            var uri = query.GetConstructedUri();

            Assert.AreEqual($"profile/h5/profiles/{gamertag}/spartan?size={size}&crop={cropType}", uri);
        }
Пример #25
0
        public async Task GetSpartanImage(string gamertag, int size, Enumeration.Halo5.CropType crop)
        {
            var query = new GetSpartanImage(gamertag)
                        .Size(size)
                        .Crop(crop)
                        .SkipCache();

            var result = await Global.Session.Query(query);

            Assert.IsInstanceOf(typeof(HaloImage), result);
        }
Пример #26
0
        public async Task GetSpartanImage_IsSerializable(string gamertag, int size, Enumeration.Halo5.CropType crop)
        {
            var query = new GetSpartanImage(gamertag)
                        .Size(size)
                        .Crop(crop)
                        .SkipCache();

            var result = await Global.Session.Query(query);

            SerializationUtility <HaloImage> .AssertRoundTripSerializationIsPossible(result);
        }
Пример #27
0
        public void GetConstructedUri_Complex_MatchesExpected(string gamertag, int size, Enumeration.CropType cropType)
        {
            var query = new GetSpartanImage()
                .ForPlayer(gamertag)
                .Size(size)
                .Crop(cropType);

            var uri = query.GetConstructedUri();

            Assert.AreEqual(string.Format(BaseUri, gamertag, $"?size={size}&crop={cropType}"), uri);
        }
Пример #28
0
        public void GetConstructedUri_Complex_MatchesExpected(string gamertag, int size, Enumeration.CropType cropType)
        {
            var query = new GetSpartanImage()
                .ForPlayer(gamertag)
                .Size(size)
                .Crop(cropType);

            var uri = query.GetConstructedUri();

            Assert.AreEqual($"profile/h5/profiles/{gamertag}/spartan?size={size}&crop={cropType}", uri);
        }
Пример #29
0
        public void Uri_MatchesExpected(string gamertag, int size, Enumeration.Halo5.CropType crop)
        {
            var query = new GetSpartanImage(gamertag);

            Assert.AreEqual($"https://www.haloapi.com/profile/h5/profiles/{gamertag}/spartan", query.Uri);

            query.Size(size);

            Assert.AreEqual($"https://www.haloapi.com/profile/h5/profiles/{gamertag}/spartan?size={size}", query.Uri);

            query.Crop(crop);

            Assert.AreEqual($"https://www.haloapi.com/profile/h5/profiles/{gamertag}/spartan?size={size}&crop={crop}", query.Uri);
        }
Пример #30
0
        public static void Validate(this GetSpartanImage getSpartanImage)
        {
            var validationResult = new ValidationResult();

            if (!getSpartanImage.Player.IsValidGamertag())
            {
                validationResult.Messages.Add("GetSpartanImage query requires a valid Gamertag (Player) to be set.");
            }

            if (getSpartanImage.Parameters.ContainsKey("size"))
            {
                var validSizes = new List <int> {
                    95, 128, 190, 256, 512
                };

                int size;
                var parsed = int.TryParse(getSpartanImage.Parameters["size"], out size);

                if (!parsed || !validSizes.Contains(size))
                {
                    validationResult.Messages.Add($"GetSpartanImage optional paramater 'Size' is invalid: {size}.");
                }
            }

            if (getSpartanImage.Parameters.ContainsKey("crop"))
            {
                var crop = getSpartanImage.Parameters["crop"];

                var defined = Enum.IsDefined(typeof(Enumeration.CropType), crop);

                if (!defined)
                {
                    validationResult.Messages.Add($"GetSpartanImage optional paramater 'Crop' is invalid: {crop}.");
                }
            }

            if (!validationResult.Success)
            {
                throw new ValidationException(validationResult.Messages);
            }
        }
Пример #31
0
        public async Task GetSpartanImage_IsSerializable(string gamertag)
        {
            var query = new GetSpartanImage()
                .ForPlayer(gamertag);

            var result = await Global.Session.Query(query);

            var serializationUtility = new SerializationUtility<GetImage>();
            serializationUtility.AssertRoundTripSerializationIsPossible(result);
        }
Пример #32
0
        public async Task GetSpartanImage_InvalidGamertag(string gamertag)
        {
            var query = new GetSpartanImage()
                .ForPlayer(gamertag);

            try
            {
                await Global.Session.Query(query);
                Assert.Fail("An exception should have been thrown");
            }
            catch (HaloApiException e)
            {
                Assert.AreEqual((int)Enumeration.StatusCode.BadRequest, e.HaloApiError.StatusCode);
            }
            catch (System.Exception e)
            {
                Assert.Fail("Unexpected exception of type {0} caught: {1}", e.GetType(), e.Message);
            }
        }
Пример #33
0
        public async Task GetSpartanImage_IsSerializable(string gamertag)
        {
            var query = new GetSpartanImage()
                .ForPlayer("Furiousn00b")
                .SkipCache();

            var result = await Global.Session.Query(query);

            SerializationUtility<GetImage>.AssertRoundTripSerializationIsPossible(result);
        }
Пример #34
0
        public async Task GetSpartanImage_InvalidSize(int size)
        {
            var query = new GetSpartanImage()
                .ForPlayer("Furiousn00b")
                .Size(size);

            await Global.Session.Query(query);
            Assert.Fail("An exception should have been thrown");
        }
Пример #35
0
        public async Task GetSpartanImage_InvalidGamertag(string gamertag)
        {
            var query = new GetSpartanImage()
                .ForPlayer(gamertag);

            await Global.Session.Query(query);
            Assert.Fail("An exception should have been thrown");
        }
Пример #36
0
        public async Task GetSpartanImage_MissingGamertag()
        {
            var query = new GetSpartanImage();

            await Global.Session.Query(query);
            Assert.Fail("An exception should have been thrown");
        }