-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpUtils2Service.bas
71 lines (60 loc) · 1.75 KB
/
HttpUtils2Service.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Service
Version=6.77
@EndOfDesignText@
#Region Module Attributes
#StartAtBoot: False
#End Region
'HttpUtils2 version 2.10 (based on OkHttp library)
'Service module
Sub Process_Globals
Private hc As OkHttpClient
Private TaskIdToJob As Map
Public TempFolder As String
Private taskCounter As Int
End Sub
Sub Service_Create
TempFolder = File.DirInternalCache
hc.Initialize("hc")
TaskIdToJob.Initialize
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Sub Service_Destroy
End Sub
Public Sub SubmitJob(job As HttpJob) As Int
taskCounter = taskCounter + 1
TaskIdToJob.Put(taskCounter, job)
If job.Username <> "" And job.Password <> "" Then
hc.ExecuteCredentials(job.GetRequest, taskCounter, job.Username, job.Password)
Else
hc.Execute(job.GetRequest, taskCounter)
End If
Return taskCounter
End Sub
Sub hc_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
Response.GetAsynchronously("response", File.OpenOutput(TempFolder, TaskId, False), True, TaskId)
End Sub
Sub Response_StreamFinish (Success As Boolean, TaskId As Int)
If Success Then
CompleteJob(TaskId, Success, "")
Else
CompleteJob(TaskId, Success, LastException.Message)
End If
End Sub
Sub hc_ResponseError (Response As OkHttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
If Response <> Null Then
Log(Response.ErrorResponse)
Response.Release
End If
CompleteJob(TaskId, False, Reason)
End Sub
Sub CompleteJob(TaskId As Int, success As Boolean, errorMessage As String)
Dim job As HttpJob = TaskIdToJob.Get(TaskId)
TaskIdToJob.Remove(TaskId)
job.success = success
job.errorMessage = errorMessage
job.Complete(TaskId)
End Sub