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

Wrapper Updates #4

Merged
merged 8 commits into from
Nov 19, 2019
Merged

Wrapper Updates #4

merged 8 commits into from
Nov 19, 2019

Conversation

exsersewo
Copy link
Contributor

@exsersewo exsersewo commented Nov 10, 2019

Summary:
Add More Client Tests,
Added Interface,
Add Comments,
Redid PostImage Response

EDIT:
Also contains experimental multipart uploading on files >= 1mb

Add Experimental Flag,
Remove Miki.Http usings for now
Add Multipart Form Data
Update README.md to reflect changes to project
@@ -10,9 +10,29 @@ public class ClientTests
[Fact]
public void ConstructClientTest()
{
var i = new ImghoardClient(ImghoardClient.Config.Default());
var i = new ImghoardClient(Config.Default());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make it so that if you pass null it will fallback to null? this way we could make it optional to pass a Config.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, if the constructor is empty, it uses the default configuration, this was just a straggler that was oversighted from testing.

{
public string Tenancy { get; set; } = "production";
public string Endpoint { get; set; } = "https://api.miki.ai/";
public string UserAgent { get; set; } = $"Mozilla/5.0 (Imghoard.Net/{Assembly.GetExecutingAssembly().GetName().Version.ToString().Substring(0, 3)}; +https://github.com/Mikibot/dotnet-imghoard)";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Mozilla/5.0? This is not a web browser.

/// <returns>The url of the uploaded image or null on failure</returns>
public async Task<string> PostImageAsync(Memory<byte> bytes, params string[] Tags)
{
if(bytes.Length >= 1000000 && !config.Experimental)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preferably do not use 'magic numbers'. Recommend 10 * Mb where Mb is a constant.

}

var b64 = Convert.ToBase64String(bytes);
image.Position = 0;
(bool supported, string prefix) = IsSupported(bytes.Span.ToArray());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This copies the array twice. I would recommend for the future to move IsSupported to a span/memory as well.

Data = $"data:image/{prefix};base64,{Convert.ToBase64String(bytes.Span)}",
Tags = Tags
},
new JsonSerializerSettings
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider making this a constant

var response = await apiClient.PostAsync("/images", body);

if (response.Success)
return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to return null? or throw an exception?

{
public class ImagesResponse
{
private readonly ImghoardClient ClientInstance;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private variables should be lowerCamelCase

Copy link
Contributor Author

@exsersewo exsersewo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe all of these have been resolved

Copy link
Member

@velddev velddev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good progress!

@@ -10,9 +10,29 @@ public class ClientTests
[Fact]
public void ConstructClientTest()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn't make much sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would recommend to verify if there's something like the client set up, or the correct URL is being propagated

src/Imghoard.Tests/ClientTests.cs Outdated Show resolved Hide resolved
src/Imghoard/Config.cs Outdated Show resolved Hide resolved

apiClient = new HttpClient();
apiClient.DefaultRequestHeaders.Add("x-miki-tenancy", config.Tenancy);
apiClient.DefaultRequestHeaders.Add("User-Agent", config.UserAgent);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this capp'd, but the other one isn't?


if (page > 0)
{
query.Append($"page={page}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would recommend to rewrite this as

List<string> args = new List<string>();

if(page > 0) 
{
    args.Add($"page={page}");
}
if(tags.Any()) 
{
    args.Add($"tags={string.Join("+", tags)}")
}

string endpoint = baseEndpoint;
if(args.Any())
{
    endpoint += "?" + string.Join("&", args);
}

src/Imghoard/ImghoardClient.cs Outdated Show resolved Hide resolved
src/Imghoard/ImghoardClient.cs Outdated Show resolved Hide resolved
src/Imghoard/Exceptions/ResponseException.cs Outdated Show resolved Hide resolved
src/Imghoard/Config.cs Outdated Show resolved Hide resolved
src/Imghoard/Models/ImagesResponse.cs Outdated Show resolved Hide resolved
src/Imghoard/ImghoardClient.cs Outdated Show resolved Hide resolved
src/Imghoard/ImghoardClient.cs Outdated Show resolved Hide resolved
{
var mock = new Mock<IImghoardClient>();

mock.Setup(x => x.GetImageAsync(It.IsAny<ulong>()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically here you want to mock the http client and inject it into the imghoard client

@velddev
Copy link
Member

velddev commented Nov 18, 2019

Technically I'm not against pushing this, but I added these and want to know what you think @exsersewo

@exsersewo exsersewo merged commit 1746e13 into master Nov 19, 2019
@exsersewo exsersewo deleted the pagination branch November 19, 2019 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants