Skip to content

Commit 4f93fa7

Browse files
tobiascadeeCopilot
andauthored
fix articles stream (#70)
* fix articles stream we have to get articles through `get articles` because then we have the statistics object. adding a child stream with the statistics object as `articles_extended` as some users might not be interested in it and it requires a request per article. * fix linting * Apply suggestion from @Copilot Co-authored-by: Copilot <[email protected]> * Apply suggestion from @Copilot Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 4f6517e commit 4f93fa7

File tree

3 files changed

+79
-9
lines changed

3 files changed

+79
-9
lines changed

tap_intercom/schemas.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
ArrayType,
55
BooleanType,
66
IntegerType,
7+
NumberType,
78
ObjectType,
89
PropertiesList,
910
Property,
@@ -521,6 +522,46 @@
521522
).to_dict()
522523

523524
articles_schema = PropertiesList(
525+
Property("id", StringType),
526+
Property("parent_type", StringType),
527+
Property("parent_ids", ArrayType(IntegerType)),
528+
Property(
529+
"translated_content",
530+
ObjectType(*translated_content),
531+
),
532+
Property("languages", ArrayType(StringType)),
533+
Property(
534+
"tags",
535+
ObjectType(
536+
Property(
537+
"tags",
538+
ArrayType(
539+
ObjectType(
540+
Property("id", StringType),
541+
Property("name", StringType),
542+
Property("applied_at", IntegerType),
543+
Property(
544+
"applied_by",
545+
ObjectType(
546+
Property("type", StringType),
547+
Property("id", StringType),
548+
),
549+
),
550+
),
551+
),
552+
),
553+
),
554+
),
555+
Property("title", StringType),
556+
Property("description", StringType),
557+
Property("body", StringType),
558+
Property("author_id", IntegerType),
559+
Property("state", StringType),
560+
Property("created_at", IntegerType),
561+
Property("updated_at", IntegerType),
562+
).to_dict()
563+
564+
articles_extended_schema = PropertiesList(
524565
Property("id", StringType),
525566
Property("parent_type", StringType),
526567
Property("parent_ids", ArrayType(IntegerType)),
@@ -562,9 +603,9 @@
562603
Property("views", IntegerType),
563604
Property("conversions", IntegerType),
564605
Property("reactions", IntegerType),
565-
Property("happy_reaction_percentage", IntegerType),
566-
Property("neutral_reaction_percentage", IntegerType),
567-
Property("sad_reaction_percentage", IntegerType),
606+
Property("happy_reaction_percentage", NumberType),
607+
Property("neutral_reaction_percentage", NumberType),
608+
Property("sad_reaction_percentage", NumberType),
568609
),
569610
),
570611
Property("created_at", IntegerType),

tap_intercom/streams.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
from __future__ import annotations
44

5+
import decimal
56
import typing as t
67
from urllib.parse import parse_qsl
78

89
from tap_intercom.client import IntercomHATEOASPaginator, IntercomStream
910
from tap_intercom.schemas import (
1011
admins_schema,
12+
articles_extended_schema,
1113
articles_schema,
1214
contacts_schema,
1315
conversation_parts_schema,
@@ -16,6 +18,9 @@
1618
teams_schema,
1719
)
1820

21+
if t.TYPE_CHECKING:
22+
import requests
23+
1924

2025
class ConversationsStream(IntercomStream):
2126
"""Stream for Intercom conversations."""
@@ -98,8 +103,8 @@ class ArticlesStream(IntercomStream):
98103
"""Stream for Intercom articles."""
99104

100105
name = "articles"
101-
path = "/articles/search"
102-
records_jsonpath = "$.data.articles[*]"
106+
path = "/articles"
107+
records_jsonpath = "$.data[*]"
103108
schema = articles_schema
104109

105110
def get_new_paginator(self) -> IntercomHATEOASPaginator:
@@ -124,11 +129,34 @@ def get_url_params(
124129
Returns:
125130
Dictionary of URL parameters.
126131
"""
127-
params = {}
132+
params = super().get_url_params(context, next_page_token)
128133
if next_page_token:
129134
# parse URL for next page
130135
params.update(dict(parse_qsl(next_page_token.query)))
131-
return params
132136

133-
# default to parent class for initial request
134-
return super().get_url_params(context, next_page_token)
137+
return params
138+
139+
def get_child_context(self, record: dict, context: dict | None) -> dict: # noqa: ARG002
140+
"""Return a context dictionary for child streams."""
141+
return {"article_id": record["id"]}
142+
143+
144+
class ArticlesExtendedStream(IntercomStream):
145+
"""Stream providing extended article data, including statistics, for individual Intercom articles."""
146+
147+
name = "articles_extended"
148+
path = "/articles/{article_id}"
149+
records_jsonpath = "$"
150+
schema = articles_extended_schema
151+
parent_stream_type = ArticlesStream
152+
153+
def parse_response(self, response: requests.Response) -> t.Iterable[dict]:
154+
"""Parse the response and return an iterator of result records.
155+
156+
Args:
157+
response: A raw :class:`requests.Response`
158+
159+
Yields:
160+
One item for every item found in the response.
161+
"""
162+
yield response.json(parse_float=decimal.Decimal)

tap_intercom/tap.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def discover_streams(self) -> list[streams.IntercomStream]:
5858
streams.TeamsStream(self),
5959
streams.ContactsStream(self),
6060
streams.ArticlesStream(self),
61+
streams.ArticlesExtendedStream(self),
6162
]
6263

6364

0 commit comments

Comments
 (0)