@@ -136,6 +136,11 @@ class Jira:
136136 }
137137 TOKEN_URL = "https://auth.atlassian.com/oauth/token"
138138 API_TOKEN_URL = "https://api.atlassian.com/oauth/token/accessible-resources"
139+ HEADER_TEMPLATE = {
140+ "Content-Type" : "application/json" ,
141+ "X-Force-Accept-Language" : "true" ,
142+ "Accept-Language" : "en" ,
143+ }
139144
140145 def __init__ (
141146 self ,
@@ -200,6 +205,31 @@ def scopes(self):
200205 def using_basic_auth (self ):
201206 return self ._using_basic_auth
202207
208+ def get_headers (
209+ self , access_token : str = None , content_type_json : bool = False
210+ ) -> dict :
211+ """Get headers for API requests
212+
213+ Args:
214+ access_token: The access token to use for authorization
215+ content_type_json: Whether to include Content-Type: application/json
216+
217+ Returns:
218+ dict: Headers for API requests
219+ """
220+ headers = self .HEADER_TEMPLATE .copy ()
221+
222+ if not content_type_json :
223+ headers .pop ("Content-Type" , None )
224+
225+ if access_token :
226+ if self ._using_basic_auth :
227+ headers ["Authorization" ] = f"Basic { access_token } "
228+ else :
229+ headers ["Authorization" ] = f"Bearer { access_token } "
230+
231+ return headers
232+
203233 def get_params (self , state_encoded ):
204234 return {
205235 ** self .PARAMS_TEMPLATE ,
@@ -303,7 +333,7 @@ def get_auth(self, auth_code: str = None) -> None:
303333 "redirect_uri" : self .redirect_uri ,
304334 }
305335
306- headers = { "Content-Type" : "application/json" }
336+ headers = self . get_headers ( content_type_json = True )
307337 response = requests .post (self .TOKEN_URL , json = body , headers = headers )
308338
309339 if response .status_code == 200 :
@@ -352,15 +382,15 @@ def get_cloud_id(self, access_token: str = None, domain: str = None) -> str:
352382 """
353383 try :
354384 if self ._using_basic_auth :
355- headers = { "Authorization" : f"Basic { access_token } " }
385+ headers = self . get_headers ( access_token )
356386 response = requests .get (
357387 f"https://{ domain } .atlassian.net/_edge/tenant_info" ,
358388 headers = headers ,
359389 )
360390 response = response .json ()
361391 return response .get ("cloudId" )
362392 else :
363- headers = { "Authorization" : f"Bearer { access_token } " }
393+ headers = self . get_headers ( access_token )
364394 response = requests .get (self .API_TOKEN_URL , headers = headers )
365395
366396 if response .status_code == 200 :
@@ -442,7 +472,7 @@ def refresh_access_token(self) -> str:
442472 "refresh_token" : self ._refresh_token ,
443473 }
444474
445- headers = { "Content-Type" : "application/json" }
475+ headers = self . get_headers ( content_type_json = True )
446476 response = requests .post (url , json = body , headers = headers )
447477
448478 if response .status_code == 200 :
@@ -582,10 +612,7 @@ def get_projects(self) -> Dict[str, str]:
582612 if not access_token :
583613 return ValueError ("Failed to get access token" )
584614
585- if self ._using_basic_auth :
586- headers = {"Authorization" : f"Basic { access_token } " }
587- else :
588- headers = {"Authorization" : f"Bearer { access_token } " }
615+ headers = self .get_headers (access_token )
589616
590617 response = requests .get (
591618 f"https://api.atlassian.com/ex/jira/{ self .cloud_id } /rest/api/3/project" ,
@@ -652,10 +679,7 @@ def get_available_issue_types(self, project_key: str = None) -> list[str]:
652679 file = os .path .basename (__file__ ),
653680 )
654681
655- if self ._using_basic_auth :
656- headers = {"Authorization" : f"Basic { access_token } " }
657- else :
658- headers = {"Authorization" : f"Bearer { access_token } " }
682+ headers = self .get_headers (access_token )
659683
660684 response = requests .get (
661685 f"https://api.atlassian.com/ex/jira/{ self .cloud_id } /rest/api/3/issue/createmeta?projectKeys={ project_key } &expand=projects.issuetypes.fields" ,
@@ -700,10 +724,7 @@ def get_metadata(self) -> dict:
700724 if not access_token :
701725 return ValueError ("Failed to get access token" )
702726
703- if self ._using_basic_auth :
704- headers = {"Authorization" : f"Basic { access_token } " }
705- else :
706- headers = {"Authorization" : f"Bearer { access_token } " }
727+ headers = self .get_headers (access_token )
707728
708729 response = requests .get (
709730 f"https://api.atlassian.com/ex/jira/{ self .cloud_id } /rest/api/3/project" ,
@@ -1579,16 +1600,7 @@ def send_findings(
15791600 message = "The issue type is invalid" , file = os .path .basename (__file__ )
15801601 )
15811602
1582- if self ._using_basic_auth :
1583- headers = {
1584- "Authorization" : f"Basic { access_token } " ,
1585- "Content-Type" : "application/json" ,
1586- }
1587- else :
1588- headers = {
1589- "Authorization" : f"Bearer { access_token } " ,
1590- "Content-Type" : "application/json" ,
1591- }
1603+ headers = self .get_headers (access_token , content_type_json = True )
15921604
15931605 for finding in findings :
15941606 status_color = self .get_color_from_status (finding .status .value )
@@ -1795,16 +1807,7 @@ def send_finding(
17951807 message = "The issue type is invalid" , file = os .path .basename (__file__ )
17961808 )
17971809
1798- if self ._using_basic_auth :
1799- headers = {
1800- "Authorization" : f"Basic { access_token } " ,
1801- "Content-Type" : "application/json" ,
1802- }
1803- else :
1804- headers = {
1805- "Authorization" : f"Bearer { access_token } " ,
1806- "Content-Type" : "application/json" ,
1807- }
1810+ headers = self .get_headers (access_token , content_type_json = True )
18081811
18091812 status_color = self .get_color_from_status (status )
18101813 severity_color = self .get_severity_color (severity .lower ())
0 commit comments