Help with translating long objC to ASObjC

I am learning both ASObjC and objC at the same time. Simpler stuff is straight forward, however I am running into problems trying to translate some longer objective-c over.

I’m attempting to play with Ebay’s API… I have it working from an example I found online written in objC
Now time to get it into applescriptobjc

[theRequest setAttributesAsDictionary:[NSDictionary dictionaryWithObject:@"urn:ebay:apis:eBLBaseComponents" forKey:@"xmlns"]];

I have tried all sorts of variations and keep throwing all sorts of “unrecognized selector sent to object”
Hope that once I see how this should be translate I can figure it out the rest!
Thanks

Assuming theRequest is a valid object of the sort you want:

theRequest’s setAttributesAsDictionary:{xmlns:“urn:ebay:apis:eBLBaseComponents”}

or pre-Mavericks:

theRequest’s setAttributesAsDictionary_({xmlns:“urn:ebay:apis:eBLBaseComponents”})

Thank You Mr. Shane!

Further continuing my little translation project I have run into yet another “bump”…

I have become stuck with the following code.

NSURLResponse *response = nil;
        NSError *error;
        NSData *data = nil;
        theData = [NSURLConnection sendSynchronousRequest:http returningResponse:&response error:&error];

I have translated into the following asobjc

theHttp is a valid xml.

set theData to current application's NSURLConnection's sendSynchronousRequest_returningResponse_error_(theHttp,reference, reference)

I am getting this error in response

[NSXMLDocument length]: unrecognized selector sent to instance 0x6100000d6ab0
An uncaught exception was raised
[NSXMLDocument length]: unrecognized selector sent to instance 0x6100000d6ab0

I am assuming it is to do with passing reference values as such?
I am currently running os x Mavericks.
Thanks!

Browser: Safari 537.74.9
Operating System: Mac OS X (10.8)

Hi,

the first parameter of the sendSynchronousRequest method must be an instance of NSURLRequest wrapping the URL

I am passing the first value as such.

tell current application’s class “NSURL” to set theurl to its URLWithString_(“https://api.sandbox.ebay.com/ws/api.dll”)
tell current application’s class “NSMutableURLRequest” to set theHttp to requestWithURL_(theurl)

Logging theHttp : <NSMutableURLRequest: 0x618000017f10> { URL: https://api.sandbox.ebay.com/ws/api.dll }

then the error occurs later, where NSXMLDocument is involved

The way to use out values is to use the keyword reference as you have done, but the method then returns the values in a list. So you need something like:

set {theData, theResponse, theError} to current application's NSURLConnection's sendSynchronousRequest_returningResponse_error_(theHttp,reference, reference)

Yet all your example code is using pre-Mavericks syntax. That works OK, but it suggests you’re not compiling your code in an external editor to check it. I’d suggest you do.

Thanks Shane, I’ll be sure to start using an external editor!

Over the past week or so I have been working on other sections of this larger project, jumping back to attempt to figure out where I have went wrong from time to time… no luck whatsoever so I figured I would post the whole stand alone ASOBJC that I have been attempting to get working. Commented out is each line that I attempt to translate over. Then the translation attempt below it.


property DevID : "DEV ID HERE"
property AppID : "APP ID HERE"
property CertID : "CERT ID HERE"

--NSXMLElement *theRequest = [[NSXMLElement alloc] initWithName:@"GetSessionIDRequest"];
set theRequest to current application's NSXMLElement's alloc()'s initWithName:"GetSessionIDRequest"

--[theRequest setAttributesAsDictionary:[NSDictionary dictionaryWithObject:@"urn:ebay:apis:eBLBaseComponents" forKey:@"xmlns"]];
theRequest's setAttributesAsDictionary:{xmlns:"urn:ebay:apis:eBLBaseComponents"}

--NSXMLElement *theCredentials = [[NSXMLElement alloc] initWithName:@"eBayAuthToken" stringValue:@"The Auth Token"];
set theCredentials to current application's NSXMLElement's alloc()'s initWithName:"RequesterCredentials"

--[theRequest addChild:theCredentials];
theRequest's addChild:theCredentials

--NSXMLDocument *theXml = [[NSXMLDocument alloc] initWithRootElement:theRequest];
set theXml to current application's NSXMLDocument's alloc()'s initWithRootElement:theRequest

--[theXml setCharacterEncoding:@"utf-8"];	
theXml's setCharacterEncoding:"utf-8"

--[theXml setVersion:@"1.0"];
theXml's setVersion:"1.0"

--NSMutableURLRequest *theHttp = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.sandbox.ebay.com/ws/api.dll"]];
set theHttp to ((current application's NSMutableURLRequest's alloc)'s initWithURL:(current application's NSURL's URLWithString:"https://api.sandbox.ebay.com/ws/api.dll"))

--[theHttp setHTTPMethod:@"POST"];
theHttp's setHTTPMethod:"POST"

--[theHttp addValue:@"419" forHTTPHeaderField:@"X-EBAY-API-COMPATIBILITY-LEVEL"];
theHttp's addValue:"419" forHTTPHeaderField:"X-EBAY-API-COMPATIBILITY-LEVEL"

--[theHttp addValue:DevID forHTTPHeaderField:@"X-EBAY-API-SESSION-CERTIFICATE"];
theHttp's addValue:DevID forHTTPHeaderField:"X-EBAY-API-SESSION-CERTIFICATE"

--[theHttp addValue:AppID forHTTPHeaderField:@"X-EBAY-API-SESSION-CERTIFICATE"];
theHttp's addValue:AppID forHTTPHeaderField:"X-EBAY-API-SESSION-CERTIFICATE"

--[theHttp addValue:CertID forHTTPHeaderField:@"X-EBAY-API-SESSION-CERTIFICATE"];
theHttp's addValue:CertID forHTTPHeaderField:"X-EBAY-API-SESSION-CERTIFICATE"

--[theHttp addValue:DevID forHTTPHeaderField:@"X-EBAY-API-DEV-NAME"];
theHttp's addValue:DevID forHTTPHeaderField:"X-EBAY-API-DEV-NAME"

--[theHttp addValue:AppID forHTTPHeaderField:@"X-EBAY-API-APP-NAME"];
theHttp's addValue:AppID forHTTPHeaderField:"X-EBAY-API-APP-NAME"

--[theHttp addValue:CertID forHTTPHeaderField:@"X-EBAY-API-CERT-NAME"];
theHttp's addValue:CertID forHTTPHeaderField:"X-EBAY-API-CERT-NAME"

--[theHttp addValue:@"GetSessionID" forHTTPHeaderField:@"X-EBAY-API-CALL-NAME"];
theHttp's addValue:"VerifyAddItem" forHTTPHeaderField:"X-EBAY-API-CALL-NAME"

--[theHttp addValue:@"0" forHTTPHeaderField:@"X-EBAY-API-SITEID"];
theHttp's addValue:"0" forHTTPHeaderField:"X-EBAY-API-SITEID"

--[theHttp addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
theHttp's addValue:"text/xml" forHTTPHeaderField:"Content-Type"

--[theHttp setHTTPBody:[theXml XMLData]];
theHttp's setHTTPBody:(theXml's XMLData)

--NSURLResponse *theResponse = nil;
--NSError *theError;
--NSData *theData = nil;
--theData = [NSURLConnection sendSynchronousRequest:theHttp returningResponse:&response error:&error];
set {theData, theResponse, theError} to current application's NSURLConnection's sendSynchronousRequest:theHttp returningResponse:(reference) |error|:(missing value)

--// If the request was successful, show the data.
--        if ([theData length] > 0) {
-- NSXMLDocument *theResult = [[NSXMLDocument alloc] initWithData:theData options:0 error:&error];
--NSLog(@"Received %@", theResult);

if theData's length > 0 then
	set theResult to ((current application's NSXMLDocument)'s alloc)'s initWithData:theData options:0
	log "Received: " & theResult
end if


I feel like my problem lies within the following


--[theHttp setHTTPBody:[theXml XMLData]];
theHttp's setHTTPBody:(theXml's XMLData)

as the external editor is constantly changing it to “theHttp’s setHTTPBody:(theXml’s xmlData)”

I just can’t seem to figure it out…

That sounds like you initially type it in the wrong case, have it in the wrong case elsewhere in the script, or have a terminology conflict. Try using pipes. And don’t leave off the trailing ():

theHttp's setHTTPBody:(theXml's |XMLData|())

Thank you Shane!
Everything is up and working now.
The whole pipe thing is what got me…
I understand to use them when there is an applescript keyword… I guess I have to pipe things that I feel might be clashing or Is there a list of clashing methods?

No, there is no list – it depends on things like what scripting additions you have installed.

Another shameless plug, but that’s why ASObjC Explorer for Mavericks is the ideal external editor: it loads all the terminology at launch, and its code-completion adds whatever pipes are needed for you.