Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get API in VFP9

Status
Not open for further replies.

Qamar Zaman

Programmer
Aug 9, 2019
7
PK
Hi Experts,

I am very week in VFP programming. I want to get API and return result in vfp9. I have .NET code of API. I want to same API called through VFP.

Thanks in advance

.NET form
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http;
using Newtonsoft.Json;
using static System.Net.Mime.MediaTypeNames;


namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        HttpClient Client= new HttpClient();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataModles dd = new DataModles();   
            var content = new StringContent(JsonConvert.SerializeObject(dd.objInv), Encoding.UTF8, "application/json");
            HttpResponseMessage response = Client.PostAsync("[URL unfurl="true"]http://localhost:8524/api/IMSFiscal/GetInvoiceNumberByModel",[/URL] content).Result;
            if (response.IsSuccessStatusCode)
            {
                label1.Text = string.Empty;
                label1.Text += $"Response from API\n\r———————————————\n\r{response.Content.ReadAsStringAsync().Result}";
            }
        }
    }
}

Object of DataModles
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp2
{
    class DataModles
    {
        public Invoice objInv = new Invoice();
        public DataModles()
        {
            objInv.InvoiceNumber = "12333";
            objInv.POSID = 123123;
            objInv.USIN = "123457";
            objInv.DateTime = DateTime.Now;
            objInv.BuyerNTN = "1234567-9";
            objInv.BuyerCNIC = "12345-1234567-8";
            objInv.BuyerName = "Buyer Name";
            objInv.BuyerPhoneNumber = "0345-1234567";
            objInv.PaymentMode = 1;
            objInv.TotalSaleValue = 120;
            objInv.TotalQuantity = 10;
            objInv.TotalBillAmount = 16610;
            objInv.TotalTaxCharged = 12310;
            objInv.Discount = 1000;
            objInv.FurtherTax = 100;
            objInv.InvoiceType = 1;
            objInv.Items = Items();

        }
        public List<InvoiceItems> Items()
        {
            List<InvoiceItems> lst = new List<InvoiceItems>();
            InvoiceItems objItem = new InvoiceItems();
            objItem.ItemCode = "70";
            objItem.ItemName = "Honey";
            objItem.Quantity = 3;
            objItem.TotalAmount = Convert.ToDouble(3000.00);
            objItem.SaleValue = Convert.ToDouble(3180);
            objItem.TaxCharged = Convert.ToDouble(180);
            objItem.TaxRate = 6;
            objItem.PCTCode = "11001010";
            objItem.FurtherTax = 20;
            objItem.InvoiceType = 1;
            objItem.Discount = 500;
            lst.Add(objItem);
            return lst;
        }
        public class Invoice
        {
            public string InvoiceNumber;
            public int POSID;
            public string USIN ;
            public DateTime DateTime;
            public string BuyerNTN ;
            public string BuyerCNIC ;
            public string BuyerName ;
            public string BuyerPhoneNumber ;
            public int PaymentMode;
            public int TotalSaleValue ;
            public int TotalQuantity;
            public int TotalBillAmount;
            public int TotalTaxCharged ;
            public int Discount;
            public int FurtherTax ;
            public int InvoiceType;
            public List<InvoiceItems> Items ;
        }

        public class InvoiceItems
        {
            public string ItemCode;
            public string ItemName;
            public int Quantity;
            public double TotalAmount;
            public double SaleValue;
            public double TaxCharged;
            public int TaxRate ;
            public string PCTCode ;
            public int FurtherTax;
            public int InvoiceType;
            public int Discount ;
        }
    }
}
 
In VFP, the term API usually refers to the set of functions from Windows (and elsewhere) that you can call from your program and which provides a set of basic services. Some examples: Sleep() pauses execution for a given number of milliseconds; GetWindowText() retrieves the text of a given window's title bar; FileTimeToSystemTime() converts a file time to system time format; Beep() causes the computer to emit a beep. There are thousands of these functions.

If that is the sort of thing you are asking about, you first need to declare the function within your program. You then call it just like a native VFP function, passing parameters and returning a result. The VFP Help topic for DECLARE DLL will give you the syntax.

To give you one simple example, this the DECLARE for GetWindowText():

Code:
DECLARE INTEGER GetWindowText IN user32;
	INTEGER hwnd,;
	STRING @lpString,;
	INTEGER cch

This means that the function returns an integer (the number of characters in the window title). It takes three parameters: the window handle (an integer); a buffer, that is, a character string to receive the result (passed by reference); and the maximum size of the buffer.

However, looking at the code you posted, I wonder if you are using the term API to mean something different. If so, perhaps you could clarify that.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
This C# code mainly creates a json string:

Code:
{"InvoiceNumber":"12333","POSID":123123,"USIN":"123457","DateTime":"2021-07-31T12:24:25.7496731+02:00","BuyerNTN":"1234567-9","BuyerCNIC":"12345-1234567-8","BuyerName":"Buyer Name","BuyerPhoneNumber":"0345-1234567","PaymentMode":1,"TotalSaleValue":120,"TotalQuantity":10,"TotalBillAmount":16610,"TotalTaxCharged":12310,"Discount":1000,"FurtherTax":100,"InvoiceType":1,"Items":[{"ItemCode":"70","ItemName":"Honey","Quantity":3,"TotalAmount":3000.0,"SaleValue":3180.0,"TaxCharged":180.0,"TaxRate":6,"PCTCode":"11001010","FurtherTax":20,"InvoiceType":1,"Discount":500}]}

Not even pretty formatted, but that's fine as it's just for the serverside (localhost) API to process.

You can also create that JSON simply by using TEXT..ENDTEXT with it's textmerge option to merge in the data of an invoice.

The second ingredient you need is a HTTP POST. I can recomend using West Winds wwHTTP for this. Here's a direct link to the documentation of the POST method:

To bring it into context with your localhost URL:
Code:
*** POST a JSON request to a server
TEXT TO lcJson NOSHOW
[{"InvoiceNumber":"12333","POSID":123123,"USIN":"123457","DateTime":"2021-07-31T12:24:25.7496731+02:00","BuyerNTN":"1234567-9","BuyerCNIC":"12345-1234567-8","BuyerName":"Buyer Name","BuyerPhoneNumber":"0345-1234567","PaymentMode":1,"TotalSaleValue":120,"TotalQuantity":10,"TotalBillAmount":16610,"TotalTaxCharged":12310,"Discount":1000,"FurtherTax":100,"InvoiceType":1,"Items":[{"ItemCode":"70","ItemName":"Honey","Quantity":3,"TotalAmount":3000.0,"SaleValue":3180.0,"TaxCharged":180.0,"TaxRate":6,"PCTCode":"11001010","FurtherTax":20,"InvoiceType":1,"Discount":500}]}]
ENDTEXT

*** Recreate wwHttp (best practice)
loHttp = CREATEOBJECT("wwHttp")

*** Post a raw Buffer of data
loHttp.cContentType="application/json"
loHttp.AddPostKey(lcJson)

lcResult = loHttp.Post("[URL unfurl="true"]http://localhost:8524/api/IMSFiscal/GetInvoiceNumberByModel")[/URL]
? lcResult

And then look into lcResult to get out what you need.

Remember, this need West Winds wwHTTP ( and of course the IMSFiscal API running on localhost port 8524.




Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top