Skip to content

Commit

Permalink
Add Profile Service
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxuser committed Jun 8, 2018
1 parent 50a4f75 commit d5ddbcd
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 0 deletions.
70 changes: 70 additions & 0 deletions XboxWebApi.Tests/Api/TestProfileModels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Specialized;
using NUnit.Framework;
using XboxWebApi.Common;
using XboxWebApi.Extensions;
using XboxWebApi.Services.Model;

namespace XboxWebApi.UnitTests.Api
{
[TestFixture]
public class TestProfileModels : TestDataProvider
{
public TestProfileModels()
: base("ApiProfile")
{
}

[Test]
public void CreateProfileRequestQuery()
{
ProfileRequestQuery query = new ProfileRequestQuery(new ProfileSetting[]
{
ProfileSetting.AccountTier,
ProfileSetting.GameDisplayName,
ProfileSetting.PublicGamerpic
});
NameValueCollection nv = query.GetQuery();

Assert.IsNotEmpty(nv);
Assert.AreEqual(1, nv.Count);
Assert.AreEqual("AccountTier,GameDisplayName,PublicGamerpic", nv["settings"]);
}

[Test]
public void DeserializeProfileResponse()
{
string json = TestData["ProfileResponse.json"];
ProfileResponse response = NewtonsoftJsonSerializer
.Create(JsonNamingStrategy.CamelCase)
.Deserialize<ProfileResponse>(json);

Assert.IsInstanceOf(typeof(IStringable), response);

Assert.IsNotNull(response.ProfileUsers);
Assert.IsNotEmpty(response.ProfileUsers);
Assert.AreEqual(1, response.ProfileUsers.Length);

Assert.AreEqual(2580478784034343, response.ProfileUsers[0].Id);
Assert.AreEqual(2580478784034343, response.ProfileUsers[0].HostId);
Assert.IsFalse(response.ProfileUsers[0].IsSponsoredUser);
Assert.IsNotNull(response.ProfileUsers[0].Settings);
Assert.IsNotEmpty(response.ProfileUsers[0].Settings);
Assert.AreEqual(6, response.ProfileUsers[0].Settings.Length);

Assert.AreEqual(ProfileSetting.AppDisplayName, response.ProfileUsers[0].Settings[0].Id);
Assert.AreEqual("Some Gamertag", response.ProfileUsers[0].Settings[0].Value);
Assert.AreEqual(ProfileSetting.Gamerscore, response.ProfileUsers[0].Settings[1].Id);
Assert.AreEqual("1337000", response.ProfileUsers[0].Settings[1].Value);
Assert.AreEqual(ProfileSetting.Gamertag, response.ProfileUsers[0].Settings[2].Id);
Assert.AreEqual("Some Gamertag", response.ProfileUsers[0].Settings[2].Value);
Assert.AreEqual(ProfileSetting.PublicGamerpic, response.ProfileUsers[0].Settings[3].Id);
Assert.AreEqual("http://images-eds.xboxlive.com/image?url=abcdef",
response.ProfileUsers[0].Settings[3].Value);
Assert.AreEqual(ProfileSetting.XboxOneRep, response.ProfileUsers[0].Settings[4].Id);
Assert.AreEqual("Superstar", response.ProfileUsers[0].Settings[4].Value);
Assert.AreEqual(ProfileSetting.RealName, response.ProfileUsers[0].Settings[5].Id);
Assert.AreEqual("John Doe", response.ProfileUsers[0].Settings[5].Value);
}
}
}
35 changes: 35 additions & 0 deletions XboxWebApi.Tests/TestData/ApiProfile/ProfileResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"profileUsers": [
{
"id": "2580478784034343",
"hostId": "2580478784034343",
"settings": [
{
"id": "AppDisplayName",
"value": "Some Gamertag"
},
{
"id": "Gamerscore",
"value": "1337000"
},
{
"id": "Gamertag",
"value": "Some Gamertag"
},
{
"id": "PublicGamerpic",
"value": "http://images-eds.xboxlive.com/image?url=abcdef"
},
{
"id": "XboxOneRep",
"value": "Superstar"
},
{
"id": "RealName",
"value": "John Doe"
}
],
"isSponsoredUser": false
}
]
}
67 changes: 67 additions & 0 deletions XboxWebApi/Services/Api/ProfileService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using RestSharp;
using XboxWebApi.Common;
using XboxWebApi.Extensions;
using XboxWebApi.Services.Model;

namespace XboxWebApi.Services.Api
{
public class ProfileService : XblService
{
public ProfileService(XblConfiguration config)
: base(config, "https://profile.xboxlive.com")
{
Headers = new NameValueCollection()
{
{"x-xbl-contract-version", "2"}
};
}

public ProfileResponse GetProfilesBatch(ulong[] xuids, ProfileSetting[] settings = null)
{
RestRequestEx request = new RestRequestEx("users/batch/profile/settings",
Method.POST);
ProfileSetting[] profileSettings = settings != null ? settings : new ProfileSetting[]
{
ProfileSetting.AppDisplayName, ProfileSetting.Gamerscore,
ProfileSetting.Gamertag, ProfileSetting.PublicGamerpic,
ProfileSetting.XboxOneRep, ProfileSetting.RealName
};
ProfilesRequest body = new ProfilesRequest(xuids, profileSettings);
request.AddHeaders(Headers);
request.AddJsonBody(body, JsonNamingStrategy.CamelCase);
IRestResponse<ProfileResponse> response = Client(JsonNamingStrategy.CamelCase)
.Execute<ProfileResponse>(request);
return response.Data;
}

private ProfileResponse _GetProfile(string resource, ProfileSetting[] settings = null)
{
RestRequestEx request = new RestRequestEx(resource, Method.GET);
ProfileSetting[] profileSettings = settings != null ? settings : new ProfileSetting[]
{
ProfileSetting.AppDisplayName, ProfileSetting.Gamerscore,
ProfileSetting.Gamertag, ProfileSetting.PublicGamerpic,
ProfileSetting.XboxOneRep, ProfileSetting.RealName
};
ProfileRequestQuery query = new ProfileRequestQuery(profileSettings);
request.AddHeaders(Headers);
request.AddQueryParameters(query.GetQuery());
IRestResponse<ProfileResponse> response = Client(JsonNamingStrategy.CamelCase)
.Execute<ProfileResponse>(request);
return response.Data;
}

public ProfileResponse GetProfile(ulong xuid, ProfileSetting[] settings = null)
{
return _GetProfile($"users/xuid({xuid})/profile/settings", settings);
}

public ProfileResponse GetProfile(string gamertag, ProfileSetting[] settings = null)
{
return _GetProfile($"users/gt({gamertag})/profile/settings", settings);
}
}
}
24 changes: 24 additions & 0 deletions XboxWebApi/Services/Model/Profile/ProfileRequestQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Specialized;
using XboxWebApi.Extensions;

namespace XboxWebApi.Services.Model
{
public class ProfileRequestQuery
{
public ProfileSetting[] Settings;

public ProfileRequestQuery(ProfileSetting[] settings)
{
Settings = settings;
}

public NameValueCollection GetQuery()
{
return new NameValueCollection()
{
{"settings", String.Join(",", Array.ConvertAll(Settings, x => x.ToString()))}
};
}
}
}
24 changes: 24 additions & 0 deletions XboxWebApi/Services/Model/Profile/ProfileResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using XboxWebApi.Extensions;

namespace XboxWebApi.Services.Model
{
public class ProfileSettingElement : IStringable
{
public ProfileSetting Id;
public string Value;
}

public class ProfileUser : IStringable
{
public ulong Id;
public ulong HostId;
public ProfileSettingElement[] Settings;
public bool IsSponsoredUser;
}

public class ProfileResponse : IStringable
{
public ProfileUser[] ProfileUsers;
}
}
30 changes: 30 additions & 0 deletions XboxWebApi/Services/Model/Profile/ProfileSetting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.ComponentModel;
using XboxWebApi.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace XboxWebApi.Services.Model
{
[JsonConverter(typeof(StringEnumConverter))]
public enum ProfileSetting
{
GameDisplayName,
AppDisplayName,
AppDisplayPicRaw,
GameDisplayPicRaw,
PublicGamerpic,
ShowUserAsAvatar,
Gamerscore,
Gamertag,
AccountTier,
TenureLevel,
XboxOneRep,
PreferredColor,
Location,
Bio,
Watermarks,
RealName,
RealNameOverride
}
}
20 changes: 20 additions & 0 deletions XboxWebApi/Services/Model/Profile/ProfilesRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using XboxWebApi.Extensions;

using XboxWebApi.Common;

namespace XboxWebApi.Services.Model
{
public class ProfilesRequest
{
public ulong[] UserIds;
public ProfileSetting[] Settings;

public ProfilesRequest(ulong[] xuids, ProfileSetting[] settings)
{
UserIds = xuids;
Settings = settings;
}
}
}
1 change: 1 addition & 0 deletions XboxWebApi/XboxWebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<Folder Include="Services\Model\" />
<Folder Include="Services\Model\People\" />
<Folder Include="Services\Model\Presence\" />
<Folder Include="Services\Model\Profile\" />
<Folder Include="Services\Model\Message\" />
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit d5ddbcd

Please sign in to comment.