File tree Expand file tree Collapse file tree 4 files changed +140
-12
lines changed Expand file tree Collapse file tree 4 files changed +140
-12
lines changed Original file line number Diff line number Diff line change 9
9
</PropertyGroup >
10
10
11
11
<ItemGroup >
12
- <PackageReference Include =" Flurl" Version =" 3.0.7" />
13
- <PackageReference Include =" Flurl.Http" Version =" 3.2.4" />
14
- <PackageReference Include =" Microsoft.Extensions.Configuration" Version =" 7.0.0" />
15
- <PackageReference Include =" Microsoft.Extensions.Configuration.Json" Version =" 7.0.0" />
16
- <PackageReference Include =" Microsoft.NET.Test.Sdk" Version =" 17.4.1" />
17
- <PackageReference Include =" NUnit" Version =" 3.13.3" />
18
- <PackageReference Include =" NUnit3TestAdapter" Version =" 4.3.1" />
12
+ <PackageReference Include =" Flurl" Version =" 3.0.7" />
13
+ <PackageReference Include =" Flurl.Http" Version =" 3.2.4" />
14
+ <PackageReference Include =" Microsoft.Extensions.Configuration" Version =" 7.0.0" />
15
+ <PackageReference Include =" Microsoft.Extensions.Configuration.Json" Version =" 7.0.0" />
16
+ <PackageReference Include =" Microsoft.NET.Test.Sdk" Version =" 17.4.1" />
17
+ <PackageReference Include =" NUnit" Version =" 3.13.3" />
18
+ <PackageReference Include =" NUnit3TestAdapter" Version =" 4.3.1" />
19
19
<PackageReference Include =" NUnit.Analyzers" Version =" 3.5.0" >
20
20
<PrivateAssets >all</PrivateAssets >
21
21
<IncludeAssets >runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets >
27
27
</ItemGroup >
28
28
29
29
<ItemGroup >
30
- <ProjectReference Include =" ..\Modrinth.Net\Modrinth.Net.csproj" />
30
+ <ProjectReference Include =" ..\Modrinth.Net\Modrinth.Net.csproj" />
31
31
</ItemGroup >
32
32
33
33
<ItemGroup >
34
- <None Include =" appsettings.json" CopyToOutputDirectory =" Always" />
34
+ <None Include =" appsettings.json" CopyToOutputDirectory =" Always" />
35
35
</ItemGroup >
36
36
37
37
</Project >
Original file line number Diff line number Diff line change 1
1
using System . Reflection ;
2
2
using Microsoft . Extensions . Configuration ;
3
+ using Modrinth . Client ;
3
4
4
5
namespace Modrinth . Net . Test . ModrinthApiTests ;
5
6
@@ -26,8 +27,14 @@ private static string GetToken()
26
27
public void SetUp ( )
27
28
{
28
29
var token = GetToken ( ) ;
29
- var userAgent = $ "Zechiax/Modrinth.Net.Test/{ Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version } ";
30
- _client = new ModrinthClient ( url : ModrinthClient . StagingBaseUrl , userAgent : userAgent , token : token ) ;
31
- _noAuthClient = new ModrinthClient ( url : ModrinthClient . StagingBaseUrl , userAgent : userAgent ) ;
30
+ var userAgent = new UserAgent
31
+ {
32
+ GitHubUsername = "Zechiax" ,
33
+ ProjectName = "Modrinth.Net.Test" ,
34
+ ProjectVersion = Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version ? . ToString ( ) ,
35
+ } ;
36
+
37
+ _client = new ModrinthClient ( url : ModrinthClient . StagingBaseUrl , userAgent : userAgent . ToString ( ) , token : token ) ;
38
+ _noAuthClient = new ModrinthClient ( url : ModrinthClient . StagingBaseUrl , userAgent : userAgent . ToString ( ) ) ;
32
39
}
33
40
}
Original file line number Diff line number Diff line change
1
+ using Modrinth . Client ;
2
+
3
+ namespace Modrinth . Net . Test ;
4
+
5
+ [ TestFixture ]
6
+ public class UserAgentTests
7
+ {
8
+ [ Test ]
9
+ public void CreateFullUserAgent ( )
10
+ {
11
+ var userAgent = new UserAgent
12
+ {
13
+ ProjectName = "ProjectName" ,
14
+ ProjectVersion = "1.0.0" ,
15
+ GitHubUsername = "Username" ,
16
+ Contact = "contact@contact.com"
17
+ } ;
18
+
19
+ Assert . That ( userAgent . ToString ( ) , Is . EqualTo ( "Username/ProjectName/1.0.0 (contact@contact.com)" ) ) ;
20
+ }
21
+
22
+ [ Test ]
23
+ public void CreateMinimalUserAgent ( )
24
+ {
25
+ var userAgent = new UserAgent
26
+ {
27
+ ProjectName = "ProjectName" ,
28
+ ProjectVersion = "1.0.0" ,
29
+ } ;
30
+
31
+ Assert . That ( userAgent . ToString ( ) , Is . EqualTo ( "ProjectName/1.0.0" ) ) ;
32
+ }
33
+
34
+ [ Test ]
35
+ public void CreateUserAgentWithNoVersion ( )
36
+ {
37
+ var userAgent = new UserAgent
38
+ {
39
+ ProjectName = "ProjectName" ,
40
+ } ;
41
+
42
+ Assert . That ( userAgent . ToString ( ) , Is . EqualTo ( "ProjectName" ) ) ;
43
+ }
44
+ }
Original file line number Diff line number Diff line change
1
+ using System . Text ;
2
+
3
+ namespace Modrinth . Client ;
4
+
5
+ /// <summary>
6
+ /// Helper class for creating a user agent string for the Modrinth API client
7
+ /// </summary>
8
+ public class UserAgent
9
+ {
10
+ /// <summary>
11
+ /// Name of the project
12
+ /// </summary>
13
+ public string ? ProjectName { get ; set ; }
14
+ /// <summary>
15
+ /// Version of the project
16
+ /// </summary>
17
+ public string ? ProjectVersion { get ; set ; }
18
+
19
+ /// <summary>
20
+ /// Email or website of the project
21
+ /// </summary>
22
+ public string ? Contact { get ; set ; }
23
+ /// <summary>
24
+ /// GitHub username of the project
25
+ /// </summary>
26
+ public string ? GitHubUsername { get ; set ; }
27
+
28
+ /// <summary>
29
+ /// Creates a user agent string based on provided information
30
+ /// </summary>
31
+ /// <returns></returns>
32
+ public override string ToString ( )
33
+ {
34
+ // Needs checks for null
35
+ var sb = new StringBuilder ( ) ;
36
+
37
+ var before = false ;
38
+ if ( GitHubUsername != null )
39
+ {
40
+ sb . Append ( GitHubUsername ) ;
41
+ before = true ;
42
+ }
43
+
44
+ if ( ProjectName != null )
45
+ {
46
+ if ( before )
47
+ {
48
+ sb . Append ( '/' ) ;
49
+ }
50
+ sb . Append ( ProjectName ) ;
51
+ before = true ;
52
+ }
53
+
54
+ if ( ProjectVersion != null )
55
+ {
56
+ if ( before )
57
+ {
58
+ sb . Append ( '/' ) ;
59
+ }
60
+ sb . Append ( ProjectVersion ) ;
61
+ before = true ;
62
+ }
63
+
64
+ if ( Contact != null )
65
+ {
66
+ if ( before )
67
+ {
68
+ sb . Append ( ' ' ) ;
69
+ }
70
+ sb . Append ( '(' ) ;
71
+ sb . Append ( Contact ) ;
72
+ sb . Append ( ')' ) ;
73
+ }
74
+
75
+ return sb . ToString ( ) ;
76
+ }
77
+ }
You can’t perform that action at this time.
0 commit comments