SHARE
TWEET

apilocations

Buttermancer Mar 11th, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'package:dio/dio.dart';
  2. import 'package:sports_asylum/models/location.dart';
  3.  
  4. class LocationsApiProvider {
  5.  
  6.   //the api url
  7.   final String _apiUrl = "http://www.buttermancer.com/api/";
  8.  
  9.   //initiate the api caller
  10.   Dio _dio;
  11.  
  12.   //setup the api caller options
  13.   LocationsApiProvider() {
  14.     Options options = Options(receiveTimeout: 5000, connectTimeout: 5000);
  15.     _dio = Dio(options);
  16.   }
  17.  
  18.   //handle errors if there is an issue with the call
  19.   String _handleError(Error error) {
  20.     String errorDescription = "";
  21.     if (error is DioError) {
  22.       switch (error.type) {
  23.         case DioErrorType.CANCEL:
  24.           errorDescription = "Request to API server was cancelled";
  25.           break;
  26.         case DioErrorType.CONNECT_TIMEOUT:
  27.           errorDescription = "Connection timeout with API server";
  28.           break;
  29.         case DioErrorType.DEFAULT:
  30.           errorDescription =
  31.           "Connection to API server failed due to internet connection";
  32.           break;
  33.         case DioErrorType.RECEIVE_TIMEOUT:
  34.           errorDescription = "Receive timeout in connection with API server";
  35.           break;
  36.         case DioErrorType.RESPONSE:
  37.           errorDescription =
  38.           "Received invalid status code: ${error.response.statusCode}";
  39.           break;
  40.       }
  41.     } else {
  42.       errorDescription = "Unexpected error occured";
  43.     }
  44.     return errorDescription;
  45.   }
  46.  
  47.   Future<LocationsResponse> getlocations() async {
  48.  
  49.     //print('location query data:' + userLocationQuery);
  50.     try {
  51.       Response response = await _dio.get(_apiUrl + '/locations', data: '');
  52.           return LocationsResponse.fromJSON(response.data);
  53.     } catch (error, stacktrace) {
  54.       print("Exception occured: $error stackTrace: $stacktrace");
  55.       return LocationsResponse.withError(_handleError(error));
  56.     }
  57.   }
  58.  
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top