Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push Kit API v2 support #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The following table describes main classes used in the sample code.
| Class Name| Description|
| ----------- | ----------- |
|AppOptions|App-related configuration.|
|ApiVersion|Push Kit API version.|
|AGConnectApp|App.|
|AGConnectMessaging|Push Kit API calling methods.|
|Message|Message body.|
Expand All @@ -39,10 +40,11 @@ The following table describes the parameters related to the **AppOptions** class

| Parameter| Description|
| ----------- | ----------- |
|ProjectId|Project ID, which is obtained from the project information.|
|ClientId|App ID, which is obtained from the app information.|
|ClientSecret|App secret, which is obtained from the app information.|
|LoginUri|URL for Huawei OAuth 2.0 to obtain a token. For details, please refer to [OAuth 2.0-based Authentication](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/oauth2-0000001212610981).|
|ApiBaseUri|Access address of Push Kit. For details, please refer to [Downlink Message Sending](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/android-server-dev-0000001050040110?ha_source=hms1).|
|ApiVersion|Push Kit API version. For details, please refer to [Sending Downlink Messages](https://developer.huawei.com/consumer/en/doc/HMSCore-References/https-send-api-0000001050986197).|

## Environment Requirements

Expand Down
4 changes: 3 additions & 1 deletion README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ C#示例代码对华为推送服务(HUAWEI Push Kit)服务端接口进行封
| 类名 | 说明 |
| ----------- | ----------- |
|AppOptions|应用相关配置|
|ApiVersion|推送服务API版本。|
|AGConnectApp|应用|
|AGConnectMessaging|推送服务相关接口的调用方法|
|Message|消息体|
Expand All @@ -40,10 +41,11 @@ C#示例代码对华为推送服务(HUAWEI Push Kit)服务端接口进行封

| 参数 | 说明 |
| ----------- | ----------- |
|ProjectId|项目ID,从项目信息中获取。|
|ClientId|应用ID,从应用信息中获取|
|ClientSecret|应用访问密钥,从应用信息中获取|
|LoginUri|华为OAuth 2.0获取token的地址。详情请参见[基于OAuth 2.0开放鉴权-客户端模式](https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides/oauth2-0000001212610981#section128682386159?ha_source=hms1)。|
|ApiBaseUri|推送服务的访问地址。详情请参见[推送服务-下行消息](https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides/android-server-dev-0000001050040110?ha_source=hms1)。|
|ApiVersion|推送服务API版本。 详情请参见【下行消息发送】(https://developer.huawei.com/consumer/cn/doc/HMSCore-References/https-send-api-0000001050986197)。|

## 环境要求

Expand Down
4 changes: 4 additions & 0 deletions src/AGConnectAdmin.Examples/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace AGConnectAdmin.Examples
{
public partial class Example
{
private const ApiVersion _apiVersion = ApiVersion.V1;

#region Tokens
private const string TOKEN_IOS = "your ios token";
private const string TOKEN_ANDROID = "your android token";
Expand All @@ -33,6 +35,8 @@ public Example(ITestOutputHelper logger)

AGConnectApp.Create(new AppOptions()
{
ApiVersion = _apiVersion,
ProjectId = "your project id",
ClientId = "your client id",
ClientSecret = "your cliient secret",
});
Expand Down
5 changes: 3 additions & 2 deletions src/AGConnectAdmin.Tests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void GlobalInit()
{
Initialized = true;
AGConnectApp.Create(TestUtils.ReadOptionsFromDisk());
}
}
}

public static AppOptions ReadOptionsFromDisk()
Expand All @@ -40,7 +40,8 @@ public static AppOptions ReadOptionsFromDisk()
return new AppOptions()
{
LoginUri = keyValuePairs["login_uri"],
ApiBaseUri = keyValuePairs["api_base_uri"],
ApiVersion = ApiVersion.V2,
ProjectId = keyValuePairs["dev_proj_id"],
ClientId = keyValuePairs["dev_app_id"],
ClientSecret = keyValuePairs["client_secret"]
};
Expand Down
4 changes: 2 additions & 2 deletions src/AGConnectAdmin.Tests/options.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"login_uri": "https://login.cloud.huawei.com/oauth2/v2/token",
"api_base_uri": "https://push-api.cloud.huawei.com",
"login_uri": "https://login.cloud.huawei.com/oauth2/v3/token",
"dev_app_id": "YOUR_REGISTRATION_APPID",
"dev_proj_id": "YOUR_REGISTRATION_PROJID",
"client_secret": "YOUR_REGISTRATION_APPSECRET"
}
41 changes: 40 additions & 1 deletion src/AGConnectAdmin/AGConnectAdmin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,50 @@
<Company>Huawei</Company>
<Description>AGConnect Admin SDK enables server-side .NET developers to integrate HCM into their
services and applications</Description>
<Copyright>Copyright (c) Huawei Technologies Co., Ltd. 2019-2019. All rights reserved.</Copyright>
<Copyright>Copyright (c) Huawei Technologies Co., Ltd. 2019-2024. All rights reserved.</Copyright>
<Authors>Huawei</Authors>
<Configurations>Debug;Release;Proxy</Configurations>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageProjectUrl>https://github.com/HMS-Core/hms-push-serverdemo-csharp</PackageProjectUrl>
<RepositoryUrl>https://github.com/HMS-Core/hms-push-serverdemo-csharp</RepositoryUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net45|AnyCPU'">
<WarningLevel>7</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net45|AnyCPU'">
<WarningLevel>7</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Proxy|net45|AnyCPU'">
<WarningLevel>7</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard2.0|AnyCPU'">
<WarningLevel>7</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|netstandard2.0|AnyCPU'">
<WarningLevel>7</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Proxy|netstandard2.0|AnyCPU'">
<WarningLevel>7</WarningLevel>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PrivateAssets>all</PrivateAssets>
Expand Down
40 changes: 40 additions & 0 deletions src/AGConnectAdmin/ApiVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace AGConnectAdmin
{
/// <summary>
/// Huawei PushKit API version level
/// </summary>
public enum ApiVersion
{
/// <summary>
/// API Base Uri V1 (App-level)
/// </summary>
V1,
/// <summary>
/// API Base Uri V2 (Project-level)
/// </summary>
V2
}

/// <summary>
/// ApiVersion Enum extension
/// </summary>
public static class ApiVersionExt
{
private const string _apiBaseUriPrefix = "https://push-api.cloud.huawei.com/v";
private const string _appLevelApiBaseUri = _apiBaseUriPrefix + "1";
private const string _projLevelApiBaseUri = _apiBaseUriPrefix + "2";

/// <summary>
/// Api version base Uri
/// </summary>
public static string ApiBaseUri(this ApiVersion apiVersion)
{
switch (apiVersion)
{
case ApiVersion.V1: return _appLevelApiBaseUri;
case ApiVersion.V2: return _projLevelApiBaseUri;
}
return string.Empty;
}
}
}
32 changes: 27 additions & 5 deletions src/AGConnectAdmin/AppOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,32 @@ public sealed class AppOptions
public AppOptions()
{
LoginUri = "https://oauth-login.cloud.huawei.com/oauth2/v3/token";
ApiBaseUri = "https://push-api.cloud.huawei.com/v1";
ApiVersion = ApiVersion.V2;
}

internal AppOptions(AppOptions options)
{
this.LoginUri = options.LoginUri;
this.ApiBaseUri = options.ApiBaseUri;
this.ApiVersion = options.ApiVersion;
this.ProjectId = options.ProjectId;
this.ClientId = options.ClientId;
this.ClientSecret = options.ClientSecret;
}

internal string GetApiUri()
{
return ApiBaseUri + string.Format("/{0}/messages:send", ClientId) ;
var res = ApiBaseUri;
switch (ApiVersion)
{
case ApiVersion.V1:
res += '/' + ClientId;
break;
case ApiVersion.V2:
res += '/' + ProjectId;
break;
}
res += "/messages:send";
return res;
}

/// <summary>
Expand All @@ -48,10 +60,20 @@ internal string GetApiUri()
public string LoginUri { get; set; }

/// <summary>
/// Gets or sets the API base path, it's optional.
/// Gets or sets the PushKit API version. Default value is project-level (V2)
/// This property is optional.
/// </summary>
public string ApiBaseUri { get; set; }
public ApiVersion ApiVersion { get; set; }

/// <summary>
/// Gets the API base uri according version
/// </summary>
public string ApiBaseUri { get => ApiVersion.ApiBaseUri(); }

/// <summary>
/// Gets or sets the PROJECT ID from AGC
/// </summary>
public string ProjectId { get; set; }

/// <summary>
/// Gets or sets the APP ID from AGC.
Expand Down