I am developing a multiplayer game. I have a friend who developing API on Swagger.io . So here is my problem:
I want to use Task function that I can use instead of Unity's own UnityWebRequest function. I look After doing some research, I wrote the following codes.
private async Task<T> Send<T>(string service, object obj)
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest(service)
{
RequestFormat = DataFormat.Json,
Method = Method.POST,
};
#if UNITY_ANDROID
uniqueID = SystemInfo.deviceUniqueIdentifier;
platform = "1";
#endif
#if UNITY_IOS
uniqueID = UIDevice.identifierForVendor
platform = "0";
#endif
request.AddHeader("DeviceID", uniqueID);
request.AddHeader("BundleID", Application.identifier);
request.AddHeader("BuildID", "0");
request.AddHeader("Platform", platform);
request.AddHeader("Version", "0");
request.AddJsonBody(obj);
var response = await client.ExecuteAsync(request);
if (response.Content != null && response.StatusCode == HttpStatusCode.OK)
{
var jsonSerializerSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore };
var result = JsonConvert.DeserializeObject<T>(response.Content, jsonSerializerSetting);
return result;
}
else
{
return default;
}
Since it is an async function, I call it with await without any problems. Like this :
I have some class that can response some other class :
[Serializable]
public class WelcomeRequest : Response
{
public string Location;
public string Language;
}
[Serializable]
public class WelcomeRequest : Response
{
public string Location;
public string Language;
}[Serializable]
public class WelcomeResponse : Response
{
public WelcomePayload Payload;
}
[Serializable]
public class WelcomePayload : Response
{
public Player Player;
public Settings Settings;
public Season Play;
public Season Purchase;
public Game GameOfSeason;
public Game[] Games;
public double PrizePool;
}
(I know it's a big project and there are a lot of subclasses. I don't want you to write code for me, I just want you to explain the logic of the code I wrote and an example that I can reference. Not to mention that I'm trying to do it alone. I don't have any friends who can help you but you.) I am able to receive files in json format from the server. But since this function only returns the object, I cannot assign sub-objects of the class. Do I need to convert it to json format again and deserialize it? Since I was working with the Firebase infrastructure before, I was able to communicate without any problems. I looked for Restsharp Unity tutorials but couldn't find any. Can you help me? At least if you guide me I will try to solve it myself. I'm sorry if I didn't express myself properly as this is my first post on this site and I don't know the rules very well. Endless thanks to all of you.
CodePudding user response:
This doesn't really answer your question but you might want to consider using a generated client.
If the API is made in swagger you should be able to make a C# API client using openapi-generator and the Swagger Schema. Then just build the generated project and drop the DLL in the unity projects Assets folder.
You probably also need to include RestSharp and Newtonsoft.Json. Just download these NuGet packages and unpack them (nupkg files are just zips) the dll should be in there. Include it by dropping the DLL in the Assets folder.
CodePudding user response:
public class Error: Response
{
public string Field; //The name of the area where the problem was detected
public string Error_; //Contains the code for the error message
public string HighLight; //It's used for the data to be highlighted in the error
field
}
This is my response class. I want to send it by assigning the "Payload" variables in the response response object of the request. I couldn't figure out how to do.