Global Sessions - Thundermaker300/RoSharp GitHub Wiki

Global Sessions provide an alternative to the original approach of providing a Session class to every API member that needs it. Instead, a single session can be "assigned" to the GlobalSession manager, and that session will be used for every API member for the remainder of the program. This approach is meant for programs that only use one session.

Assignment

using RoSharp;
using RoSharp.Utility;

Session session = new Session();
await session.LoginAsync(".ROBLOSECURITY_CODE_HERE");

GlobalSession.AssignSession(session);

// Access the assigned session
Console.WriteLine(GlobalSession.Assigned.API.User.Name); // Output the authenticated user's username, by accessing the assigned session in the GlobalSession.

The session created above is now the session that will be used for API members. Global sessions must not be null and must have already called LoginAsync and successfully authenticated, OR have an API key attached through the SetAPIKey method.

If an API member is not provided a session, it will default to using the global session. A different session can still be provided to API members and that session will be used over the global session.

[!WARNING] If a global session is not assigned when an object, such as an Experience is created (such as from Experience.FromId), it will NOT be added to the instance if a global session is assigned later. It is encouraged to assign the session globally before creating any instances.

De-Assignment

using RoSharp;
using RoSharp.Utility;

GlobalSession.Unassign()

This API member removes the assigned global session.

Using global session

Most API do not require a session to be passed as a parameter. In these cases, a session just shouldn't be provided and the system will automatically use the global session. However, APIs such as the PriceFloorAPI's methods require a session parameter. In these cases, null can be provided instead.

using RoSharp;
using RoSharp.API;
using RoSharp.Enums;

int? priceForBodyAssets = await PriceFloorAPI.GetPriceFloorForTypeAsync(AssetType.Body, null)