I am building a cricket live score and updates app. I bought an API from sports-monks and developed my flutter application and using HTTP and Dio() Network request i got the data on application interface but it slows the performance of application due to failed requests, and other problem is that it is too costly because number of API-hits are too much when refreshing data for a single user at a time while users increases it will be too much expensive.
Now I want a strategy from where i will be able to store my JSON API's response data to Firestore database and in my user application i just only read data and display to user.
Is there any way to store my JSON formatted data to Firestore or firebase server. I will refresh of some of our urls to 24hrs and some like livescore response to 30sec
CodePudding user response:
You will need to create a backend using node.js or any other language.
Create an API function to get data from sports-monks API and then direct it to firebase and store the database on firebase. Then You can connect the Firebase database to the app.
Without creating a backend database you cannot store the data from API to firebase directly at end-user as that FUNCTION will run always and end usr uses the application
CodePudding user response:
You can save your entire JSON data using the below code.
Model class
class JourneyModel {
JourneyModel({
this.key,
this.id,
this.uid,
this.username,
this.notes,
this.journeyImageUrl,
this.journeyImagePath,
this.updatedAt,
this.addedAt,
this.totalLikes,
this.totalComments,
this.tags,
});
int id;
String key;
String uid;
String username;
String notes;
String journeyImageUrl;
String journeyImagePath;
dynamic updatedAt;
dynamic addedAt;
int totalLikes;
int totalComments;
List<String> tags;
dynamic reference;
factory JourneyModel.fromRawJson(String str) =>
JourneyModel.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
JourneyModel.fromMap(Map<String, dynamic> json, {this.reference})
: assert(json != null),
key = json["key"] == null ? null : json["key"],
id = json["id"] == null ? null : json["id"],
uid = json["uid"] == null ? null : json["uid"],
username = json["username"] == null ? null : json["username"],
notes = json["notes"] == null ? null : json["notes"],
journeyImageUrl = json["journey_image_url"] == null
? null
: json["journey_image_url"],
journeyImagePath = json["journey_image_path"] == null
? null
: json["journey_image_path"],
updatedAt = json["updated_at"] == null ? null : json["updated_at"],
addedAt = json["added_at"] == null ? null : json["added_at"],
totalLikes = json["total_likes"] == null ? null : json["total_likes"],
totalComments =
json["total_comments"] == null ? null : json["total_comments"],
tags = json["tags"] == null ? null : List<String>.from(json["tags"].map((x) => x));
factory JourneyModel.fromSnapshot(DocumentSnapshot snapshot) {
final model =
JourneyModel.fromJson(snapshot.data() as Map<String, dynamic>);
model.reference = snapshot.reference.id;
return model;
}
factory JourneyModel.fromJson(Map<String, dynamic> json) => JourneyModel(
key: json["key"] == null ? null : json["key"],
id: json["id"] == null ? null : json["id"],
uid: json["uid"] == null ? null : json["uid"],
username: json["username"] == null ? null : json["username"],
notes: json["notes"] == null ? null : json["notes"],
journeyImageUrl: json["journey_image_url"] == null
? null
: json["journey_image_url"],
journeyImagePath: json["journey_image_path"] == null
? null
: json["journey_image_path"],
updatedAt: json["updated_at"] == null ? null : json["updated_at"],
addedAt: json["added_at"] == null ? null : json["added_at"],
totalLikes: json["total_likes"] == null ? null : json["total_likes"],
totalComments:
json["total_comments"] == null ? null : json["total_comments"],
tags: json["tags"] == null ? null : List<String>.from(json["tags"].map((x) => x)),
);
Map<String, dynamic> toJson() => {
"reference": reference == null ? null : reference,
"id": id == null ? null : id,
"key": key == null ? null : key,
"uid": uid == null ? null : uid,
"username": username == null ? null : username,
"notes": notes == null ? null : notes,
"journey_image_url": journeyImageUrl == null ? null : journeyImageUrl,
"journey_image_path":
journeyImagePath == null ? null : journeyImagePath,
"updated_at": updatedAt == null ? null : updatedAt,
"added_at": addedAt == null ? null : addedAt,
"total_likes": totalLikes == null ? null : totalLikes,
"total_comments": totalComments == null ? null : totalComments,
"tags": tags == null ? null : List<dynamic>.from(tags.map((x) => x)),
};
}
Firestore class
class FirebaseDB {
static Future<DocumentReference> addJourney(
{@required JourneyModel journey}) async {
final collection =
FirebaseFirestore.instance.collection("journeys");
return collection.add(journey.toJson());
}
static Future<List<JourneyModel>> getAllJourneys() async {
Query collection =
FirebaseFirestore.instance.collection("journeys");
collection = collection.orderBy(ParamsArgus.KEY_ADDED_AT, descending: true);
QuerySnapshot snapshot = await collection.get();
return snapshot.docs.map((DocumentSnapshot doc) {
return JourneyModel.fromSnapshot(doc);
}).toList();
}
}
CodePudding user response:
Now I want a strategy from where i will be able to store my JSON API's response data to Firestore database and in my user application i just only read data and display to user. Is there any way to store my JSON formatted data to Firestore or firebase server. I will refresh of some of our urls to 24hrs and some like livescore response to 30sec
You could use one or more scheduled Cloud Function(s) that will call the API(s) and store the data in the Firestore database.
Cloud Functions are executed on the Firebase/Google Cloud back-end infrastructure and can very well call a REST API endpoint, by using, for example, the Node.js axios library. You'll find several examples on the web, including SO, by searching for "Firebase Cloud Functions axios".
Upon receiving the data from the API, the Cloud Function can write to the Firestore DB, using the Node.js Admin SDK (Again, many examples available on SO). Your users only access the Firestore DB, and you take advantage of the Firestore performance.
This way you only pay for the calls to the API(s) triggered by the Cloud Function, following the frequency you decide, and not the calls triggered by the users from their instance of your app.
