ServiceRequiresLogin in Delphi

delphi
(bnx2012) #1

I have converted a RODL based service to Code First so that I can use the new HTTP API. However, there is a lack of detail in the documentation and samples on how to do anything. For example, the documentation says I can use the ServiceRequiresLogin option to replicate the old way. But there is no mention of how, nor any reference in any of the library code. And what about the “old” RequiresSession property in the DataModule - that is still there - is that the real answer still?

Likewise there is a lack of information on the new HTTP API and the attributes and how to use them. I also note that there is an option to support HTTP response codes through exceptions, but how is this handled for the “old” function use? Do I have to define two functions, one for old, one new?

I’d much appreciate some worked examples of all of the options. It would also be good to have more complex examples of a service with a separate login service - all the samples seem to be very simple.

Thanks for moving it all forward though - looks good. I just need to know how to use it!

(EvgenyK) #2

Looks like you read .NET based part about HTTP API.
Delphi part still uses RequiresSession .

I’ve attached Delphi sample that demonstrates usage of sessions:
httpapi_security.zip (33.1 KB)

ServiceBuilder has special editor for HTTP API attributes:

for CodeFirst based services, you need to provide these attributes manually via ROCustom attribute like

// for methods
    [ROCustom('HttpApiPath','calculate')]
    [ROCustom('HttpApiMethod','GET')]
    [ROCustom('HttpApiResult','201')]
    [ROCustom('HttpApiTags', 'tag')]
    [ROCustom('HttpApiOperationId', 'operation')]
// for parameters
    [ROCustom('HttpApiQueryParameter','1')]
    [ROCustom('HttpApiHeaderParameter','1')]

Response code can be provided via

raise EROHttpApiException.Create(HTTP_404_code, HTTP_404_status);

see also HTTP API sample at Adding HttpAPI to a Server (Delphi) article.

Note: You may need to install latest ROD beta.

HttpApi Authentication Sample for Delphi
(bnx2012) #3

Thank you for the response. Yes, it does look like the .Net info is mixed in with the Delphi info quite a bit - that page doesn’t make it clear that it is different.

I would like to know how to handle responses differently according to how the API is called - an HTTP exception causes what to happen when called with the old clients?

I shall separate my questions, so will create a new question on out parameters.

(EvgenyK) #4

you can create http api wrappers for your existing methods that will return required codes, like

function TMyService.httpapi_wrapper_for_method1(...): ...;
begin
  try
     result := method1(...);
  except
    on e: EMYException1 do raise EROHttpApiException.Create(my_httpapi_code1);
    on e: EMYException2 do raise EROHttpApiException.Create(my_httpapi_code2);
    on e: Exception do raise EROHttpApiException.Create(my_httpapi_code3);
  end;
end;

another possibility is detect http api call in OnGetDispatchInfo event and raise correspondent error in old method