-
Notifications
You must be signed in to change notification settings - Fork 42
feat: add http client #28
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
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
addrinfo
struct on Macos. Previously, making thegetaddrinfo
call would return ip address.Haven't really tested beyond get requests, but is working making requests to httpbin.org and google.com currently.