A web service for your data

This forum is for posting of useful information
Message
Author
Diego Euri Almanzar
Posts: 167
Joined: Thu Nov 05, 2020 10:51 am
Location: DOMINICAN REPUBLIC

Re: A web service for your data

#11 Post by Diego Euri Almanzar »

Hello dear Roger

Hope you're well.

As always, your ideas are great. Precisely, 3 days ago, I published a topic on your forum, asking for help to use Xbase++ DLL libraries, in a C# program, so as not to lose my code, or many things that I have developed in Xbase++.

The distinguished forum members advised me to develop a REST API, or Webservice.

And since I follow everything you write, I realize that this topic is also related to Webservice.

And after reading your recommendations, and seeing the source program that you have published in this topic, I cannot understand what the instruction is that forms the object for the Webservice.

In case that instruction is CXPENDPOINT, I would face a headache, because with CxpEndpoint, I was never able to publish anything because the use of the certificate was not possible.

For example:

oHttp := CxpEndpoint():new( PORT, "localhost")
cerready := oHttp:setCertificate( "JCSERVER\WEBHOSTING\www.megaprord.com" )

I also used a pfx file with its key, as a certificate, but it never worked.

My question is, with which Xbase instruction, can I develop a REST API, or a Webservice, or an HTTPservice?

It would be enough for this API to have a single main function, because through that function, it would enter the other functions of the DLL.

I will appreciate the help.

User avatar
rdonnay
Site Admin
Posts: 4729
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: A web service for your data

#12 Post by rdonnay »

I also used a pfx file with its key, as a certificate, but it never worked.
I also had problems wih a pfx file.
It would load and register ok, but after a while the service would lock up.
I wrote a timer that would restart the service when it was idle for a time and that worked better but was not fully reliable.
We dealt with that by writing a Java program that was basically an HTTPS proxy.
It used the certificate and called our endpoints using HTTP.

I didn't write that program because I'm not a Java programmer.

We really didn't have a need for a public service, we just wanted to make sure we could provide some security in case we were hacked.
The eXpress train is coming - and it has more cars.

Diego Euri Almanzar
Posts: 167
Joined: Thu Nov 05, 2020 10:51 am
Location: DOMINICAN REPUBLIC

Re: A web service for your data

#13 Post by Diego Euri Almanzar »

Excellent explanation, Roger

Also, over the course of this day, I was studying a little more, and I realized that CXPendpoint is not the same as HTTPendpoint

Since I have been developing CXP programs, I thought it was all the same, but it is not.
Today, I understood that HTTPendpoint is where APIs are developed. I didn't know that instruction. Now I will be able to develop an API to use the Xbase++ DLLs, and the DLLs developed by me with Xbase++

Of course, since it is only to access the Xbase DLLs, I don't need an HTTPS API, with an API for internal use HTTP, on port 81, it is more than enough.

Thank you.

User avatar
SlavkoDam
Posts: 86
Joined: Wed Apr 27, 2022 10:12 am
Location: Negotin, Serbia
Contact:

Re: A web service for your data

#14 Post by SlavkoDam »

Just to remind, in the documentation for WebHandler() class, Alaska gave an example how to create a Web service with HttpEndpoint() and WebHandler() classes. Here is this code.

Code: Select all

// The example shows a server that implements the Endpoint
// CurrentTime/get. Requests to other endpoints on CurrentTime
// receive a default handling

#include "inkey.ch"
#include "web.ch"

#define PORT 81
PROCEDURE Main()
  LOCAL cPort
  LOCAL oHttpEndpoint
  LOCAL nKey

  cPort := AllTrim(Str(PORT))
  ? "Time Server sample."
  ?
  ? "With a web browser navigate to:"
  ?
  ? "     http://localhost:"+cPort+"/CurrentTime/get"
  ? "     http://localhost:"+cPort+"/CurrentTime/unknown"
  ?
  ? "Press ESC to quit"

  //
  // The listener thread is established
  //
  oHttpEndpoint := HttpEndpoint():new( PORT )
  oHttpEndpoint:start()

  // This sample does not do anything special
  nKey := 0
  DO WHILE nKey <> K_ESC
     nKey := Inkey(1)
  ENDDO

  //
  // Stop the endpoint before the application
  // is terminated
  //
  oHttpEndpoint:stop()

RETURN

//
// Handler for the current time
//
CLASS CurrentTime FROM WebHandler
  EXPORTED:
    METHOD get
    METHOD execute
ENDCLASS

//
// The method :get() handles the RESTful path CurrentTime/get
//
METHOD CurrentTime:get()
  LOCAL cThreadId
  LOCAL cResponse := ""

  cThreadId := AllTrim(Str(ThreadId()))

  cResponse += "<!DOCTYPE html>"
  cResponse += "<html>"
  cResponse +=   "<head><title>"+ProcName()+"</title></head>"
  cResponse +=   "<body>"
  cResponse +=     "<h1>Current Time:</h1>"
  cResponse +=     "<p>"+Time()+" (HH:MM:SS)</p>"
  cResponse +=     "<small>By thread: "+cThreadId+"</small>"
  cResponse +=   "</body>"
  cResponse += "</html>"

RETURN cResponse

//
// Dispatch the request to existing methods. Return a generic
// web page for all other requests.
//
METHOD CurrentTime:execute( cMethodName )
  LOCAL cResponse := ""

  IF IsMethod(SELF, cMethodName)
    RETURN SUPER:execute( cMethodName )
  ENDIF

  cResponse += "<!DOCTYPE html>"
  cResponse += "<html>"
  cResponse +=   "<head><title>Unhandled request</title></head>"
  cResponse +=   "<body>"
  cResponse +=     "<h1>No handler for: "+cMethodName+"</h1>"
  cResponse +=     "<p>Please try with: /CurrentTime/get</p>"
  cResponse +=     "<small>Sorry</small>"
  cResponse +=   "</body>"
  cResponse += "<html>"

RETURN cResponse
Rodger's code for Web service is more advanced than Alaska example because it contains multiple Endpoint URLs and uses JSON formatted responses, but basically it implements the same scenario as in Alaska example. This is the right way how to create a Web service with Xbase++ HttpEndpoint() and WebHandler() classes.
Best regards,

Slavoljub Damnjanovic
SD-SoftDesign, Alaska Software Technology Partner
https://www.sd-softdesign.com
https://www.sd-softdesign.rs

Diego Euri Almanzar
Posts: 167
Joined: Thu Nov 05, 2020 10:51 am
Location: DOMINICAN REPUBLIC

Re: A web service for your data

#15 Post by Diego Euri Almanzar »

Excellent, SlavkoDam

Thank you.

Post Reply