-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetworkClient.kt.rtf
More file actions
196 lines (195 loc) · 6.23 KB
/
Copy pathNetworkClient.kt.rtf
File metadata and controls
196 lines (195 loc) · 6.23 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
{\rtf1\ansi\ansicpg1252\cocoartf2865
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
\f0\fs24 \cf0 package com.suspension.analyzer.network\
\
import okhttp3.MediaType.Companion.toMediaType\
import okhttp3.MultipartBody\
import okhttp3.OkHttpClient\
import okhttp3.RequestBody.Companion.asRequestBody\
import okhttp3.RequestBody.Companion.toRequestBody\
import org.json.JSONArray\
import org.json.JSONObject\
import retrofit2.Retrofit\
import retrofit2.converter.gson.GsonConverterFactory\
import retrofit2.http.*\
import java.io.File\
import java.util.concurrent.TimeUnit\
\
/**\
* API models for backend communication\
*/\
data class VehicleProfileDto(\
val make: String,\
val model: String,\
val year: Int,\
val suspensionType: String,\
val notes: String = ""\
)\
\
data class TestRunSubmissionDto(\
val vehicleId: String,\
val testType: String,\
val roadSurface: String,\
val protocolComplianceScore: Float,\
val peakLateralG: Float,\
val peakLongitudinalG: Float,\
val peakVerticalG: Float,\
val reboundSettleTime: Long,\
val sensorDataJson: String,\
val ambientTemp: Float = 0f\
)\
\
data class BenchmarkComparisonDto(\
val vehicleId: String,\
val testType: String,\
val yourPeakLateralG: Float,\
val baselinePeakLateralG: Float,\
val baselinePercentile: Int,\
val sampleCount: Int\
)\
\
/**\
* Retrofit API service interface\
*/\
interface SuspensionAnalyzerApi \{\
\
@POST("api/v1/vehicles")\
suspend fun submitVehicleProfile(\
@Body profile: VehicleProfileDto\
): ApiResponse<String>\
\
@Multipart\
@POST("api/v1/test-runs/submit")\
suspend fun submitTestRun(\
@Part("data") data: RequestBody,\
@Part photo: MultipartBody.Part?\
): ApiResponse<TestRunResponse>\
\
@GET("api/v1/benchmarks/compare")\
suspend fun getBenchmarkComparison(\
@Query("make") make: String,\
@Query("model") model: String,\
@Query("testType") testType: String,\
@Query("peakLateralG") peakLateralG: Float\
): ApiResponse<BenchmarkComparisonDto>\
\
@GET("api/v1/benchmarks/vehicle")\
suspend fun getVehicleBenchmarks(\
@Query("vehicleId") vehicleId: String\
): ApiResponse<List<BenchmarkDataDto>>\
\}\
\
data class ApiResponse<T>(\
val success: Boolean,\
val data: T? = null,\
val error: String? = null\
)\
\
data class TestRunResponse(\
val testRunId: String,\
val message: String\
)\
\
data class BenchmarkDataDto(\
val peakLateralG: Float,\
val peakLongitudinalG: Float,\
val peakVerticalG: Float,\
val count: Int,\
val percentile: Int\
)\
\
/**\
* Network client for backend communication\
*/\
class SuspensionAnalyzerNetworkClient(private val baseUrl: String) \{\
\
private val httpClient = OkHttpClient.Builder()\
.connectTimeout(30, TimeUnit.SECONDS)\
.readTimeout(30, TimeUnit.SECONDS)\
.writeTimeout(30, TimeUnit.SECONDS)\
.build()\
\
private val retrofit = Retrofit.Builder()\
.baseUrl(baseUrl)\
.client(httpClient)\
.addConverterFactory(GsonConverterFactory.create())\
.build()\
\
private val api = retrofit.create(SuspensionAnalyzerApi::class.java)\
\
suspend fun submitVehicleProfile(profile: VehicleProfileDto): Result<String> \{\
return try \{\
val response = api.submitVehicleProfile(profile)\
if (response.success && response.data != null) \{\
Result.success(response.data)\
\} else \{\
Result.failure(Exception(response.error ?: "Unknown error"))\
\}\
\} catch (e: Exception) \{\
Result.failure(e)\
\}\
\}\
\
suspend fun submitTestRun(\
submission: TestRunSubmissionDto,\
photoFile: File?\
): Result<TestRunResponse> \{\
return try \{\
val submissionJson = JSONObject().apply \{\
put("vehicleId", submission.vehicleId)\
put("testType", submission.testType)\
put("roadSurface", submission.roadSurface)\
put("protocolComplianceScore", submission.protocolComplianceScore)\
put("peakLateralG", submission.peakLateralG)\
put("peakLongitudinalG", submission.peakLongitudinalG)\
put("peakVerticalG", submission.peakVerticalG)\
put("reboundSettleTime", submission.reboundSettleTime)\
put("ambientTemp", submission.ambientTemp)\
put("sensorData", JSONArray(submission.sensorDataJson))\
\}.toString()\
\
val dataRequestBody = submissionJson.toRequestBody("application/json".toMediaType())\
\
val photoPart = photoFile?.let \{\
MultipartBody.Part.createFormData(\
"photo",\
it.name,\
it.asRequestBody("image/jpeg".toMediaType())\
)\
\}\
\
val response = api.submitTestRun(dataRequestBody, photoPart)\
\
if (response.success && response.data != null) \{\
Result.success(response.data)\
\} else \{\
Result.failure(Exception(response.error ?: "Upload failed"))\
\}\
\} catch (e: Exception) \{\
Result.failure(e)\
\}\
\}\
\
suspend fun getBenchmarkComparison(\
make: String,\
model: String,\
testType: String,\
peakLateralG: Float\
): Result<BenchmarkComparisonDto> \{\
return try \{\
val response = api.getBenchmarkComparison(make, model, testType, peakLateralG)\
if (response.success && response.data != null) \{\
Result.success(response.data)\
\} else \{\
Result.failure(Exception(response.error ?: "Fetch failed"))\
\}\
\} catch (e: Exception) \{\
Result.failure(e)\
\}\
\}\
\}\
}