Attachment 'HTTP.v1.0.txt'

Download

   1 :Class HTTP
   2 ⍝ Provides a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI.
   3 
   4 ⍝ List of Methods:
   5 ⍝ GET    = Sends a GET request to the specified Uri and returns the response as Bytes in an asynchronous operation.
   6 
   7 
   8 ⍝ .Net assemblies:
   9 :Using
  10 :Using System,System.dll
  11 :Using System.Net,System.dll
  12 :Using System.Net.Http,System.Net.Http.dll
  13 
  14     (⎕IO ⎕ML ⎕WX)←1 3 3  ⍝ System variables
  15 
  16     :Field Public _Uri
  17     :Field Public _TimeOut←5000 ⍝ Default Time-out value in milliseconds
  18 
  19     ∇ Init0
  20       :Access Public
  21       :Implements Constructor
  22       _Uri←⎕NULL
  23 
  24 
  25     ∇ r←BuildUri url
  26     ⍝ Build a Uniform Resource Identifier (URI)
  27     ⍝ url = IP Address as numbers (192 168 1 1) or characters ('192.168.1.1' or 'www.mysite.com')
  28      
  29     ⍝ r[1] = 1 for Success, 0 for Failure
  30     ⍝ r[2] = URI if Successfull, Literal Error if Failure
  31      
  32       :Trap 0
  33           :If 0=↑1↑0⍴url ⍝ Numbers ?
  34               url←(⎕NEW IPAddress(⊂,url)).ToString
  35               _Uri←(⎕NEW UriBuilder(⊂,url)).Uri
  36               r←1 _Uri
  37      
  38           :Else
  39             ⍝ Characters
  40               _Uri←(⎕NEW UriBuilder(⊂,url)).Uri
  41               r←1 _Uri
  42      
  43           :EndIf
  44      
  45       :Else
  46         ⍝ Show the error.
  47           :If 90=⎕EN
  48             ⍝ .Net Error
  49               r←0('URI: ',⎕EXCEPTION.GetBaseException.Message)
  50           :Else
  51             ⍝ APL Error
  52               r←0(1⊃⎕DM),': ',{(' '=1↑⍵)↓((1↓a,0)∨a←' '≠⍵)/⍵}(2⊃⎕DM)
  53           :EndIf
  54       :EndTrap
  55 
  56 
  57 
  58     ∇ r←{timeOut}GET(url get);http;task;uri
  59       :Access Public
  60     ⍝ Sends a GET request to the specified Uri and returns the response as Bytes in an asynchronous operation.
  61     ⍝ url = IP Address as numbers (192 168 1 1) or characters ('192.168.1.1' or 'www.mysite.com')
  62     ⍝ get = GET request
  63     ⍝ timeOut = Optional delay to wait for the completion of the GET request
  64     ⍝           If not specify, the value of the Field _TimeOut will be used
  65 
  66     ⍝ r[1] = 1 for Success, 0 for Failure
  67     ⍝ r[2] = Response as bytes if Successful, Literal Error if Failure
  68     ⍝        The response can be transform to characters by using ⎕UCS with the proper encoding.
  69 
  70     ⍝ TYPICAL EXAMPLE:
  71 
  72     ⍝ http ← ⎕NEW HTTP
  73     ⍝ request ← http.GET 'www.rfc-editor.org' '/rfc/rfc793.txt'
  74     ⍝ request ← ⎕UCS 2⊃request                   ⍝ Convert bytes to characters with no encoding
  75     ⍝ request ← ⊃{⎕ML←3 ⋄ (⍵≠⎕UCS 10)⊂⍵}request  ⍝ Format the request as text
  76 
  77     ⍝ request ← http.GET 'http://www.google.com' '/search?q=dyalog+apl'
  78     ⍝ request ← ⎕UCS 2⊃request                   ⍝ Convert bytes to characters with no encoding
  79     ⍝ request ← '<',⊃{⎕ML←3 ⋄ (⍵≠'<')⊂⍵}request  ⍝ Format the request as HTML
  80      
  81     ⍝ Parse the url
  82       :If 0=1↑r←BuildUri url
  83         ⍝ Failure to build the URI. Return the error as result.
  84           →0
  85       :Else
  86         ⍝ Success
  87           uri←2⊃r
  88       :End
  89      
  90     ⍝ Set timeOut from field if not present
  91       :If 0=⎕NC'timeOut'
  92           timeOut←_TimeOut
  93       :End
  94      
  95     ⍝ Make the GET request
  96       :Trap 0
  97           http←⎕NEW HttpClient
  98           http.Timeout←⎕NEW TimeSpan(0,0,0,timeOut)
  99           http.BaseAddress←uri
 100           task←http.GetByteArrayAsync(⊂,get)
 101      
 102          ⍝ Wait no more than _TimeOut for the task to complete
 103           :If ⎕TSYNC{task.Wait ⍵}&(_TimeOut)
 104             ⍝ Success
 105               r←1 task.Result
 106               task.Dispose
 107           :Else
 108             ⍝ Failure
 109               r←0('HTTP GET Task did not complete: ',task.GetBaseException.Message)
 110           :EndIf
 111      
 112       :Else
 113         ⍝ Show the error.
 114           :If 90=⎕EN
 115             ⍝ .Net Error
 116               r←0('HTTP GET: ',⎕EXCEPTION.GetBaseException.Message)
 117           :Else
 118             ⍝ APL Error
 119               r←0(1⊃⎕DM),': ',{(' '=1↑⍵)↓((1↓a,0)∨a←' '≠⍵)/⍵}(2⊃⎕DM)
 120           :EndIf
 121       :EndTrap
 122      
 123     ⍝ Clean-up
 124       :Trap 0
 125           http.Dispose
 126       :EndTrap
 127 
 128 
 129 :EndClass

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2016-01-10 14:28:39, 4.3 KB) [[attachment:HTTP.v1.0.txt]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.