Skip to content

Commit

Permalink
Add GetTimeline method
Browse files Browse the repository at this point in the history
  • Loading branch information
Xwilarg committed Jul 6, 2023
1 parent 4e58282 commit aa3cea7
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 8 deletions.
20 changes: 17 additions & 3 deletions BlueskySharp.Tests/UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ namespace BlueskySharp.Tests
[TestClass]
public class UnitTest
{
[TestMethod]
public async Task TestGetPosts()
private async Task<BlueskyClient> GetLoggedClientAsync()
{
string identifier = Environment.GetEnvironmentVariable("BLUESKY_IDENTIFIER")!;
string password = Environment.GetEnvironmentVariable("BLUESKY_PASSWORD")!;

var client = new BlueskyClient();
await client.AuthentifiateAsync(identifier, password);

var p = await client.GetProfile("theindra.nl");
return client;
}

[TestMethod]
public async Task TestGetPosts()
{
var p = await (await GetLoggedClientAsync()).GetProfileAsync("theindra.nl");

Assert.IsNotNull(p.Did);
Assert.AreEqual("theindra.nl", p.Handle);
Assert.AreEqual("Indra", p.DisplayName);
Assert.IsNotNull(p.Description);
Expand All @@ -29,5 +35,13 @@ public async Task TestGetPosts()
Assert.IsFalse(p.Viewer.Muted);
Assert.IsFalse(p.Viewer.BlockedBy);
}

[TestMethod]
public async Task TestGetTimeline()
{
var p = await (await GetLoggedClientAsync()).GetTimelineAsync();

// TODO
}
}
}
13 changes: 12 additions & 1 deletion BlueskySharp/BlueskyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task AuthentifiateAsync(string identifier, string password)
HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", res.AccessJwt);
}

public async Task<Profile> GetProfile(string actor)
public async Task<Profile> GetProfileAsync(string actor)
{
var resp = await HttpClient.GetAsync($"{_url}/xrpc/app.bsky.actor.getProfile?actor={HttpUtility.UrlEncode(actor)}");
if (!resp.IsSuccessStatusCode)
Expand All @@ -45,6 +45,17 @@ public async Task<Profile> GetProfile(string actor)
return JsonSerializer.Deserialize<Profile>(await resp.Content.ReadAsStringAsync(), _options);
}

public async Task<Timeline> GetTimelineAsync()
{
var resp = await HttpClient.GetAsync($"{_url}/xrpc/app.bsky.feed.getTimeline");
if (!resp.IsSuccessStatusCode)
{
var msg = JsonSerializer.Deserialize<ErrorResponse>(await resp.Content.ReadAsStringAsync(), _options).Message;
throw new Exception(msg);
}
return JsonSerializer.Deserialize<Timeline>(await resp.Content.ReadAsStringAsync(), _options);
}

~BlueskyClient()
{
HttpClient.Dispose();
Expand Down
64 changes: 64 additions & 0 deletions BlueskySharp/Response/Feed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Text.Json.Serialization;

namespace BlueskySharp.Response
{
public class ImageMetadata
{
[JsonPropertyName("$type")]
public string Type { set; get; }
public object Ref { set; get; } // TODO: parse properly
public string MimeType { set; get; }
public int Size { set; get; }
}

public class ImageInfo
{
public ImageMetadata[] Image;
public string Alt;
}

public class Image
{
public string Thumb { set; get; }
public string Fullsize { set; get; }
public string Alt { set; get; }
}

public class Embed
{
[JsonPropertyName("$type")]
public string Type { set; get; } // TODO: Replace by enum
public Image[] Images { set; get; }
}

public record Record
{
public string Text { set; get; }
[JsonPropertyName("$type")]
public string Type { set; get; }
public Embed Embed { set; get; }
public string[] Langs { set; get; }
public DateTime CreatedAt { set; get; }
}

public record Post
{
public string Uri { set; get; }
public string Cid { set; get; }
public ProfileShort Author { set; get; }
public Record Record { set; get; }
public Embed Embed { set; get; }
public int ReplyCount { set; get; }
public int RepostCount { set; get; }
public int LikeCount { set; get; }
public DateTime IndexedAt { set; get; }
public Viewer Viewer { set; get; }
public string[] Labels { set; get; }
}

public record Timeline
{
public string Cursor { set; get; }
public Post[] Posts { set; get; }
}
}
13 changes: 9 additions & 4 deletions BlueskySharp/Response/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ public record Viewer
public string FollowedBy { set; get; }

Check warning on line 8 in BlueskySharp/Response/Profile.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'FollowedBy' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}

public record Profile
public record ProfileShort
{
public string Did { set; get; }

Check warning on line 13 in BlueskySharp/Response/Profile.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Did' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Handle { set; get; }

Check warning on line 14 in BlueskySharp/Response/Profile.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Handle' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string DisplayName { set; get; }

Check warning on line 15 in BlueskySharp/Response/Profile.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'DisplayName' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Description { set; get; }
public string Avatar { set; get; }

Check warning on line 16 in BlueskySharp/Response/Profile.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Avatar' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string[] Labels { set; get; }

Check warning on line 17 in BlueskySharp/Response/Profile.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Labels' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public Viewer Viewer { set; get; }

Check warning on line 18 in BlueskySharp/Response/Profile.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Viewer' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}

public record Profile : ProfileShort
{
public string Description { set; get; }

Check warning on line 23 in BlueskySharp/Response/Profile.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Description' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Banner { set; get; }

Check warning on line 24 in BlueskySharp/Response/Profile.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Banner' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public int FollowsCount { set; get; }
public int FollowersCount { set; get; }
public int PostsCount { set; get; }
public DateTime IndexedAt { set; get; }
public string[] Labels { set; get; }
public Viewer Viewer { set; get; }
}
}

0 comments on commit aa3cea7

Please sign in to comment.