Skip to content

Commit

Permalink
webclient improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mkxcode committed Feb 17, 2018
1 parent cfc8fbb commit 5c2e4cc
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 17 deletions.
17 changes: 14 additions & 3 deletions src/capex.http/HTTPClient.sling
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class MyListener is HTTPClientListener imports cape
prop response as HTTPClientResponse
prop buffer as buffer
prop errorHandler as function<void,string>
prop ctx as LoggingContext

func onResponseReceived(response as HTTPClientResponse) override as bool
{
Expand All @@ -55,8 +56,15 @@ class MyListener is HTTPClientListener imports cape
{
}

func onStatus(message as string) override
{
if message:
Log.debug(ctx, message)
}

func onError(message as string) override
{
Log.error(ctx, message)
if errorHandler:
errorHandler(message)
}
Expand All @@ -66,27 +74,30 @@ class MyListener is HTTPClientListener imports cape
}
}

func execute(client as this, request as HTTPClientRequest, listener as function<void,HTTPClientResponse>, errorHandler as function<void,string> = null) static
func execute(ctx as LoggingContext, client as this, request as HTTPClientRequest, listener as function<void,HTTPClientResponse>, errorHandler as function<void,string> = null) static
{
var ll = new MyListener()
ll.setCtx(ctx)
ll.setErrorHandler(errorHandler)
execute(client, request, ll)
if listener:
listener(ll.getResponse())
}

func executeForBuffer(client as this, request as HTTPClientRequest, listener as function<void,HTTPClientResponse,buffer>, errorHandler as function<void,string> = null) static
func executeForBuffer(ctx as LoggingContext, client as this, request as HTTPClientRequest, listener as function<void,HTTPClientResponse,buffer>, errorHandler as function<void,string> = null) static
{
var ll = new MyListener()
ll.setCtx(ctx)
ll.setErrorHandler(errorHandler)
execute(client, request, ll)
if listener:
listener(ll.getResponse(), ll.getBuffer())
}

func executeForString(client as this, request as HTTPClientRequest, listener as function<void,HTTPClientResponse,string>, errorHandler as function<void,string> = null) static
func executeForString(ctx as LoggingContext, client as this, request as HTTPClientRequest, listener as function<void,HTTPClientResponse,string>, errorHandler as function<void,string> = null) static
{
var ll = new MyListener()
ll.setCtx(ctx)
ll.setErrorHandler(errorHandler)
execute(client, request, ll)
if listener:
Expand Down
2 changes: 1 addition & 1 deletion src/capex.web/JSONAPIClient.sling
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* SOFTWARE.
*/

class imports cape:
class:

prop apiUrl as string

Expand Down
23 changes: 14 additions & 9 deletions src/capex.web/NativeWebClient.sling
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,26 @@
* SOFTWARE.
*/

class imports cape:
class:

func instance static as WebClient
{
IFDEF("target_android") {
return(new WebClientForAndroid())
IFDEF "target_android" {
return new WebClientForAndroid()
}
ELSE IFDEF("target_js") {
return(new WebClientForJavaScript())
ELSE IFDEF "target_js" {
return new WebClientForJavaScript()
}
ELSE IFDEF("target_iosoc") {
return(new WebClientForIOS())
ELSE IFDEF "target_iosoc" {
return new WebClientForIOS()
}
ELSE IFDEF "target_cs" {
// FIXME: Should also provide the asynchronous version that can be
// used in GUI (?) environments.
return new WebClientForDotnetSync()
}
ELSE {
ERROR("Not implemented.")
return(null)
ERROR "Not implemented."
return null
}
}
2 changes: 1 addition & 1 deletion src/capex.web/WebClient.sling
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
* SOFTWARE.
*/

interface imports cape:
interface:

func query(method as string, url as string, headers as KeyValueList<string,string>, body as buffer, callback as function<void, string, KeyValueList<string,string>, buffer>)
2 changes: 1 addition & 1 deletion src/capex.web/WebClientForAndroid@target_android.sling
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* SOFTWARE.
*/

class is WebClient imports cape:
class is WebClient:

IFDEF("target_android") {
depend "androidPermission:android.permission.INTERNET"
Expand Down
77 changes: 77 additions & 0 deletions src/capex.web/WebClientForDotnetSync@target_cs.sling
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

/*
* This file is part of Jkop
* Copyright (c) 2016-2018 Job and Esther Technologies Oy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

class is WebClient:

func query(method as string, url as string, headers as KeyValueList<string,string>, body as buffer, callback as function<void, string, KeyValueList<string,string>, buffer>)
{
lang "cs" {{{
var rq = System.Net.WebRequest.Create(url);
rq.Method = method;
}}}
if headers {
var it = headers.iterate()
while it {
var pair = it.next()
if not pair:
break
lang "cs" {{{
rq.Headers[pair.key] = pair.value;
}}}
}
}
if body {
lang "cs" {{{
rq.ContentLength = body.Length;
System.IO.Stream stream = rq.GetRequestStream();
stream.Write(body, 0, body.Length);
stream.Close();
}}}
}
var status = 0
var hdrs = new KeyValueList<string,string>()
var rbody as buffer
lang "cs" {{{
var resp = (System.Net.HttpWebResponse)rq.GetResponse();
if(resp != null) {
status = (int)resp.StatusCode;
foreach(string key in resp.Headers.AllKeys) {
hdrs.add(key, resp.Headers[key]);
}
}
var rs = resp.GetResponseStream();
if(rs != null) {
byte[] buffer = new byte[16*1024];
using(System.IO.MemoryStream ms = new System.IO.MemoryStream()) {
int read;
while((read = rs.Read(buffer, 0, buffer.Length)) > 0) {
ms.Write(buffer, 0, read);
}
rbody = ms.ToArray();
}
}
}}}
if callback:
callback(String.forInteger(status), hdrs, rbody)
}
2 changes: 1 addition & 1 deletion src/capex.web/WebClientForIOS@target_iosoc.sling
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* SOFTWARE.
*/

class is WebClient imports cape:
class is WebClient:

depend "plist:{ \"NSAppTransportSecurity\" : { \"NSAllowsArbitraryLoads\" : \"true\" } }"

Expand Down
2 changes: 1 addition & 1 deletion src/capex.web/WebClientForJavaScript@target_js.sling
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* SOFTWARE.
*/

class is WebClient imports cape:
class is WebClient:

func query(method as string, url as string, requestHeaders as KeyValueList<string,string>, body as buffer, callback as function<void, string, KeyValueList<string,string>, buffer>)
{
Expand Down
1 change: 1 addition & 0 deletions src/capex.web/this.pling
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
title = "Jkop CAPE Web Extensions"
description = "Web related classes for Jkop CAPE"
type = "library"
preFilters += "@cape"

0 comments on commit 5c2e4cc

Please sign in to comment.