Skip to content

Commit

Permalink
Hosting: EdgegapPlugin updated to 3.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
miwarnec committed Jan 14, 2025
2 parents 74a0733 + 698cc04 commit 357c91e
Show file tree
Hide file tree
Showing 34 changed files with 3,171 additions and 1,640 deletions.
12 changes: 12 additions & 0 deletions Assets/Mirror/Hosting/Edgegap/.config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"csharpier": {
"version": "0.29.2",
"commands": [
"dotnet-csharpier"
]
}
}
}
4 changes: 3 additions & 1 deletion Assets/Mirror/Hosting/Edgegap/Edgegap.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "Edgegap",
"rootNamespace": "",
"references": [],
"includePlatforms": [],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
Expand Down
62 changes: 40 additions & 22 deletions Assets/Mirror/Hosting/Edgegap/Editor/Api/EdgegapApiBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,25 @@ private string GetBaseUrl() =>
protected EdgegapApiBase(
ApiEnvironment apiEnvironment,
string apiToken,
EdgegapWindowMetadata.LogLevel logLevel = EdgegapWindowMetadata.LogLevel.Error)
EdgegapWindowMetadata.LogLevel logLevel = EdgegapWindowMetadata.LogLevel.Error
)
{
this.SelectedApiEnvironment = apiEnvironment;

this._httpClient.BaseAddress = new Uri($"{GetBaseUrl()}/");
this._httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
new MediaTypeWithQualityHeaderValue("application/json")
);

string cleanedApiToken = apiToken.Replace("token ", ""); // We already prefixed token below
this._httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("token", cleanedApiToken);
this._httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"token",
cleanedApiToken
);

this.LogLevel = logLevel;
}


#region HTTP Requests
/// <summary>
/// POST | We already added "https://api.edgegap.com/" (or similar) BaseAddress via constructor.
Expand All @@ -64,7 +67,10 @@ protected EdgegapApiBase(
/// - Success => returns HttpResponseMessage result
/// - Error => Catches errs => returns null (no rethrow)
/// </returns>
protected async Task<HttpResponseMessage> PostAsync(string relativePath = "", string json = "{}")
protected async Task<HttpResponseMessage> PostAsync(
string relativePath = "",
string json = "{}"
)
{
StringContent stringContent = CreateStringContent(json);
Uri uri = new Uri(_httpClient.BaseAddress, relativePath); // Normalize POST uri: Can't end with `/`.
Expand Down Expand Up @@ -92,7 +98,10 @@ protected async Task<HttpResponseMessage> PostAsync(string relativePath = "", st
/// - Success => returns HttpResponseMessage result
/// - Error => Catches errs => returns null (no rethrow)
/// </returns>
protected async Task<HttpResponseMessage> PatchAsync(string relativePath = "", string json = "{}")
protected async Task<HttpResponseMessage> PatchAsync(
string relativePath = "",
string json = "{}"
)
{
StringContent stringContent = CreateStringContent(json);
Uri uri = new Uri(_httpClient.BaseAddress, relativePath); // Normalize PATCH uri: Can't end with `/`.
Expand Down Expand Up @@ -129,11 +138,12 @@ protected async Task<HttpResponseMessage> PatchAsync(string relativePath = "", s
/// - Success => returns HttpResponseMessage result
/// - Error => Catches errs => returns null (no rethrow)
/// </returns>
protected async Task<HttpResponseMessage> GetAsync(string relativePath = "", string customQuery = "")
protected async Task<HttpResponseMessage> GetAsync(
string relativePath = "",
string customQuery = ""
)
{
string completeRelativeUri = prepareEdgegapUriWithQuery(
relativePath,
customQuery);
string completeRelativeUri = prepareEdgegapUriWithQuery(relativePath, customQuery);

if (IsLogLevelDebug)
Debug.Log($"GetAsync to: `{completeRelativeUri} with customQuery: `{customQuery}`");
Expand All @@ -160,18 +170,23 @@ protected async Task<HttpResponseMessage> GetAsync(string relativePath = "", str
/// - Success => returns HttpResponseMessage result
/// - Error => Catches errs => returns null (no rethrow)
/// </returns>
protected async Task<HttpResponseMessage> DeleteAsync(string relativePath = "", string customQuery = "")
protected async Task<HttpResponseMessage> DeleteAsync(
string relativePath = "",
string customQuery = ""
)
{
string completeRelativeUri = prepareEdgegapUriWithQuery(
relativePath,
customQuery);
string completeRelativeUri = prepareEdgegapUriWithQuery(relativePath, customQuery);

if (IsLogLevelDebug)
Debug.Log($"DeleteAsync to: `{completeRelativeUri} with customQuery: `{customQuery}`");
Debug.Log(
$"DeleteAsync to: `{completeRelativeUri} with customQuery: `{customQuery}`"
);

try
{
return await ExecuteRequestAsync(() => _httpClient.DeleteAsync(completeRelativeUri));
return await ExecuteRequestAsync(
() => _httpClient.DeleteAsync(completeRelativeUri)
);
}
catch (Exception e)
{
Expand All @@ -186,7 +201,8 @@ protected async Task<HttpResponseMessage> DeleteAsync(string relativePath = "",
/// <returns></returns>
private static async Task<HttpResponseMessage> ExecuteRequestAsync(
Func<Task<HttpResponseMessage>> requestFunc,
CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default
)
{
HttpResponseMessage response = null;
try
Expand Down Expand Up @@ -215,16 +231,18 @@ private static async Task<HttpResponseMessage> ExecuteRequestAsync(
// Check for a successful status code
if (response == null)
{
Debug.Log("!Success (null response) - returning 500");
Debug.Log("Error: (null response) - returning 500");
return CreateUnknown500Err();
}

if (!response.IsSuccessStatusCode)
{
HttpMethod httpMethod = response.RequestMessage.Method;
Debug.Log($"!Success: {(short)response.StatusCode} {response.ReasonPhrase} - " +
$"{httpMethod} | {response.RequestMessage.RequestUri}` - " +
$"{response.Content?.ReadAsStringAsync().Result}");
Debug.Log(
$"Error: {(short)response.StatusCode} {response.ReasonPhrase} - "
+ $"{httpMethod} | {response.RequestMessage.RequestUri}` - "
+ $"{response.Content?.ReadAsStringAsync().Result}"
);
}

return response;
Expand Down
44 changes: 42 additions & 2 deletions Assets/Mirror/Hosting/Edgegap/Editor/Api/EdgegapAppApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,49 @@ public async Task<EdgegapHttpResult<UpsertAppVersionResult>> CreateAppVersion(Cr

return result;
}

/// <summary>
/// GET to v1/apps
/// - Get all applications.
/// - API Doc | https://docs.edgegap.com/api/#tag/Applications/operation/applications-get
/// </summary>
/// <returns>
/// Http info with GetAppsResult data model
/// - Success: 200
/// </returns>
public async Task<EdgegapHttpResult<GetAppsResult>> GetApps()
{
HttpResponseMessage response = await GetAsync($"v1/apps");
EdgegapHttpResult<GetAppsResult> result = new EdgegapHttpResult<GetAppsResult>(response); // MIRROR CHANGE: 'new()' not supported in Unity 2020

bool isSuccess = response.StatusCode == HttpStatusCode.OK; // 200
if (!isSuccess)
return result;

return result;
}

/// <summary>
/// GET to v1/app/{appName}/versions
/// </summary>
/// <returns>
/// Http info with GetAppVersionsResult data model
/// - Success: 200
/// </returns>
public async Task<EdgegapHttpResult<GetAppVersionsResult>> GetAppVersions(string appName)
{
HttpResponseMessage response = await GetAsync($"v1/app/{appName}/versions");
EdgegapHttpResult<GetAppVersionsResult> result = new EdgegapHttpResult<GetAppVersionsResult>(response);

bool isSuccess = response.StatusCode == HttpStatusCode.OK; // 200
if (!isSuccess)
return result;

return result;
}
#endregion // API Methods


#region Chained API Methods
/// <summary>
/// PATCH and/or POST to v1/app/: Upsert an *existing* application version with new specifications.
Expand Down
Loading

0 comments on commit 357c91e

Please sign in to comment.