-
Notifications
You must be signed in to change notification settings - Fork 12
Question: How do I do HTTP? #100
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,130 @@ | ||
| namespace ComputerWorld.wit.exports.example.calculator; | ||
| using System; | ||
| using ComputerWorld.wit.imports.wasi.http.v0_2_0; // May be generated if wasi:http present; harmless if unused | ||
| using static ComputerWorld.wit.imports.wasi.http.v0_2_0.ITypes; | ||
|
|
||
| namespace ComputerWorld.wit.exports.example.calculator; | ||
|
|
||
| // Implementation of operations interface generated from WIT | ||
| public class OperationsImpl : IOperations | ||
| { | ||
| public static int Add(int left, int right) | ||
| public static int Add(int left, int right) => left + right; | ||
|
|
||
| public static string ToUpper(string input) => input.ToUpperInvariant(); | ||
|
|
||
| // Renamed to match WIT change: get-repo(owner, name) | ||
| public static string GetRepo(string owner, string name) | ||
| { | ||
| return left + right; | ||
| // Performs a simple unauthenticated GET to the GitHub repository metadata endpoint. | ||
| // Parameters map directly to the GitHub URL path segments. | ||
| try | ||
| { | ||
| if (string.IsNullOrWhiteSpace(owner) || string.IsNullOrWhiteSpace(name)) | ||
| return "{\"error\":\"missing-owner-or-name\"}"; | ||
| var safeOwner = owner.Trim(); | ||
| var safeName = name.Trim(); | ||
| // Very basic validation to avoid path injection | ||
| if (safeOwner.Contains('/') || safeName.Contains('/')) | ||
| return "{\"error\":\"invalid-slash\"}"; | ||
| var url = $"https://api.github.com/repos/{safeOwner}/{safeName}"; | ||
|
|
||
| // Empty headers collection (GitHub typically requires a User-Agent, but per request no headers are passed). | ||
| // Fields headers; | ||
| // try | ||
| // { | ||
| // headers = Fields.FromList(new System.Collections.Generic.List<(string, byte[])>()); | ||
| // } | ||
| // catch (WitException<HeaderError> hex) | ||
| // { | ||
| // return $"{{\"error\":\"header-build\",\"detail\":\"{hex.TypedValue.Tag}\"}}"; | ||
| // } | ||
|
|
||
| var headers = new Fields(); | ||
| try { | ||
| // GitHub API recommends providing a User-Agent; also add Accept for clarity. | ||
| headers.Set("User-Agent", new System.Collections.Generic.List<byte[]> { System.Text.Encoding.UTF8.GetBytes("componentize-dotnet-sample/1.0") }); | ||
| headers.Set("Accept", new System.Collections.Generic.List<byte[]> { System.Text.Encoding.UTF8.GetBytes("application/vnd.github+json") }); | ||
| } catch (WitException<HeaderError>) { /* non-fatal if immutable/forbidden */ } | ||
| var req = new OutgoingRequest(headers); | ||
| req.SetMethod(Method.Get()); | ||
|
|
||
| var uri = new Uri(url); | ||
| req.SetScheme(uri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase) ? Scheme.Https() : Scheme.Http()); | ||
| req.SetAuthority(uri.Authority); | ||
| req.SetPathWithQuery(uri.PathAndQuery); | ||
|
|
||
| RequestOptions? options = null; | ||
| var future = OutgoingHandlerInterop.Handle(req, options); | ||
|
|
||
| IncomingResponse? incoming = null; | ||
| const int timeoutMs = 5000; // total budget | ||
| var sw = System.Diagnostics.Stopwatch.StartNew(); | ||
| int delay = 10; // start with 10ms, exponential backoff up to 250ms | ||
| while (sw.ElapsedMilliseconds < timeoutMs) | ||
| { | ||
| var resultOpt = future.Get(); | ||
| if (resultOpt != null) | ||
| { | ||
| var outer = resultOpt.Value; | ||
| if (outer.IsOk) | ||
| { | ||
| var inner = outer.AsOk; | ||
| if (inner.IsOk) | ||
| { | ||
| incoming = inner.AsOk; | ||
| } | ||
| else | ||
| { | ||
| var err = inner.AsErr; | ||
| return $"{{\"error\":\"http-transport\",\"code\":\"{err.Tag}\"}}"; | ||
| } | ||
| } | ||
| break; | ||
| } | ||
| System.Threading.Thread.Sleep(delay); | ||
| delay = Math.Min(delay * 2, 250); | ||
| } | ||
|
|
||
| if (incoming == null) | ||
| { | ||
| return $"{{\"error\":\"timeout\",\"elapsedMs\":{sw.ElapsedMilliseconds}}}"; | ||
| } | ||
|
|
||
| int status = incoming.Status(); | ||
| string body = string.Empty; | ||
| try | ||
| { | ||
| var bodyRes = incoming.Consume(); | ||
| var stream = bodyRes.Stream(); | ||
| var bytes = new System.Collections.Generic.List<byte>(); | ||
| while (true) | ||
| { | ||
| var chunk = stream.Read(1024); | ||
| if (chunk.Length == 0) | ||
| break; | ||
| bytes.AddRange(chunk); | ||
| } | ||
| body = System.Text.Encoding.UTF8.GetString(bytes.ToArray()); | ||
| stream.Dispose(); | ||
| bodyRes.Dispose(); | ||
| } | ||
| finally | ||
| { | ||
| incoming.Dispose(); | ||
| future.Dispose(); | ||
| } | ||
|
|
||
| if (status >= 200 && status < 300) return body; | ||
| return $"{{\"error\":\"http-status\",\"status\":{status},\"body\":{EscapeForJson(body)} }}"; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| return $"{{\"error\":\"exception\",\"message\":\"{Escape(ex.Message)}\"}}"; | ||
| } | ||
| } | ||
|
|
||
| public static string ToUpper(string input) | ||
| private static string Escape(string s) => s.Replace("\\", "\\\\").Replace("\"", "\\\""); | ||
| private static string EscapeForJson(string s) | ||
| { | ||
| return input.ToUpperInvariant(); | ||
| return "\"" + Escape(s) + "\""; | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| // Generated by `wit-bindgen` 0.41.0. DO NOT EDIT! | ||
| // <auto-generated /> | ||
| #nullable enable | ||
| using System.Runtime.InteropServices; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Runtime.CompilerServices; | ||
| namespace ComputerWorld { | ||
|
|
||
| public interface IComputerWorld { | ||
| } | ||
|
|
||
| public readonly struct None {} | ||
|
|
||
| [StructLayout(LayoutKind.Sequential)] | ||
| public readonly struct Result<TOk, TErr> | ||
| { | ||
| public readonly byte Tag; | ||
| private readonly object value; | ||
|
|
||
| private Result(byte tag, object value) | ||
| { | ||
| Tag = tag; | ||
| this.value = value; | ||
| } | ||
|
|
||
| public static Result<TOk, TErr> Ok(TOk ok) | ||
| { | ||
| return new Result<TOk, TErr>(Tags.Ok, ok!); | ||
| } | ||
|
|
||
| public static Result<TOk, TErr> Err(TErr err) | ||
| { | ||
| return new Result<TOk, TErr>(Tags.Err, err!); | ||
| } | ||
|
|
||
| public bool IsOk => Tag == Tags.Ok; | ||
| public bool IsErr => Tag == Tags.Err; | ||
|
|
||
| public TOk AsOk | ||
| { | ||
| get | ||
| { | ||
| if (Tag == Tags.Ok) | ||
| { | ||
| return (TOk)value; | ||
| } | ||
|
|
||
| throw new ArgumentException("expected k, got " + Tag); | ||
| } | ||
| } | ||
|
|
||
| public TErr AsErr | ||
| { | ||
| get | ||
| { | ||
| if (Tag == Tags.Err) | ||
| { | ||
| return (TErr)value; | ||
| } | ||
|
|
||
| throw new ArgumentException("expected Err, got " + Tag); | ||
| } | ||
| } | ||
|
|
||
| public class Tags | ||
| { | ||
| public const byte Ok = 0; | ||
| public const byte Err = 1; | ||
| } | ||
| } | ||
|
|
||
| public class Option<T> { | ||
| private static Option<T> none = new (); | ||
|
|
||
| private Option() | ||
| { | ||
| HasValue = false; | ||
| } | ||
|
|
||
| public Option(T v) | ||
| { | ||
| HasValue = true; | ||
| Value = v; | ||
| } | ||
|
|
||
| public static Option<T> None => none; | ||
|
|
||
| [MemberNotNullWhen(true, nameof(Value))] | ||
| public bool HasValue { get; } | ||
|
|
||
| public T? Value { get; } | ||
| } | ||
|
|
||
| public class WitException: Exception { | ||
| public object Value { get; } | ||
| public uint NestingLevel { get; } | ||
|
|
||
| public WitException(object v, uint level) | ||
| { | ||
| Value = v; | ||
| NestingLevel = level; | ||
| } | ||
| } | ||
|
|
||
| public class WitException<T>: WitException { | ||
| public T TypedValue { get { return (T)this.Value;} } | ||
|
|
||
| public WitException(T v, uint level) : base(v!, level) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| public static class InteropReturnArea | ||
| { | ||
| [InlineArray(2)] | ||
| [StructLayout(LayoutKind.Sequential, Pack = 4)] | ||
| internal struct ReturnArea | ||
| { | ||
| private uint buffer; | ||
|
|
||
| internal unsafe nint AddressOfReturnArea() | ||
| { | ||
| return (nint)Unsafe.AsPointer(ref buffer); | ||
| } | ||
| } | ||
|
|
||
| [ThreadStatic] | ||
| [FixedAddressValueType] | ||
| internal static ReturnArea returnArea = default; | ||
| } | ||
|
|
||
| namespace exports { | ||
|
|
||
| public static class ComputerWorld | ||
| { | ||
| } | ||
| } | ||
|
|
||
| } |
14 changes: 14 additions & 0 deletions
14
...alculator/Adder/generated/wit/ComputerWorld.wit.exports.example.calculator.IOperations.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Generated by `wit-bindgen` 0.41.0. DO NOT EDIT! | ||
| // <auto-generated /> | ||
| #nullable enable | ||
|
|
||
| namespace ComputerWorld.wit.exports.example.calculator; | ||
|
|
||
| public interface IOperations { | ||
| static abstract int Add(int left, int right); | ||
|
|
||
| static abstract string ToUpper(string input); | ||
|
|
||
| static abstract string GetRepo(string owner, string name); | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may need to poll the response (
future.Subscribe+Poll) instead of simply waiting.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jsturtevant posted this sample in #98 that uses
PollWasiEventLoopUntilResolved. It also appears that HttpClient API can be used. That would be amazing. Going to try out https://github.com/jsturtevant/csharp-components-demos/blob/main/console/Program.csThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, If you are doing outbound http HttpClient should work!