Sugar HTTP

@mh I’m struggling with using Http in Sugar :frowning: I am unable to get response.Content. How can I see its value in the debugger? When I use it in the Android project, It always gets passed as null. Do you have anything running in swift that confirms if Http works in Sugar?

let completion1: HttpResponseBlock<String!>! = { response in {
		//NSLog(response.Content)
		//completion(response.Content)
		completion(response.Content)
		}
	}
	let url = Url("http://httpbin.org/get")
	Http.DownloadStringAsync(url, Encoding.UTF8, completion1)

Hm. for one, this code doesn’t compile. The extra set of curlies after “in” needs to go away, and completion isn’t defined. Can you post a full compiling snippet/project?

Okay so I’ve removed the extra curlies but now I always get response to the an “unknown value” in the debugger.
Btw I noticed that whatever I pass to the completion (even a static string) it always shows up as “Unknown Value” in the debugger. Could it be that the debugger is buggy?

Here’s the full code. Can you figure out what’s wrong?

import Sugar

public class SKNetwork {

public class func performNetworkRequestWithUri(uriString: Sugar.String, withCompletion completion: (response: Sugar.String?) -> ()) {
	
	//let url = Url(uriString: uriString)
	let completion1: HttpResponseBlock<String!>! = { response in {
		//NSLog(response.Content)
		//completion(response.Content)

		completion(response.Content)
		}
	}
	
	let url = Url("http://httpbin.org/get")
	//let url = Url("http://m090009.com")
	Http.DownloadStringAsync(url, Encoding.UTF8, completion1)
	
}
}

@mh any luck with this?

you want to add this to your manifest (under Properties in the IDE):


  <uses-permission android:name="android.permission.INTERNET" />

After that I get a proper response. I did notice 1 of the types being wrong, so I’ll post a bug fir that.

If you put a breakpoint on completion(response.Content) and expand response you can see:

exception: java.lang.SecurityException: Permission denied (missing INTERNET permission?)

Thanks, logged as bugs://72111

Awesome started a new test project and forgot to do this! It’s working perfectly on Android.

@ck @mh As for iOS, I’m getting this compiler error. Just to get it right, I copied the .a files from the bin > Release into the Xcode project (and added them to link binaries with libs) also added the -Objc linker flag. Since I’m using a swift project I also added the Bridging-Header.h and imported the header. After doing this I get this error.

Strangely, if I comment the import statement in the bridging header, I get a different error:

You want to add a ref to Sugar.a too for the linker errors. As for the unknowntype, can you show the exact types you ended up with for SKFunctionals, so I can fix the header generator?

@ck where can I get Sugar.a from ?

This is the SKFunctionals Class

public class SKFunctionals {
public func bind<A,B> (a: A?, f: A->B) -> B? {
    if let x = a {
		 return f(x)
	 }
	 else {
	     return nil
	 }
}
}

Thanks, logged as bugs://72116

libSugar.a is next to libSugar.fx, depending on if you use fire it’s inside the Fire.app, else it’s in Program Files (x86)\Elements\Nougat. Odds are though, there’s a libSugar.a next to your existing project output dir already.

@ck okay so I found the libSugar.a in the Fire.app, I selected the libSugar from the iOS folder but after doing so, it tells me that its skipping the file due to unmatched architecture this is the exact warning

ld: warning: ignoring file /Users/salmanhasratkhan/Desktop/test/test/test/libSugar.a, missing required architecture x86_64 in file /Users/salmanhasratkhan/Desktop/test/test/test/libSugar.a (2 slices)

Should I be using the OS X libSugar.a ?

There’s no libSugar.a next to my existing project maybe this could be a feature request to add it to the bin folder of the class library?

@ck Okay so I tried using the libSugar.a from the OS X folder and now its not skipping the file, but I have 45 errors.

That’s libxml2 (I think just a linker param:: -l xml2)

@mh @ck One question though, even though I created a static library for iOS in Fire, why does it compile as an OS X library? even when I added the libSugar.a, I had to add the OS X version as clang would just skip the iOS version of libSugar.a. Is this a bug or is it expected behaviour?

@ck after adding the linker params, I’ve also added the libNougat.a, and now the errors are reduced to 2 only. Some protocol is referenced that doesn’t work

@ck I've added the linker param, but now its giving me 5 errors, some protocol and boxed char are not found. Just to be sure, so far I've just added the compiled static library from Fire and the libSugar.a. Should I be adding something else?

i think both issues are related to a mismatch somewhere, like Fire compiling iPhoneSimulator (which matches OSX in platform), and you compiling OSX or reverse.

Can I send you the project and maybe you can look at it ?

Please, yes. it definitely sounds like its getting mixed up between OS X and iOS, somehow.

bugs://72116 got closed with status fixed.

bugs://72111 got closed with status nochangereq.