Skip to content

Commit

Permalink
feat: migrate Dagger.SDK to netstand2.0 (#27)
Browse files Browse the repository at this point in the history
Signed-off-by: Thanabodee Charoenpiriyakij <wingyminus@gmail.com>
  • Loading branch information
wingyplus authored Jun 22, 2024
1 parent 4a2670f commit e769d64
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public string RenderInputObject(Type type)

var toKeyValuePairsProperties = type.InputFields.Select(field =>
$"""
kvPairs.Add(KeyValuePair.Create("{field.Name}", {RenderArgumentValue(field, asProperty: true)} as Value));
kvPairs.Add(new KeyValuePair<string, Value>("{field.Name}", {RenderArgumentValue(field, asProperty: true)} as Value));
""");

var toKeyValuePairsMethod =
$$"""
public List<KeyValuePair<string,Value>> ToKeyValuePairs()
public List<KeyValuePair<string, Value>> ToKeyValuePairs()
{
var kvPairs = new List<KeyValuePair<string, Value>>();
{{string.Join("\n", toKeyValuePairsProperties)}}
Expand Down Expand Up @@ -248,7 +248,7 @@ private static string RenderReturnValue(Field field)
(await Engine.ExecuteList<{typeName}Id>(GraphQLClient, queryBuilder))
.Select(id =>
new {typeName}(
QueryBuilder.Builder().Select("load{typeName}FromID", [new Argument("id", new StringValue(id.Value))]),
QueryBuilder.Builder().Select("load{typeName}FromID", ImmutableList.Create<Argument>(new Argument("id", new StringValue(id.Value)))),
GraphQLClient
)
)
Expand Down
7 changes: 6 additions & 1 deletion sdk/Dagger.SDK/Dagger.SDK.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
Expand All @@ -19,5 +19,10 @@
<ItemGroup>
<AdditionalFiles Include="introspection.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Collections.Immutable" Version="6.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.3" />
</ItemGroup>

</Project>
5 changes: 2 additions & 3 deletions sdk/Dagger.SDK/Engine.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Immutable;
using System.Diagnostics;
using System.Net.Http.Json;
using System.Text.Json;

using Dagger.SDK.GraphQL;
Expand Down Expand Up @@ -46,7 +44,8 @@ private static async Task<JsonElement> Request(GraphQLClient client, QueryBuilde
var query = queryBuilder.Build();
var response = await client.RequestAsync(query);
// TODO: handle error here.
var jsonElement = await response.Content.ReadFromJsonAsync<JsonElement>()!;
var data = await response.Content.ReadAsStringAsync();
var jsonElement = JsonSerializer.Deserialize<JsonElement>(data);
return jsonElement.GetProperty("data");
}

Expand Down
15 changes: 9 additions & 6 deletions sdk/Dagger.SDK/GraphQL/GraphQLClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;

namespace Dagger.SDK.GraphQL;

Expand All @@ -12,19 +11,22 @@ public class GraphQLClient
{
private readonly HttpClient _httpClient;

public GraphQLClient() : this(Environment.GetEnvironmentVariable("DAGGER_SESSION_PORT")!, Environment.GetEnvironmentVariable("DAGGER_SESSION_TOKEN")!)
public GraphQLClient() : this(Environment.GetEnvironmentVariable("DAGGER_SESSION_PORT")!,
Environment.GetEnvironmentVariable("DAGGER_SESSION_TOKEN")!)
{
}

public GraphQLClient(string port, string token, string scheme = "http", string host = "localhost")
{
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", GetTokenHeaderValue(token));
_httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic", GetTokenHeaderValue(token));
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_httpClient.BaseAddress = new Uri($"{scheme}://{host}:{port}");
}

private static string GetTokenHeaderValue(string token) => Convert.ToBase64String(Encoding.UTF8.GetBytes($"{token}:"));
private static string GetTokenHeaderValue(string token) =>
Convert.ToBase64String(Encoding.UTF8.GetBytes($"{token}:"));

/// <summary>
/// Perform GraphQL request.
Expand All @@ -33,6 +35,7 @@ public GraphQLClient(string port, string token, string scheme = "http", string h
/// <returns>Raw HTTP response.</returns>
public async Task<HttpResponseMessage> RequestAsync(string query)
{
return await _httpClient.PostAsJsonAsync("/query", new { query });
var content = new StringContent(JsonSerializer.Serialize(new { query }), Encoding.UTF8, "application/json");
return await _httpClient.PostAsync("/query", content);
}
}
6 changes: 3 additions & 3 deletions sdk/Dagger.SDK/GraphQL/GraphQLResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ namespace Dagger.SDK.GraphQL;
public class GraphQLError
{
[JsonPropertyName("message")]
public required string Message { get; set; }
public string Message { get; set; }

[JsonPropertyName("path")]
public required List<string> Path { get; set; }
public List<string> Path { get; set; }
}

public class GraphQLResponse
Expand All @@ -18,5 +18,5 @@ public class GraphQLResponse
public List<GraphQLError>? Errors { get; set; }

[JsonPropertyName("data")]
public required JsonElement Data { get; set; }
public JsonElement Data { get; set; }
}
4 changes: 2 additions & 2 deletions sdk/Dagger.SDK/GraphQL/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class QueryBuilder(ImmutableList<Field> path)
{
public readonly ImmutableList<Field> Path = path;

private QueryBuilder() : this([]) { }
private QueryBuilder() : this(ImmutableList<Field>.Empty) { }

/// <summary>
/// Select a field with name.
Expand All @@ -20,7 +20,7 @@ private QueryBuilder() : this([]) { }
/// <returns>A new QueryBuilder instance.</returns>
public QueryBuilder Select(string name)
{
return Select(name, []);
return Select(name, ImmutableList<Argument>.Empty);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion sdk/Dagger.SDK/Scalar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Dagger.SDK;

public class Scalar
{
public string Value { get; init; }
public string Value { get; set; }

public override string ToString() => Value;
}

0 comments on commit e769d64

Please sign in to comment.