API Wrapper Classes

Posted by beakersoft | Posted in Programming | Posted on 08-11-2007

0

The past couple of days I have been on a short course run by one of our vendors about using the API from one of there products.

Now I have to firstly applaud them for making there product open enough for us to call it using its api, there are some bits of business logic missing, but for the most part it is very good and very well documented.

The only problem with it is the amount of parameters you can pass to some of the methods, and the amount of information you get back. In some case’s there are over 30 inputs to a method, but only 2 are mandatory. So Calling the API could look something like this:

result = AppInteraction.API(“APICustSearch”, pi_pointer, pi_source, type.missing, type.missing, type.missing) etc etc

This kind of code while functional can get very messy and hard to read. What we could do with is a class to wrap the functionality of the API calls in.

First thing to do is create a couple of classes for the input and outputs of the method. This example is using the method called APICustSearch, it will have 5 possible inputs and 3 outputs.

Public Class CustSearch _input
Public Spointer As String

Public Source As String

Public date As String

Public Active As String
Public MinSpend As String

End Class

Public Class CustSearch _results
Public Result As Integer

Public Resulttext As String

Public Custname As String

End Class

The above classes just deal with the inputs/outputs of the api, next we need to code the actual api call into a class:

Public Class AppWrapper
‘ Defintion for API APICustSearch
Public Function CustSearch(ByVal oInput As CustSearch_input) As CustSearch_results

Dim oApp As New AppAPI.Interaction
Dim oResults As New AppAPI.colGeneric
Dim oReturn As New Suborderrestart_results

oResults = oApp.API(“APICustSearch”, oInput.Spointer , oInput.Source, oInput.date, oInput.Active, oInput.MinSpend )

While System.Runtime.InteropServices.Marshal.ReleaseComObject(oApp) > 0
End While
While System.Runtime.InteropServices.Marshal.ReleaseComObject(oResults) > 0
End While

oResults = Nothing
oApp = Nothing

Return oReturn
End Function

End Class

So now we have our class to call the functionality of the API method we are interested in, we can easily call it from our application like so:

Dim App As New AppWrapper
Dim CustSearchParam As New CustSearch_input
Dim search_result As CustSearch_results

With CustSearchParam
.Spointer = “12345″
.Source = “API”
.date = “08/11/2007″
.Active = “Y”
.MinSpend = “0″
End With

search_result = App.CustSearch(CustSearchParam)

‘this is some checking specific to the API i was working with. If the search_result.Result was not 0
‘an error had happened and the details were in search_result.Resulttext
If (search_result.Result <> 0) Then
MessageBox.Show(search_result.Resulttext, “Search error”)
End If

If (search_result.Result = 0) Then
MessageBox.Show(search_result.Resulttext, “SubOrderRestart Suceeded” & vbnewline & search_result.Custname )
End If

You will obviously have to tailor the classes based on the api you are using, but you can just add all the methods you use the the AppWrapper class, and create new input and output classes based on the spec.

Apologies for any errors in the example code, I have re-written sections of mine to make it more generic, but hopefully you should get the idea

Google Maps and IE6

Posted by beakersoft | Posted in Programming | Posted on 02-10-2007

0

I’ve been doing some messing about over the last week or so with the Google maps and Fickr API’s, to get photos on Flickr displayed on a map.

After much tinkering and hacking around, I finally got it working. You can see the results on my beer website at http://www.oldmanales.co.uk/feat_OktoberFest07.asp

While writing this I came across a strange bug that seamed to effect IE6. I only started noticing it the last couple of days though, so maybe Google have been messing with the code.

Anyway, what would happen is when I loaded my map I got the little error icon at the bottom left corner of my browser. Double clicking it gave this error:

GIcon is undefinded

GIcon was the variable I was using to create a marker on the map that you can click and it would show a pop-up window.

I looked at a few posts, some suggested changing the version of the maps api, some building the icon differently and a couple more. However, the only one that worked for me was going into IE’s advanced settings and turning on allow HTTP 1.1 through proxy connections option.


Once this had been turned on I know longer got the error and my map is displayed without ant problems.

Once I thought about the error I realised that I noticed it first when I checked the map at work, using a proxy server, and judging by the name of the setting it probably only happens when using a proxy to access the web.
I’m not entity sure what the setting means but i will look into it.

On the topic of the map, once I have finished it off all the features i want to put in it and I’ve tested it as much as possible i will post the code for it on here.

Open to everyone

Posted by beakersoft | Posted in Programming | Posted on 19-07-2007

0

If you look at the right hand side off my blog now, you’ll will see that you can now translate my blog into different languages. The idea came about when I was looking on FeedBurner at the different places visitors were coming from, and all the different languages they were using.

I had a quick look around the web to see if there were any ‘widgets’ I could steel to accomplish this. The only one I could find was using Babel fish by Alta Vista, you can get it at http://www.altavista.com/help/free/free_searchbox_transl the only thing that put me of using this was the Google translate tool supported more languages.

So I headed over to http://www.google.com/translate_t, and basicly looked at the source code of that page and created a form of my own. Now if you click on one of the flags it will translate the blog page your are looking at to your language. Simple but effective!

If you are in the UK try and get hold of a copy of this weeks IT Week, on page 27 there’s a really interesting article about a company doing some interesting research into the future of power consumption. Unfortunately I can find it on there website so you’ll have to get hold of the magazine.

Navigating XML and Unknown Injurys

Posted by beakersoft | Posted in Programming | Posted on 16-07-2007

0

I learnt something about readingXML file’s today, using VB.net and XPath.
I had an xml file, and some of the data in it were tags similar to this:

<CustomField name =”title” type=”str”>This is a Title</CustomField>
<CustomField name =”titleDate” type=”dte”>01/01/2007</CustomField>

So, couldn’t just navigate to the CustomField tag, as there was more than one of them. I’ve never really gone to much into how to navigate round an XML file, i’ve always just gone straight to the node I was interested in and pulled out the data.

This time, I had to delve more into the XPath navigator. I found out I could use the following in my XPathNodeIterator, to return me only nodes that contained the attribute title:

/Story/StoryData/CustomField[@Name="Title"]

I could then iterate round all the retuned nodes and process the data. Some of the XML processing tools and classes in .Net do take a bit of time to get your head round, but they do seam to work well and are very fast.

On another note, I went over on my ankle playing football (soccer if your in the US) on Sunday, it swelled up pretty bad so I went to the hospital.
They sent me for an x-ray, and it turned out it was just badly sprained this time, but i’ve broken or chipped it before and didn’t notice. How the hell did that happen!
I’m now stuck at home for a couple of boring days to let it heel a bit, they said i shouldn’t even drive for a week. Not bloody likely!

Got another article on aspfree.com its about controlling Windows 2003 fax services through an application, you can read it here