Skip to content
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ var complexFiler = new CompoundFilter(
- [x] Users
- [x] Retrieve a User
- [x] List all users
- [x] Retrieve your token's bot user
- [x] Search

## Contribution Guideline
Expand Down
10 changes: 9 additions & 1 deletion Src/Notion.Client/Api/ApiEndpoints.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Notion.Client
using System;

namespace Notion.Client
{
public static class ApiEndpoints
{
Expand All @@ -15,6 +17,12 @@ public static class UsersApiUrls
{
public static string Retrieve(string userId) => $"/v1/users/{userId}";
public static string List() => "/v1/users";

/// <summary>
/// Get the <see cref="uri string"/> for retrieve your token's bot user.
/// </summary>
/// <returns>Returns a <see cref="uri string"/> retrieve your token's bot user.</returns>
public static string Me() => "/v1/users/me";
}

public static class BlocksApiUrls
Expand Down
6 changes: 6 additions & 0 deletions Src/Notion.Client/Api/Users/IUsersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@ public interface IUsersClient
{
Task<User> RetrieveAsync(string userId);
Task<PaginatedList<User>> ListAsync();

/// <summary>
/// Retrieves the bot User associated with the API token provided in the authorization header.
/// </summary>
/// <returns>User object of type bot having an owner field with information about the person who authorized the integration.</returns>
Task<User> MeAsync();
}
}
9 changes: 9 additions & 0 deletions Src/Notion.Client/Api/Users/UsersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,14 @@ public async Task<PaginatedList<User>> ListAsync()
{
return await _client.GetAsync<PaginatedList<User>>(UsersApiUrls.List());
}

/// <summary>
/// Retrieves the bot User associated with the API token provided in the authorization header.
/// </summary>
/// <returns>User object of type bot having an owner field with information about the person who authorized the integration.</returns>
public async Task<User> MeAsync()
{
return await _client.GetAsync<User>(UsersApiUrls.Me());
}
}
}
10 changes: 10 additions & 0 deletions Src/Notion.Client/Models/User/Bot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace Notion.Client
{
public class Bot
{
[JsonProperty("owner")]
public IBotOwner Owner { get; set; }
}
}
14 changes: 14 additions & 0 deletions Src/Notion.Client/Models/User/BotOwner/IBotOwner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using JsonSubTypes;
using Newtonsoft.Json;

namespace Notion.Client
{
[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(UserOwner), "user")]
[JsonSubtypes.KnownSubType(typeof(WorkspaceIntegrationOwner), "workspace")]
public interface IBotOwner
{
[JsonProperty("type")]
string Type { get; set; }
}
}
12 changes: 12 additions & 0 deletions Src/Notion.Client/Models/User/BotOwner/UserOwner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json;

namespace Notion.Client
{
public class UserOwner : IBotOwner
{
public string Type { get; set; }

[JsonProperty("user")]
public User User { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json;

namespace Notion.Client
{
public class WorkspaceIntegrationOwner : IBotOwner
{
public string Type { get; set; }

[JsonProperty("workspace")]
public bool Workspace { get; set; }
}
}
10 changes: 10 additions & 0 deletions Src/Notion.Client/Models/User/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace Notion.Client
{
public class Person
{
[JsonProperty("email")]
public string Email { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,4 @@ public class User : IObject
[JsonProperty("bot")]
public Bot Bot { get; set; }
}

public class Person
{
[JsonProperty("email")]
public string Email { get; set; }
}

public class Bot
{

}
}
6 changes: 6 additions & 0 deletions Test/Notion.UnitTests/Notion.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@
<None Update="data\users\RetrieveUserResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\users\MeResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\users\MeUserLevelResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\search\SearchResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
67 changes: 67 additions & 0 deletions Test/Notion.UnitTests/UserClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,72 @@ public async Task RetrieveUser()
user.Person.Email.Should().Be("vedkoditkar@gmail.com");
user.Bot.Should().BeNull();
}

[Fact]
public async Task RetrieveTokenUser_WorkspaceInternalToken()
{
// Arrange
var jsonData = await File.ReadAllTextAsync("data/users/MeResponse.json");

var path = ApiEndpoints.UsersApiUrls.Me();

Server.Given(CreateGetRequestBuilder(path))
.RespondWith(
Response.Create()
.WithStatusCode(200)
.WithBody(jsonData)
);

// Act
var user = await _client.MeAsync();

// Assert
user.Id.Should().Be("590693f3-797f-4970-98ff-7284106393e5");
user.Name.Should().Be("Test");
user.AvatarUrl.Should().BeNull();
user.Type.Should().Be("bot");
user.Person.Should().BeNull();
user.Bot.Should().NotBeNull();
user.Bot.Owner.Should().BeOfType<WorkspaceIntegrationOwner>();

var owner = (WorkspaceIntegrationOwner)user.Bot.Owner;
owner.Workspace.Should().BeTrue();
}

[Fact]
public async Task RetrieveTokenUser_UserLevelToken()
{
// Arrange
var jsonData = await File.ReadAllTextAsync("data/users/MeUserLevelResponse.json");

var path = ApiEndpoints.UsersApiUrls.Me();

Server.Given(CreateGetRequestBuilder(path))
.RespondWith(
Response.Create()
.WithStatusCode(200)
.WithBody(jsonData)
);

// Act
var user = await _client.MeAsync();

// Assert
user.Id.Should().Be("16d84278-ab0e-484c-9bdd-b35da3bd8905");
user.Name.Should().Be("pied piper");
user.AvatarUrl.Should().BeNull();
user.Type.Should().Be("bot");
user.Person.Should().BeNull();
user.Bot.Should().NotBeNull();
user.Bot.Owner.Should().BeOfType<UserOwner>();

var owner = (UserOwner)user.Bot.Owner;
owner.User.Id.Should().Be("5389a034-eb5c-47b5-8a9e-f79c99ef166c");
owner.User.Name.Should().Be("christine makenotion");
owner.User.AvatarUrl.Should().BeNull();
owner.User.Type.Should().Be("person");
owner.User.Person.Email.Should().Be("christine@makenotion.com");
owner.User.Bot.Should().BeNull();
}
}
}
13 changes: 13 additions & 0 deletions Test/Notion.UnitTests/data/users/MeResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"object": "user",
"id": "590693f3-797f-4970-98ff-7284106393e5",
"name": "Test",
"avatar_url": null,
"type": "bot",
"bot": {
"owner": {
"type": "workspace",
"workspace": true
}
}
}
22 changes: 22 additions & 0 deletions Test/Notion.UnitTests/data/users/MeUserLevelResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"object": "user",
"id": "16d84278-ab0e-484c-9bdd-b35da3bd8905",
"name": "pied piper",
"avatar_url": null,
"type": "bot",
"bot": {
"owner": {
"type": "user",
"user": {
"object": "user",
"id": "5389a034-eb5c-47b5-8a9e-f79c99ef166c",
"name": "christine makenotion",
"avatar_url": null,
"type": "person",
"person": {
"email": "christine@makenotion.com"
}
}
}
}
}
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ var complexFiler = new CompoundFilter(
- [x] Users
- [x] Retrieve a User
- [x] List all users
- [x] Retrieve your token's bot user
- [x] Search

## Contribution Guideline
Expand Down