Search code examples
iosmknetworkkit

MKNetworkKit - Host Name and apiPath parameters


I was thinking of using MKNetworkKit as base for Network operations for an iOS App. I'm a bit confused about using the initWithHostName:customerHeaders: and initWithHostName: apiPath:customerHeaders: methods.

In the App, I need to communicate to a number of hosts and they have different ways of specifying the Host Name and URL. For example:

HostNameX.com - The Domain Name for HostX,

The first host has the service prepended to the Hostname as so:

serviceA.HostNameX.com?someparam=value The Path for ServiceA (returns info) serviceB.HostNameX.com?someparam=value The Path for ServiceB (returns info)


HostNameY.com - The Domain Name for HostY

The second host has the service or command appended to the Hostname as so:

HostNameY.com/serviceA?someparam =value The Path for ServiceA (returns info) HostNameY.com/serviceB?someparam =value The Path for ServiceB (returns info)


For the HostNameY.com case, I was thinking of creating one MKEngine Instance and then passing in the "serviceA?someparam=value" or "serviceB?someparam=value" string as the Path parameter to the operationWithPath:params:httpMethod:ssl: method.

This would work well for what I want to do, however for HostNameX.com I'm not sure how to prepend "serviceA." or "serviceB." to the host name? The only way I can see of doing it is to create two separate MKEngine Instances one for "serviceA" and one for "serviceB". Is this the case?

Am I missing something or is there a way to be able to prepend the service to the domain name after calling initWithHostName:apiPath:customerHeaders: ?

Thanks in advance for any suggestions. All the Best Dave


Solution

  • I solved this problem by using the operationWithURLString and building my own URL String. To me anyway, this seems a much better solution than allocating a number of MKEngine Instances just to handle something that can be passed as a parameter. This way I can also append path components as well as parameters to the URL String, for example:

    http://serviceA.hostname.com/fred/simmons?isvip=true&hasBooked=false

    Interestingly, the parameter dictionary that is passed to operationWithURLString:params:httpMethod: works in two ways:

    if the httpMethod is GET, then the dictionary is used to pass parameters in the URL, if the httpMethod is POST or PUT (or DELETE?) then the Dictionary is formatted and sent as the Request Body.

    Question - is it valid to have a POST Command with parameters?

    For example:

    http://bookingservice.hostname.com/bookings?isvip=true&lastname=simmons

    Is this a valid URL as a POST not a GET?