Skip to content

Commit d5ddbcd

Browse files
committed
Add Profile Service
1 parent 50a4f75 commit d5ddbcd

File tree

8 files changed

+271
-0
lines changed

8 files changed

+271
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Specialized;
3+
using NUnit.Framework;
4+
using XboxWebApi.Common;
5+
using XboxWebApi.Extensions;
6+
using XboxWebApi.Services.Model;
7+
8+
namespace XboxWebApi.UnitTests.Api
9+
{
10+
[TestFixture]
11+
public class TestProfileModels : TestDataProvider
12+
{
13+
public TestProfileModels()
14+
: base("ApiProfile")
15+
{
16+
}
17+
18+
[Test]
19+
public void CreateProfileRequestQuery()
20+
{
21+
ProfileRequestQuery query = new ProfileRequestQuery(new ProfileSetting[]
22+
{
23+
ProfileSetting.AccountTier,
24+
ProfileSetting.GameDisplayName,
25+
ProfileSetting.PublicGamerpic
26+
});
27+
NameValueCollection nv = query.GetQuery();
28+
29+
Assert.IsNotEmpty(nv);
30+
Assert.AreEqual(1, nv.Count);
31+
Assert.AreEqual("AccountTier,GameDisplayName,PublicGamerpic", nv["settings"]);
32+
}
33+
34+
[Test]
35+
public void DeserializeProfileResponse()
36+
{
37+
string json = TestData["ProfileResponse.json"];
38+
ProfileResponse response = NewtonsoftJsonSerializer
39+
.Create(JsonNamingStrategy.CamelCase)
40+
.Deserialize<ProfileResponse>(json);
41+
42+
Assert.IsInstanceOf(typeof(IStringable), response);
43+
44+
Assert.IsNotNull(response.ProfileUsers);
45+
Assert.IsNotEmpty(response.ProfileUsers);
46+
Assert.AreEqual(1, response.ProfileUsers.Length);
47+
48+
Assert.AreEqual(2580478784034343, response.ProfileUsers[0].Id);
49+
Assert.AreEqual(2580478784034343, response.ProfileUsers[0].HostId);
50+
Assert.IsFalse(response.ProfileUsers[0].IsSponsoredUser);
51+
Assert.IsNotNull(response.ProfileUsers[0].Settings);
52+
Assert.IsNotEmpty(response.ProfileUsers[0].Settings);
53+
Assert.AreEqual(6, response.ProfileUsers[0].Settings.Length);
54+
55+
Assert.AreEqual(ProfileSetting.AppDisplayName, response.ProfileUsers[0].Settings[0].Id);
56+
Assert.AreEqual("Some Gamertag", response.ProfileUsers[0].Settings[0].Value);
57+
Assert.AreEqual(ProfileSetting.Gamerscore, response.ProfileUsers[0].Settings[1].Id);
58+
Assert.AreEqual("1337000", response.ProfileUsers[0].Settings[1].Value);
59+
Assert.AreEqual(ProfileSetting.Gamertag, response.ProfileUsers[0].Settings[2].Id);
60+
Assert.AreEqual("Some Gamertag", response.ProfileUsers[0].Settings[2].Value);
61+
Assert.AreEqual(ProfileSetting.PublicGamerpic, response.ProfileUsers[0].Settings[3].Id);
62+
Assert.AreEqual("http://images-eds.xboxlive.com/image?url=abcdef",
63+
response.ProfileUsers[0].Settings[3].Value);
64+
Assert.AreEqual(ProfileSetting.XboxOneRep, response.ProfileUsers[0].Settings[4].Id);
65+
Assert.AreEqual("Superstar", response.ProfileUsers[0].Settings[4].Value);
66+
Assert.AreEqual(ProfileSetting.RealName, response.ProfileUsers[0].Settings[5].Id);
67+
Assert.AreEqual("John Doe", response.ProfileUsers[0].Settings[5].Value);
68+
}
69+
}
70+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"profileUsers": [
3+
{
4+
"id": "2580478784034343",
5+
"hostId": "2580478784034343",
6+
"settings": [
7+
{
8+
"id": "AppDisplayName",
9+
"value": "Some Gamertag"
10+
},
11+
{
12+
"id": "Gamerscore",
13+
"value": "1337000"
14+
},
15+
{
16+
"id": "Gamertag",
17+
"value": "Some Gamertag"
18+
},
19+
{
20+
"id": "PublicGamerpic",
21+
"value": "http://images-eds.xboxlive.com/image?url=abcdef"
22+
},
23+
{
24+
"id": "XboxOneRep",
25+
"value": "Superstar"
26+
},
27+
{
28+
"id": "RealName",
29+
"value": "John Doe"
30+
}
31+
],
32+
"isSponsoredUser": false
33+
}
34+
]
35+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.Specialized;
4+
using RestSharp;
5+
using XboxWebApi.Common;
6+
using XboxWebApi.Extensions;
7+
using XboxWebApi.Services.Model;
8+
9+
namespace XboxWebApi.Services.Api
10+
{
11+
public class ProfileService : XblService
12+
{
13+
public ProfileService(XblConfiguration config)
14+
: base(config, "https://profile.xboxlive.com")
15+
{
16+
Headers = new NameValueCollection()
17+
{
18+
{"x-xbl-contract-version", "2"}
19+
};
20+
}
21+
22+
public ProfileResponse GetProfilesBatch(ulong[] xuids, ProfileSetting[] settings = null)
23+
{
24+
RestRequestEx request = new RestRequestEx("users/batch/profile/settings",
25+
Method.POST);
26+
ProfileSetting[] profileSettings = settings != null ? settings : new ProfileSetting[]
27+
{
28+
ProfileSetting.AppDisplayName, ProfileSetting.Gamerscore,
29+
ProfileSetting.Gamertag, ProfileSetting.PublicGamerpic,
30+
ProfileSetting.XboxOneRep, ProfileSetting.RealName
31+
};
32+
ProfilesRequest body = new ProfilesRequest(xuids, profileSettings);
33+
request.AddHeaders(Headers);
34+
request.AddJsonBody(body, JsonNamingStrategy.CamelCase);
35+
IRestResponse<ProfileResponse> response = Client(JsonNamingStrategy.CamelCase)
36+
.Execute<ProfileResponse>(request);
37+
return response.Data;
38+
}
39+
40+
private ProfileResponse _GetProfile(string resource, ProfileSetting[] settings = null)
41+
{
42+
RestRequestEx request = new RestRequestEx(resource, Method.GET);
43+
ProfileSetting[] profileSettings = settings != null ? settings : new ProfileSetting[]
44+
{
45+
ProfileSetting.AppDisplayName, ProfileSetting.Gamerscore,
46+
ProfileSetting.Gamertag, ProfileSetting.PublicGamerpic,
47+
ProfileSetting.XboxOneRep, ProfileSetting.RealName
48+
};
49+
ProfileRequestQuery query = new ProfileRequestQuery(profileSettings);
50+
request.AddHeaders(Headers);
51+
request.AddQueryParameters(query.GetQuery());
52+
IRestResponse<ProfileResponse> response = Client(JsonNamingStrategy.CamelCase)
53+
.Execute<ProfileResponse>(request);
54+
return response.Data;
55+
}
56+
57+
public ProfileResponse GetProfile(ulong xuid, ProfileSetting[] settings = null)
58+
{
59+
return _GetProfile($"users/xuid({xuid})/profile/settings", settings);
60+
}
61+
62+
public ProfileResponse GetProfile(string gamertag, ProfileSetting[] settings = null)
63+
{
64+
return _GetProfile($"users/gt({gamertag})/profile/settings", settings);
65+
}
66+
}
67+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Specialized;
3+
using XboxWebApi.Extensions;
4+
5+
namespace XboxWebApi.Services.Model
6+
{
7+
public class ProfileRequestQuery
8+
{
9+
public ProfileSetting[] Settings;
10+
11+
public ProfileRequestQuery(ProfileSetting[] settings)
12+
{
13+
Settings = settings;
14+
}
15+
16+
public NameValueCollection GetQuery()
17+
{
18+
return new NameValueCollection()
19+
{
20+
{"settings", String.Join(",", Array.ConvertAll(Settings, x => x.ToString()))}
21+
};
22+
}
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using XboxWebApi.Extensions;
3+
4+
namespace XboxWebApi.Services.Model
5+
{
6+
public class ProfileSettingElement : IStringable
7+
{
8+
public ProfileSetting Id;
9+
public string Value;
10+
}
11+
12+
public class ProfileUser : IStringable
13+
{
14+
public ulong Id;
15+
public ulong HostId;
16+
public ProfileSettingElement[] Settings;
17+
public bool IsSponsoredUser;
18+
}
19+
20+
public class ProfileResponse : IStringable
21+
{
22+
public ProfileUser[] ProfileUsers;
23+
}
24+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.ComponentModel;
3+
using XboxWebApi.Common;
4+
using Newtonsoft.Json;
5+
using Newtonsoft.Json.Converters;
6+
7+
namespace XboxWebApi.Services.Model
8+
{
9+
[JsonConverter(typeof(StringEnumConverter))]
10+
public enum ProfileSetting
11+
{
12+
GameDisplayName,
13+
AppDisplayName,
14+
AppDisplayPicRaw,
15+
GameDisplayPicRaw,
16+
PublicGamerpic,
17+
ShowUserAsAvatar,
18+
Gamerscore,
19+
Gamertag,
20+
AccountTier,
21+
TenureLevel,
22+
XboxOneRep,
23+
PreferredColor,
24+
Location,
25+
Bio,
26+
Watermarks,
27+
RealName,
28+
RealNameOverride
29+
}
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using XboxWebApi.Extensions;
4+
5+
using XboxWebApi.Common;
6+
7+
namespace XboxWebApi.Services.Model
8+
{
9+
public class ProfilesRequest
10+
{
11+
public ulong[] UserIds;
12+
public ProfileSetting[] Settings;
13+
14+
public ProfilesRequest(ulong[] xuids, ProfileSetting[] settings)
15+
{
16+
UserIds = xuids;
17+
Settings = settings;
18+
}
19+
}
20+
}

XboxWebApi/XboxWebApi.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<Folder Include="Services\Model\" />
3030
<Folder Include="Services\Model\People\" />
3131
<Folder Include="Services\Model\Presence\" />
32+
<Folder Include="Services\Model\Profile\" />
3233
<Folder Include="Services\Model\Message\" />
3334
</ItemGroup>
3435
<ItemGroup>

0 commit comments

Comments
 (0)