feat: add http client#28
Conversation
| fn get_ip_address(self, host: String) raises -> in_addr: | ||
| var host_ptr = to_char_ptr(host) | ||
| var servinfo = Pointer[Self]().alloc(1) | ||
| servinfo.store(Self()) | ||
|
|
||
| var hints = Self() | ||
| hints.ai_family = AF_INET | ||
| hints.ai_socktype = SOCK_STREAM | ||
| hints.ai_flags = AI_PASSIVE | ||
|
|
||
| var error = getaddrinfo[Self]( | ||
| host_ptr, | ||
| Pointer[UInt8](), | ||
| Pointer.address_of(hints), | ||
| Pointer.address_of(servinfo), | ||
| ) | ||
| if error != 0: | ||
| print("getaddrinfo failed") | ||
| raise Error("Failed to get IP address. getaddrinfo failed.") | ||
|
|
||
| var addrinfo = servinfo.load() | ||
|
|
||
| var ai_addr = addrinfo.ai_addr | ||
| if not ai_addr: | ||
| print("ai_addr is null") | ||
| raise Error( | ||
| "Failed to get IP address. getaddrinfo was called successfully, but" | ||
| " ai_addr is null." | ||
| ) | ||
|
|
||
| var addr_in = ai_addr.bitcast[sockaddr_in]().load() | ||
|
|
||
| return addr_in.sin_addr |
There was a problem hiding this comment.
This method is the same in both unix & macos structs. Figure when they add the ability to set default function in a trait this can be moved the trait as it's identical for both.
|
I'm glad some of my work on mojo-http-client was able to help with this! |
|
Woah, great teamwork! Will review today, I think we can combine this with the other code that was already merged and get an even better implementation 💪 |
|
@ZacHooper made a couple changes to bring this in line with latest state on |
|
Awesome looks good to me! 😄 Did notice one thing though. Not sure if I'm using the HTTPRequest/URI structs wrong but found I needed to add the port to the URL or it caused compiler error. Good var req = HTTPRequest(
URI("http://www.httpbin.org:80/status/300"),
req_str,
RequestHeader(req_str),
)Bad var req = HTTPRequest(
URI("http://www.httpbin.org/status/300"),
req_str,
RequestHeader(req_str),
) |
|
The unified code from the two implementation looks good, it is more robust and better looking than my original PR for sure. Congrats ! |
|
@ZacHooper just pushed a fix to properly handle URLs without a port and encode requests. Also added a simple test for external calls. Can you try again? |
|
Yep working great. Much easier to write the request now 😄. |
|
Perfect! Done merged. |
Mojo HTTP Client
addrinfostruct on Macos. Previously, making thegetaddrinfocall would return ip address.Haven't really tested beyond get requests, but is working making requests to httpbin.org and google.com currently.