Sample code for 30+ languages & platforms
Swift

Belgium eHealth Platform - AddressBook - Search for Professionals

See more Belgian eHealth Platform Examples

Demonstrates how to search for professionals using the AddressBook API.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // --------------------------------------------------------------------------------
    // Also see Chilkat's Online WSDL Code Generator
    // to generate code and SOAP Request and Response XML for each operation in a WSDL.
    // --------------------------------------------------------------------------------

    // Provide a certificate + private key.
    // Note: If your certificate + private key is located on a hardware token or smartcard, you can call a different function to load from smartcard..
    let cert = CkoCert()!
    success = cert.loadPfxFile(path: "SSIN=12345678.acc.p12", password: "p12_password")
    if success == false {
        print("\(cert.lastErrorText!)")
        return
    }

    // Let's get the SAML Security Token obtained from https://services-acpt.ehealth.fgov.be/IAM/SecurityTokenService/v1/RequestSecurityToken
    // a very short time ago..

    // See this example for how we got the SAML security token: eHealth Belgium Request Security Token
    let bdSecToken = CkoBinData()!
    success = bdSecToken.loadFile(path: "qa_data/tokens/ehealth-fgov-be-sectoken.xml")
    if success == false {
        print("Failed to load SAML security token")
        return
    }

    // The SAML security token is just the part from <Assertion ..> ... </Assertion>
    // So let's get just that part..
    var startIdx: Int = bdSecToken.findString(str: "<Assertion", startIdx: 0, charset: "utf-8").intValue
    if startIdx < 0 {
        print("Did not find the start of the SAML assertion.")
        return
    }

    var endIdx: Int = bdSecToken.findString(str: "</Assertion>", startIdx: 0, charset: "utf-8").intValue
    if endIdx < 0 {
        print("Did not find the end of the SAML assertion.")
        return
    }

    // Adjust the index to the 1st byte after "</Assertion>"
    endIdx = endIdx + 12
    var base64_saml_token: String? = bdSecToken.getEncodedChunk(offset: startIdx, numBytes: endIdx - startIdx, encoding: "base64")

    print("\(base64_saml_token!)")

    // Our SOAP request will look like this:

    // The SOAP request to search for professionals looks like this:

    // <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:add="urn:be:fgov:ehealth:addressbook:protocol:v1">
    //     <soapenv:Header>
    // 	      <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    // 	         <wsse:BinarySecurityToken
    // 	            ValueType="http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.0#SAMLAssertionID"
    // 	            EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">BASE64_SAML_TOKEN</wsse:BinarySecurityToken>
    // 	      </wsse:Security>
    //     </soapenv:Header>
    //     <soapenv:Body>
    //         <add:SearchProfessionalsRequest Id="bdc38ae62-3e7f-4f80-80f7-c3e745500fa3" IssueInstant="2016-03-23T18:49:26.968+01:00" Offset="0" MaxElements="100">
    //             <add:SSIN>74062423769</add:SSIN>
    //         </add:SearchProfessionalsRequest>
    //     </soapenv:Body>
    // </soapenv:Envelope>

    let xml = CkoXml()!
    xml.tag = "soapenv:Envelope"
    xml.updateAttrAt(tagPath: "soapenv:Header|wsse:Security", autoCreate: true, attrName: "xmlns:wsse", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")
    xml.updateAttrAt(tagPath: "soapenv:Header|wsse:Security|wsse:BinarySecurityToken", autoCreate: true, attrName: "ValueType", attrValue: "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.0#SAMLAssertionID")
    xml.updateAttrAt(tagPath: "soapenv:Header|wsse:Security|wsse:BinarySecurityToken", autoCreate: true, attrName: "EncodingType", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary")

    xml.updateChildContent(tagPath: "soapenv:Header|wsse:Security|wsse:BinarySecurityToken", value: base64_saml_token)

    xml.updateAttrAt(tagPath: "soapenv:Body|add:SearchProfessionalsRequest", autoCreate: true, attrName: "Id", attrValue: "bdc38ae62-3e7f-4f80-80f7-c3e745500fa3")

    let dt = CkoDateTime()!
    dt.setFromCurrentSystemTime()
    xml.updateAttrAt(tagPath: "soapenv:Body|add:SearchProfessionalsRequest", autoCreate: true, attrName: "IssueInstant", attrValue: dt.get(asTimestamp: true))

    xml.updateAttrAt(tagPath: "soapenv:Body|add:SearchProfessionalsRequest", autoCreate: true, attrName: "Offset", attrValue: "0")
    xml.updateAttrAt(tagPath: "soapenv:Body|add:SearchProfessionalsRequest", autoCreate: true, attrName: "MaxElements", attrValue: "100")
    xml.updateChildContent(tagPath: "soapenv:Body|add:SearchProfessionalsRequest|urn:SSIN", value: "74062423769")

    print("\(xml.getXml()!)")

    let http = CkoHttp()!

    success = http.setSslClientCert(cert: cert)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    http.setRequestHeader(name: "Content-Type", value: "text/xml")
    http.setRequestHeader(name: "SOAPAction", value: "urn:be:fgov:ehealth:addressbook:protocol:v1:searchProfessionals")

    var xmlStr: String? = xml.getXml()
    let resp = CkoHttpResponse()!
    success = http.httpStr(verb: "POST", url: "https://services.ehealth.fgov.be/AddressBook/v1", bodyStr: xmlStr, charset: "utf-8", contentType: "application/xml", response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    print("\(resp.bodyStr!)")
    print("response status code = \(resp.statusCode.intValue)")

}