Text Code Samples - Web Service to API Conversion

Document created by JackCowardin Administrator on Oct 26, 2016Last modified by JackCowardin Administrator on Dec 8, 2016
Version 5Show Document
  • View in full screen mode

The following are C# .NET code samples in text format. These are basic console applications intended to provide the basic programming statements to invoke a GuideStar Web Service or ReST API.

 

Code Sample to Call the Detail Web Service:

http://Web Service URL https/Web Service URL: https://gsservices.guidestar.org/GuideStar_SearchService/SearchService.asmx

 

Web Service WSDL:  https://gsservices.guidestar.org/GuideStar_SearchService/SearchService.asmx?WSDL

 

Description:

The web service takes an xml string as input and returns an xml string containing the search results. The input xml string must adhere to the input schema, which can be found at

https://gsservices.guidestar.org/GuideStar_SearchService/WebServiceSearchQuery.xsd

 

The output xml string will adhere to the output schema, which can be found at

https://gsservices.guidestar.org/GuideStar_SearchService/WebServiceSearchReturnMaster.xsd.

  

Querying the Web Service:
Per the input schema, certain elements must appear in the xml input string for every query.  These elements, and a brief description, are:

            version – represents the current version of the schema, currently 1.0

            login – Web Service username

     password – Web Service password

     pageSize – Number of results returned per web service request

     offset – Within the total result set, the result number to begin retrieving from (e.g., for 1000 total results, a page size of 100 and an offset of 50, you will receive records 50-150).

 

The remaining elements within the schema may or may not be included in a query. 

 

Source code in C# .NET for the Detail Web Service:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using GSWebSvc.org.guidestar.gsservices;

 

namespace GSWebSvc

{

  class Program

  {

    static void Main(string[] args)

    {

      string user = "<username here>";

      string passwd= "<password here>";

      SearchService ws = new SearchService();

      string xml_input = createXMLQuery("1", user, password, "10", "0", "cleveland");

      string webSvcReturnVal = ws.GuideStarDetail(xml_input);   // could use "GuideStarBasic", etc.

      Console.WriteLine(webSvcReturnVal);

      Console.ReadKey();

    }

 

    private static string createXMLQuery(string version, string user, string passwd, string pageSize, string offset, string keyword)

    {

      StringBuilder xquery = new StringBuilder();

      xquery.Append("<query>");

      xquery.Append("<version>" + version + "</version>");

      xquery.Append("<login>" + user + "</login>");

      xquery.Append("<password>" + passwd + "</password>");

      xquery.Append("<pageSize>" + pageSize + "</pageSize>");

      xquery.Append("<offset>" + offset + "</offset>");

      xquery.Append("<keyword>" + keyword + "</keyword>");

      xquery.Append("</query>");

      return xquery.ToString();

    }

  }

}

Source code in C# .NET for the Detail ReST API:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Net;

using System.IO;

 

namespace GSRestCLient

{

  class Program

  {

    static void Main(string[] args)

    {

      string user = "<username here>";

      string passwd= "<password here>";

      var req = WebRequest.Create( "https://data.guidestar.org/v1/detail/7394706.xml");

      req.Credentials = new NetworkCredential(user, passwd);

      var response = req.GetResponse();

      Stream dataStream = response.GetResponseStream();

      Console.WriteLine(new StreamReader(dataStream).ReadToEnd());

      Console.ReadKey();

    }

  }

}

Another C# Example, using the RestSharp library.

 

RestSharp is a simple but powerful library for use in calling Web APIs. RestSharp can simplify web API calls but can also be used for deserialization of the API response and provides access to the entire response from an API call, including HTTP headers.

More about JSON and XML Deserialization...

RestSharp is available here:

RestSharp - Simple REST and HTTP Client for .NET

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using RestSharp;

using RestSharp.Authenticators;

 

namespace GSRestCLient

{

  class Program

  {

    static void Main(string[] args)

    {

      string user = "<username here>";

      string passwd= "<password here>";

      RestClient restClient = new RestClient( "https://data.guidestar.org/v1/detail/7394706.xml");

      restClient.Authenticator = new HttpBasicAuthenticator(user, passwd);

      var request = new RestRequest(Method.GET);

      IRestResponse result = restClient.Execute(request);

      Console.WriteLine(result.Content);

      Console.ReadKey();

    }

  }

}

CharityCheck PDF API Example:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using RestSharp;

using RestSharp.Authenticators;

using System.Net;

using System.IO;

using System.Diagnostics;

 

namespace GSRestCLient

{

  class Program

  {

    static void Main(string[] args)

    {

      string ein = "13-1624102";          //use this Employer ID number for the call.

     

      // create the web request

      var req = WebRequest.Create("https://data.guidestar.org/v3/CharityCheck/pdf/" + ein);

      req.Credentials = new NetworkCredential("<user name here>", "<password here>");            

      var response = req.GetResponse();

 

      // store the results in a local file with the name according to the EIN to be read

      // by the rendering program, e.g, Acrobat or a browser

      string file = "C:\\" + ein + ".pdf";     

      using (Stream output = File.Open(file, FileMode.Create))

      {

        using (Stream input = response.GetResponseStream())

        {

          input.CopyTo(output);

        }

      }

    

      // invoke Adobe Acrobat to render the PDF

      string app_path = "C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\AcroRd32.exe";

      Process.Start(app_path, file);     

    }

  }

}

 

Back To Introduction           Converting from GuideStar Web Services (SOAP) to APIs (REST).

Back to Code Examples       

Web Service To API Conversion - Detail API Example

Web Service To API Conversion - CharityCheck PDF Example

Attachments

    Outcomes