Repository: googleapis/python-bigquery

Issue:
Add to_api_repr method to Model
Same as #299 but for models:
 
`AttributeError: 'Model' object has no attribute 'to_api_repr'`

Current workaround: using `model._properties` but object fields like `featureColumns` https://cloud.google.com/bigquery/docs/reference/rest/v2/models#Model will be missing.

Discussion:

Patch:
diff --git a/google/cloud/bigquery/model.py b/google/cloud/bigquery/model.py
index 092d98c2..1143b71f 100644
--- a/google/cloud/bigquery/model.py
+++ b/google/cloud/bigquery/model.py
@@ -317,6 +317,14 @@ class Model(object):
     def __repr__(self):
         return "Model(reference={})".format(repr(self.reference))
 
+    def to_api_repr(self):
+        """Construct the API resource representation of this model.
+
+        Returns:
+            Dict[str, object]: Model reference represented as an API resource
+        """
+        return json_format.MessageToDict(self._proto)
+
 
 class ModelReference(object):
     """ModelReferences are pointers to models.


Test patch:
diff --git a/tests/unit/model/test_model.py b/tests/unit/model/test_model.py
index 2c007942..9fa29a49 100644
--- a/tests/unit/model/test_model.py
+++ b/tests/unit/model/test_model.py
@@ -318,3 +318,47 @@ def test_repr(target_class):
         "Model(reference=ModelReference("
         "project_id='my-proj', dataset_id='my_dset', model_id='my_model'))"
     )
+
+
+def test_to_api_repr(target_class):
+    from google.protobuf import json_format
+
+    model = target_class("my-proj.my_dset.my_model")
+    resource = {
+        "etag": "abcdefg",
+        "modelReference": {
+            "projectId": "my-project",
+            "datasetId": "my_dataset",
+            "modelId": "my_model",
+        },
+        "creationTime": "1274284800000",
+        "lastModifiedTime": "1317484800000",
+        "modelType": "LOGISTIC_REGRESSION",
+        "trainingRuns": [
+            {
+                "trainingOptions": {"initialLearnRate": 1.0},
+                "startTime": "2010-05-19T16:00:00Z",
+            },
+            {
+                "trainingOptions": {"initialLearnRate": 0.5},
+                "startTime": "2011-10-01T16:00:00Z",
+            },
+            {
+                "trainingOptions": {"initialLearnRate": 0.25},
+                "startTime": "2012-12-21T16:00:00Z",
+            },
+        ],
+        "description": "A friendly description.",
+        "location": "US",
+        "friendlyName": "A friendly name.",
+        "labels": {"greeting": "こんにちは"},
+        "expirationTime": "1356105600000",
+        "encryptionConfiguration": {
+            "kmsKeyName": "projects/1/locations/us/keyRings/1/cryptoKeys/1"
+        },
+    }
+    model._proto = json_format.ParseDict(
+        resource, types.Model()._pb, ignore_unknown_fields=True
+    )
+    got = model.to_api_repr()
+    assert got == resource